Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
721 views
in Technique[技术] by (71.8m points)

xslt 1.0 - Using XSLT1 how can I simply assign an incrementing value beginning with 1?

In my XSL script I have the following:

<body>
    <xsl:for-each select="MeetingWorkBook/Meeting">
        <xsl:apply-templates select="TFGW/BibleReadingItem/Readers/Reader"/>
        <xsl:apply-templates select="AYFM/StudentItem/Students/Student"/>
    </xsl:for-each>
</body>

The start of the template is:

<xsl:template match="Student | Reader">
    <xsl:if test="self::Student">
         <hr/>
    </xsl:if>
    <div>
        <xsl:attribute name="id">
            <xsl:text>containter-student-slip</xsl:text>
            <xsl:variable name="pos" select="position()"/>
            <xsl:value-of select="$pos"/>
        </xsl:attribute>

I want the id to simply be a numerical value, starting at 1 and up.

I realise it is no good using position() because it is relative to both matching element types.

Using XSLT1 how can I simply assign an incrementing value beginning with 1?

question from:https://stackoverflow.com/questions/65886097/using-xslt1-how-can-i-simply-assign-an-incrementing-value-beginning-with-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to use position(), you could probably* just change:

<xsl:for-each select="MeetingWorkBook/Meeting">
    <xsl:apply-templates select="TFGW/BibleReadingItem/Readers/Reader"/>
    <xsl:apply-templates select="AYFM/StudentItem/Students/Student"/>
</xsl:for-each>

to:

<xsl:for-each select="MeetingWorkBook/Meeting">
    <xsl:apply-templates select="TFGW/BibleReadingItem/Readers/Reader | AYFM/StudentItem/Students/Student"/>
</xsl:for-each>

Alternatively*, it may be possible to use xsl:number.


(*) Of course, without a reproducible example these are merely guesses.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...