Reputation:
I have been trying to increment attribute value of following xml. Please help where I am wrong in getting desired result.
Input xml:
<?xml version="1.0" encoding="UTF-8" ?>
<GetProratedPriceInput xmlns="http://www.BRMProration.org">
<ProductsInfo>
<START_T>2015-07-28T18:07:07.561</START_T>
<END_T>2015-07-28T18:07:07.561</END_T>
</ProductsInfo>
<ProductsInfo>
<START_T>2015-07-28T18:07:07.561</START_T>
<END_T>2015-07-28T18:07:07.561</END_T>
</ProductsInfo>
<ProductsInfo>
<START_T>2015-07-28T18:07:07.562</START_T>
<END_T>2015-07-28T18:07:07.562</END_T>
</ProductsInfo>
</GetProratedPriceInput>
XSLT which I am using.
<xsl:template match="/">
<xsl:variable name="counter">
<xsl:value-of select="0"/>
</xsl:variable>
<abc:inputFlist>
<xsl:for-each select="/ns2:GetProratedPriceInput/ns2:ProductsInfo">
<abc:RESULTS elem="{$counter}">
<abc:END_T>
<xsl:value-of select="ns2:END_T"/>
</abc:END_T>
<abc:START_T>
<xsl:value-of select="ns2:START_T"/>
</abc:START_T>
<xsl:variable name="counter">
<xsl:value-of select="$counter + 1"/>
</xsl:variable>
</abc:RESULTS>
</xsl:for-each>
</abc:inputFlist>
</xsl:template>
</xsl:stylesheet>
Ouput from this xslt:
<abc:inputFlist xmlns:abc="http://xmlns.oracle.com/abc/schemas/BusinessOpcodes">
<abc:RESULTS elem="0">
<abc:END_T>2015-07-28T18:07:07.561</abc:END_T>
<abc:START_T>2015-07-28T18:07:07.561</abc:START_T>
</abc:RESULTS>
<abc:RESULTS elem="0">
<abc:END_T>2015-07-28T18:07:07.561</abc:END_T>
<abc:START_T>2015-07-28T18:07:07.561</abc:START_T>
</abc:RESULTS>
<abc:RESULTS elem="0">
<abc:END_T>2015-07-28T18:07:07.562</abc:END_T>
<abc:START_T>2015-07-28T18:07:07.562</abc:START_T>
</abc:RESULTS>
</abc:inputFlist>
but Desired output is as below:
<abc:inputFlist xmlns:abc="http://xmlns.oracle.com/abc/schemas/BusinessOpcodes">
<abc:RESULTS elem="0">
<abc:END_T>2015-07-28T18:07:07.561</abc:END_T>
<abc:START_T>2015-07-28T18:07:07.561</abc:START_T>
</abc:RESULTS>
<abc:RESULTS elem="1">
<abc:END_T>2015-07-28T18:07:07.561</abc:END_T>
<abc:START_T>2015-07-28T18:07:07.561</abc:START_T>
</abc:RESULTS>
<abc:RESULTS elem="2">
<abc:END_T>2015-07-28T18:07:07.562</abc:END_T>
<abc:START_T>2015-07-28T18:07:07.562</abc:START_T>
</abc:RESULTS>
</abc:inputFlist>
Please help what is wrong with increment counter variable.
Thanks for all the help.
Upvotes: 0
Views: 426
Reputation: 70598
Variables in XSLT are immutable, and cannot be changed. In your code you are simply defining a new variable which "shadows" the previous variable, but it will actually go out of scope immediately at the end of each xsl:for-each
block
You don't need to use a variable at all here. You can make use of the position()
function to keep a track of the count. Just change the line <abc:RESULTS elem="{$counter}">
to this:
<abc:RESULTS elem="{position() - 1}">
Upvotes: 1