1

My problem is that i want to auto increment the value of Element tag "BOOKSEQNUM" by 10 starting from the 100000, Please see below, i have put the code of input, ouput (coming with my XSLT), XSLT code and the output that it should come. Can you please help me if this is possible in XSLT, i have searched for its solution but not found anything.

Input file:

                <?xml version="1.0" encoding="utf-8"?>
                <r id="TYPL.HB001">
                <COMM>
                <commbody>
                <level id="comchap">
                <level id="TTPL_Z9.0002">
                <level id="TTPL_Z9.0003"/>
                </level>
                <level id="TTPL_Z9.0004.para0">
                <level id="TTPL_Z9.0005" >
                <level id="TTPL_Z9.0006"/>
                <level id="TTPL_Z9.0016"/>
                <level id="TTPL_Z9.0022"/>
                <level id="TTPL_Z9.0031">
                <level id="TTPL_Z9.0041"/>                 
                </level>
                </level>
                <level id="TTPL_Z9.0046">
                <level id="TTPL_Z9.0047"/>
                <level id="TTPL_Z9.0058"/>
                <level id="TTPL_Z9.0063">
                </level>
                </level>
                </level>
                </level>
                </commbody>
                </COMM>
                </r>

XSLT Code:

            <?xml version="1.0" encoding="UTF-8"?>
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            version="2.0"> 
            <xsl:output omit-xml-declaration="yes" indent="yes"/>
            <xsl:template match="/">
            <xsl:apply-templates select="//level"/>
            </xsl:template>
            <xsl:template match="level">
            <xsl:variable name="BSN_Number">100000</xsl:variable>
            <xsl:element name="BOOKSEQNUM">
            <xsl:attribute name="id" select="./@id"/>
            <xsl:for-each select=".">
            <xsl:value-of select="$BSN_Number+10"/>
            </xsl:for-each>
            </xsl:element>       
            </xsl:template>
            </xsl:stylesheet>

Output coming:

            <BOOKSEQNUM id="comchap">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0002">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0003">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0004">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0005">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0006">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0016">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0022">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0031">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0041">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0046">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0047">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0058">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0063">100010</BOOKSEQNUM>

But I want output should be like:

    <BOOKSEQNUM id="comchap">100010</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0002">100020</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0003">100030</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0004">100040</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0005">100050</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0006">100060</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0016">100070</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0022">100080</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0031">100090</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0041">1000100</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0046">1000110</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0047">1000120</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0058">1000130</BOOKSEQNUM>
    <BOOKSEQNUM id="TTPL_Z9.0063">1000140</BOOKSEQNUM>

1 Answer 1

3

Variables in XSLT are immutable, and cannot be changed. However, in this case all you need to do a simple bit of maths with the position() function

 <xsl:value-of select="10000 + position() * 10"/>

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="//level"/>
    </xsl:template>

    <xsl:template match="level">
        <BOOKSEQNUM id="{@id}">
            <xsl:value-of select="10000 + position() * 10"/>
        </BOOKSEQNUM>       
    </xsl:template>
</xsl:stylesheet>

As a side note, there is no need to use xsl:element to create an element with a static name, just write out the element name directly. Also note the use of Attribute Value Templates in creating the id attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Tim, you resolved my problem.. By your above code i am getting perfect output that i want and also feeling wonder that it can be done by position() function!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.