Reputation: 178350
Apologies if this a seen as a "gimme the codez" type question but my xpath skills were not up to scratch on this. It is in my opinion generic enough to be of interest for more people
Given this multilingual XML file below. We need to substitue the blank Dutch entries with their English sibling element.
Input:
<Testing>
<T4 t="dutch"></T4>
<T4 t="english">Testing Software</T4>
<T4 t="french"/>
<T4 t="italian"/>
</Testing>
<P>
<T1 t="dutch"></T1>
<T1 t="english">Testing Phase. </T1>
<T1 t="french"></T1>
<T1 t="italian"></T1>
</P>
output:
<Testing>
<T4 t="dutch">
<trans>Testing Software</trans>
</T4>
<T4 t="english">Testing Software</T4>
<T4 t="french"/>
<T4 t="italian"/>
</Testing>
<P>
<T1 t="dutch"><trans>Testing Phase.</trans></T1>
<T1 t="english">Testing Phase. </T1>
<T1 t="french"></T1>
<T1 t="italian"></T1>
</P>
Upvotes: 1
Views: 53
Reputation: 52888
This should work...
XML Input (wrapped in input
to be well-formed)
<input>
<Testing>
<T4 t="dutch"></T4>
<T4 t="english">Testing Software</T4>
<T4 t="french"/>
<T4 t="italian"/>
</Testing>
<P>
<T1 t="dutch"></T1>
<T1 t="english">Testing Phase. </T1>
<T1 t="french"></T1>
<T1 t="italian"></T1>
</P>
</input>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@t='dutch'][not(node())]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<trans><xsl:value-of select="normalize-space(../*[@t='english'])"/></trans>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output
<input>
<Testing>
<T4 t="dutch">
<trans>Testing Software</trans>
</T4>
<T4 t="english">Testing Software</T4>
<T4 t="french"/>
<T4 t="italian"/>
</Testing>
<P>
<T1 t="dutch">
<trans>Testing Phase.</trans>
</T1>
<T1 t="english">Testing Phase. </T1>
<T1 t="french"/>
<T1 t="italian"/>
</P>
</input>
Upvotes: 1