Select a data with some condtion in xslt
My data is like this:
&l开发者_JAVA百科t;Source>
<input>
<plant>
YYYYY
</plant>
<group>
Georgia Power Co
</group>
<unit>
Wilmot IC 5
</unit>
<Status>
Operating
</Status>
<code>
56504
</code>
<change>
2
</change>
</input>
<input>
<plant>
XXXXX
</plant>
<group>
Detroit Edison Co
</group>
<unit>
Wilmot IC 5
</unit>
<Status>
Operating
</Status>
<code>
56504
</code>
<change>
0
</change>
</input>
<input>
<plant>
ZZZZZZ
</plant>
<group>
Detroit Edison Co
</group>
<unit>
Wilmot IC 4
</unit>
<Status>
Operating
</Status>
<code>
56504
</code>
<change>
2
</change>
</input>
</Source>
I wanted to select the input node data based on the following conditions:
- The input data which has
<change>
as 2 should compare the<units>
having<change>
as 0. - If the units match in both the records, I should compare other values in both the nodes.
- If there are any changes to the values I need to bold them.
- In the output data I should not have the set whose change was 0. Meaning that it should only display the input which is 2.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="input">
<xsl:variable name="other" select="../input[change=0 and unit=current()/unit]"/>
<xsl:if test="$other">
<match>
<unit>
<xsl:value-of select="unit"/>
</unit>
<xsl:for-each select="*[name() != 'change']">
<xsl:variable name="name" select="name()"/>
<xsl:variable name="original" select="$other/*[name() = $name]"/>
<xsl:if test="not(current() = $original)">
<value>
<xsl:attribute name="name">
<xsl:value-of select="$name"/>
</xsl:attribute>
<two>
<xsl:value-of select="current()"/>
</two>
<zero>
<xsl:value-of select="$original"/>
</zero>
</value>
</xsl:if>
</xsl:for-each>
</match>
</xsl:if>
</xsl:template>
<xsl:template match="/Source">
<xsl:apply-templates select="input[change=2]"/>
</xsl:template>
</xsl:stylesheet>
returns
<match>
<unit>
Wilmot IC 5
</unit>
<value name="plant">
<two>
YYYYY
</two>
<zero>
XXXXX
</zero>
</value>
<value name="group">
<two>
Georgia Power Co
</two>
<zero>
Detroit Edison Co
</zero>
</value>
</match>
which seems like what you're looking for.
精彩评论