我是一个完全新手xslt.我正在尝试进行一个转换,对源xml文档进行微小的更改,例如:
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
<file>
<trans-unit>
<source>Kodiak1 [[Name]]</source>
<target></target>
</trans-unit>
</file>
</xliff>
Run Code Online (Sandbox Code Playgroud)
至:
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
<file>
<trans-unit>
<source>Kodiak1 [[Name]]</source>
<target>Kodiak1 <ph>Name</ph></target>
</trans-unit>
</file>
</xliff>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我想出了:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="target">
<target>
<xsl:value-of select="preceding-sibling::source" />
</target>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
它将文本从<source>节点复制到<target>节点但现在我被卡住了 - 尤其是因为如果我添加另一个<xsl:template match="...">它与原始文本匹配(例如,不在新文本上 - 你能告诉我下一步应该是什么吗?
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="trans-unit[contains(source, '[[')]/target">
<xsl:variable name="vS" select="../source"/>
<target>
<xsl:value-of select="substring-before($vS, '[')"/>
<ph>
<xsl:value-of select=
"translate(substring-after($vS, '[['), ']','')"/>
</ph>
</target>
</xsl:template>
<xsl:template match="target">
<target>
<xsl:value-of select="../source"/>
</target>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
应用于此XML文档时(提供的文档稍微有点兴趣):
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
<file>
<trans-unit>
<source>Kodiak1 [[Name]]</source>
<target></target>
</trans-unit>
<trans-unit>
<source>Kodiak2</source>
<target></target>
</trans-unit>
</file>
</xliff>
Run Code Online (Sandbox Code Playgroud)
产生想要的,正确的结果:
<xliff xmlns:xliff="urn:oasis:names:tc:xliff:document:1.1" version="1.1">
<file>
<trans-unit>
<source>Kodiak1 [[Name]]</source>
<target>Kodiak1 <ph>Name</ph>
</target>
</trans-unit>
<trans-unit>
<source>Kodiak2</source>
<target>Kodiak2</target>
</trans-unit>
</file>
</xliff>
Run Code Online (Sandbox Code Playgroud)
说明:
适当使用模板和标准XPath函数substring-before(),substring-after()以及translate().
| 归档时间: |
|
| 查看次数: |
316 次 |
| 最近记录: |