开发者

How to retain a variable value and update it in XSLT

I have a attribute called ID, which is incremented 1 everytime the value is assigned it to.I have a situation where the value to be incremented everytime the loop is entered. The outer loop 开发者_Go百科has 2 records and inner loop has 3 records.

Outer loop 1 - Inner loop 1, 2, and 3, the ID attribute gets incremented as 1, 2 and 3, but

Outer loop 2 - Inner loop .... again the ID attribute starts with 1. I need this to be 4, 5 and so on..

This increment ID has no data related to the input XML file. All the processing has to be done in the XSLT.

XSLT FIle :

<xsl:variable name="localISYMPid" select="0"/>  
<xsl:element name="Test1">   

    <xsl:for-each select="Solutions/Solution">

        <xsl:if test="Observations/Observation!= ''  ">

            <xsl:for-each select="Observations/Observation">
                <xsl:element name="Roles">

                    <!-- Generating the ID value -->
                    <xsl:variable name="ids" select="generate-id(.)"/>
                    <!-- ***************************-->

                    <xsl:attribute name="CK"><xsl:value-of select="substring-after($ids,'Solution')"/></xsl:attribute>
                    <xsl:attribute name="ID"><xsl:value-of select="position() + $localISYMPid "/></xsl:attribute>
                </xsl:element>
            </xsl:for-each>
        </xsl:if>
    </xsl:for-each>
</xsl:element>

Please help me in printing the value of ID 4 as when the inner loop is looped 3 times for outer loop 1 and for outer loop 2 and when the inner loop is 1, the ID value shall be 4 .as of now, its printing 1 again..

ID     1
ID     2
ID     3
----------
ID     1(it should print 4)

I am using XSLT 1.0

Thank you, Ramm


It's always useful to show your source document. But I suspect you can achieve what you want with something like

<xsl:attribute name="ID">
  <xsl:number level="any" from="Solution"/>
</xsl:attribute>


You're using the position() function to determine the ID which is local to it's parent element (Observation in this case). If you really want a global one then you could do something like count(preceding::Roles) + 1 instead, which will count all Roles nodes prior to the current one, and then add one.

You might need to restrict the preceding::Roles predicate a little further depending on your requirements, such as preceding::Roles[parent::Observation] for example, to make sure you're only counting Roles elements that are a child of an Observation element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜