My input XML consists of the following,
<root>
<entry>
<type>U</type>
<value>111</value>
</entry>
<entry>
<type>X</type>
<value>222</value>
</entry>
<entry>
<type>E</type>
<value>333</value>
</entry>
<entry>
<type>Q</type>
<value>444</value>
</entry>
</root>
Output i required,
<ROOT>
<ENTRY>
<SLNO>1</SLNO>
<VALUE>111</VALUE>
</ENTRY>
<ENTRY>
<VALUE>222</VALUE>
</ENTRY>
<ENTRY>
<VALUE>333</VALUE>
</ENTRY>
<ENTRY>
<SLNO>2</SLNO>
<VALUE>444</VALUE>
</ENTRY>
</ROOT>
I need to parse all the records, but need to put serial number for records whose type is not X and E.
I have written a for-each to for the same and used ´position()´ to display a serial number with condtion for type E and X.
So I’m getting serial number as 1, 4 instead of 1, 2 because of ´postion()´.
I thought of creating a global variable and increment it inside my if block, but XSLT 1.0 will not allow to increment variable values.
How can I achieve this?
My sample XSL code is asl follows,
<xsl:for-each select="/ROOT/ENTRY">
<xsl:if test="(TYPE != 'X') and (TYPE != 'E')">
<xsl:text><![CDATA[<SLNO>]]></xsl:text>
<xsl:number value="position()"/>
<xsl:text><![CDATA[</SLNO>]]></xsl:text>
</xsl:if>
<!-- Printing remaining values -->
</xsl:for-each>
Please help.
Instead of using position(), you could count the number of preceding type elements in your XML
Additionally, you can simplify your current XSLT by matching the type element directly, rather than the entry element, and then just replace it with the new slno element.
For example, try the following XSLT
When applied to your sample XML, the following is output
Note, I am using all lower-case element names here. I wasn’t sure from your question if you wanted to translate them into upper-case or not.