How to use incremental counter to provide unique identifier in XSLT?

I use XSLT to convert a very large XML document into (X) HTML. For some tags, I convert them to <div>. I would like to be able to create a unique identifier for these tags, using an extra integer to form part of the unique identifier.

An example of the rule used:

<xsl:template match="bookcoll/book">
    <div class="book">
        <xsl:apply-templates/>
    </div>
</xsl:template>

This XSLT template works well. Now I would like to mark the tag:

<div class="book">;  

becomes:

<div class="book" id="book-[COUNTER-VALUE]">  

Ideally, the counter will start at 1, not 0.

I don't know if this matters, I use javax.xml.parsers and javax.xml.transform Java packages to perform the actual conversion. I am a bit XSLT noob, so if there is any relevant information that I missed, let me know.

XSLT?

+3
4

// :

<div class="book" id="book-{generate-id()}">

, , . HTML- ( ).

EDIT: , :

<!-- in the calling template… -->
<xsl:apply-templates select="bookcoll/book[xpath to filter them if necessary]" />

<!-- …later -->
<xsl:template match="bookcoll/book">
  <div class="book" id="book-{position()}">
    <xsl:apply-templates/>
  </div>
</xsl:template>

format-number() position() .

position() node "", . <xsl:apply-templates> , , .

+7

, position(), xsl:for-each:

<xsl:template match="bookcoll">
    <xsl:for-each select="book">
        <div class="book" id="book-{position()}">
            <xsl:apply-templates/>
        </div>
    </xsl:for-each>
</xsl:template>

- :

<div class="book" id="book-1">book1</div>
<div class="book" id="book-2">book2</div>
<div class="book" id="book-3">book3</div>

<bookcoll>
    <book>book1</book>
    <book>book2</book>
    <book>book3</book>
</bookcoll>
+4

Java- ... , , 1) Java / , Oxygen ( ) 2) , , , ( , , , Etc)

0
source

Take a look at the position () function.

0
source

Source: https://habr.com/ru/post/1722485/


All Articles