Add ordinal number to each item when using Xsl:for-each in XSL

Source: Internet
Author: User
Tags cdata xsl

You often use For-each to iterate through a node set in XML, but often you need to add a sequence number

To do this:

<xsl:template match="Book">
<xsl:for-each select="Authors/Author">
<xsl:number value="position()" />
<xsl:value-of select="Name" />
</xsl:for-each>
</xsl:template>

Of course, if you're not satisfied, you want to add "." to the numbers. You can do this:

<xsl:number value= "position ()" format= "1."/>

Even, you can use the picture to express 1\2\3 and so on:

<xsl:element name="img">
<xsl:attribute name="src">images/n<xsl:number value="position()" format="1" />.gif</xsl:attribute>
<xsl:attribute name="border">0</xsl:attribute>
</xsl:element>

Note: You have a file like this in the Images folder: N1.gif,n2.gif,n3.gif ...

Let's say we have XML as follows:

<Book>
<ID>125353-8993-3737</ID>
<ISBN>22222</ISBN>
<Title>共产主义一定能胜利</Title>
<Authors>
<Author>
<Name><![CDATA[Bill&Hoele]]></Name>
<ID>12</ID>
</Author>
<Author>
<Name><![CDATA[Eylor]]></Name>
<ID>1</ID>
</Author>
</Authors>
</Book>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.