How to increment a XSL integer variable

2019-01-26 20:21发布

问题:

Updated

I have some huge data, which becomes a large table say table parent

each table parent's row will correspond to another table (code given below) say table child So when any particular column from the table parent row is clicked (hyperlinked) it goes to that part of table table child

so i need a counter to differentiate between each table child. Please help me figure out this problem. Thanks

<xsl:choose>
    <xsl:variable name="counter" as="xs:integer"/>
    $counter=0      <!--here i am assigning 0-->
    <xsl:when test="DBInfo/ORSDBInfo/ORSReposTableTypeInd1/ORSReposColumAllWithTableTypeInd1/@ColumnNm">
        <dd>
            <xsl:for-each select="DBInfo/ORSDBInfo/ORSReposTableTypeInd1">
                <div class="horz">
                <a name="_ORS$counter" href="#_top">ORSReposColumAllWithTableTypeInd1:<xsl:value-of select="$counter"/> </a>
                <table border="1">          <!--above I am using counter to print-->
                    <tbody>
                        <tr>
                            <th>Creator</th>
                            <th>LastUpdate</th>
                            <th>UpdatedBy</th>
                        </tr>
                <xsl:for-each select="ORSReposColumAllWithTableTypeInd1">
                    <tr>
                        <td><xsl:value-of select="@Creator"/></td>
                        <td><xsl:value-of select="@LastUpdate"/></td>
                        <td><xsl:value-of select="@UpdatedBy"/>
                    </tr>
                </xsl:for-each>
                    </tbody>
                </table>
            </div>
            $counter=$counter+1     <!--Counter is incremented-->
            <br/>
            </xsl:for-each>
        </dd>
    </xsl:when>
    <xsl:otherwise>
    </xsl:otherwise>
</xsl:choose>

More Updates

So after referring to Wilfred's answer I came up with

<a name="_ORS" href="#_top">ORSReposColumAllWithTableTypeInd1_<xsl:number value="position()" format="1" /></a>

but now how to use it inside <a name="_ORS" so that i get _ORS1, _ORS2, _ORS3 and so on...

回答1:

What about using:

<xsl:number/>

See http://www.w3.org/TR/xslt#number

There are various ways to include it as attributes into your anchor tag.

Option 1: Using variables

<xsl:variable name="number">
  <xsl:number/>
</xsl:variable>
<a name="{$number}">blabla</a>

Option 2: xsl:attribute

<a>
  <xsl:attribute name="name">
    <xsl:number/>
  </xsl:attribute>
</a>


回答2:

Use position(). Since you are incrementing this once per for-each why don't you simply use position()?

<a name="_ORS$counter" href="#_top">ORSReposColumAllWithTableTypeInd1:<xsl:value-of select="position()"/> </a>


回答3:

The notion of an increment is foreign to XSLT as a functional language. However, you could just count the number of preceding elements:

<xsl:value-of select="count(preceding-sibling::ORSReposTableTypeInd1)"/>

Depending on your document layout, this might get more complicated, your milage may vary.



回答4:

I have same problem and simplest way to solve it is to use Saxon. Here you can find my solution Incrementing and checking the counter variable in XSLT



标签: xslt