I’m a complete newcomer to xslt. I’m trying to come up with a transformation that makes minor changes to a source xml document, eg from:
<?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>
to:
<?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>
So far I’ve come up with:
<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>
Which copies the text from the <source> node to the <target> node but now I’m stuck – not least because if I add another <xsl:template match="..."> it matches on the original (eg , not on the new text – can you tell me what the next step should be?
This transformation:
when applied on the this XML document (the provided one made slightly more interesting):
produces the wanted, correct result:
Explanation:
Appropriate use of templates and the standard XPath functions
substring-before(),substring-after()andtranslate().