I want to modify a node in a branch depending on it's sibling. In the following example, I want to add <var2 Value='D'/>
to the pre-existing <nest2>
if nest1
contains <var1 Value='A'>
.
Input:
<variables>
<nest1>
<var1 Value='A'/>
<var1 Value='B'/>
</nest1>
<nest2>
<var2 Value='C'/>
</nest2>
</variables>
Output
<variables>
<nest1>
<var1 Value='A'/>
<var1 Value='B'/>
</nest1>
<nest2>
<var2 Value='C'/>
<var2 Value='D'/>
</nest2>
</variables>
I can match <var1 Value='A'/>
, but I don't know how to modify the pre-existing <nest2>
. I can match <nest2>
, but I don't know how to check to see if <var1 Value='A'/>
exists in <nest1>
. I'm ju开发者_JAVA技巧st stumped on where to go from here.
I am using xslt 1.0.
Here is a sample stylesheet that should do the job:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="nest2[preceding-sibling::nest1/var1[@Value = 'A']]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<var2 Value="D"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
精彩评论