在xslt中创建序列号

时间:2012-11-26 07:24:52

标签: xml xslt xslt-2.0

输入XML:

<doc>
 <section>
  <para>Paragraph 1</para>
  <para>Paragraph 2</para>
  <para>Paragraph 3</para>
 </section>
  <para>Paragraph 4</para>
  <para>Paragraph 5</para>
  <para>Paragraph 6</para>
</doc>

必需的输出:

<book>
<section>
  <p class="para" id="0001">Paragraph 1</p>
  <p class="para" id="0002">Paragraph 2</p>
  <p class="para" id="0003">Paragraph 3</p>
 </section>
  <p class="para" id="0004">Paragraph 4</p>
  <p class="para" id="0005">Paragraph 5</p>
  <p class="para" id="0006">Paragraph 6</p>
 </book>

我尝试使用以下XSL,但我没有获得所需的输出。有人可以帮我吗?

<xsl:template match="para">
    <xsl:variable name="count" select="position()"/>
    <p class="para" id="{$count}">
        <xsl:apply-templates/>
    </p>
</xsl:template>

我得到的结果是:

<p class="para" id="2">Paragraph 1</p>
<p class="para" id="4">Paragraph 2</p>
<p class="para" id="6">Paragraph 3</p>

    <p class="para" id="4">Paragraph 4</p>
    <p class="para" id="6">Paragraph 5</p>
    <p class="para" id="8">Paragraph 6</p>

2 个答案:

答案 0 :(得分:13)

这是一个不使用xsl:for-each或任何xsl:variable的选项...

XML输入

<doc>
    <section>
        <para>Paragraph 1</para>
        <para>Paragraph 2</para>
        <para>Paragraph 3</para>
    </section>
    <para>Paragraph 4</para>
    <para>Paragraph 5</para>
    <para>Paragraph 6</para>
</doc>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="doc">
        <book>
            <xsl:apply-templates select="@*|node()"/>
        </book>
    </xsl:template>

    <xsl:template match="para">
        <p class="para" id="{format-number(count(preceding::para)+1,'0000')}">
            <xsl:apply-templates select="@*|node()"/>
        </p>
    </xsl:template>

</xsl:stylesheet>

XML输出

<book>
   <section>
      <p class="para" id="0001">Paragraph 1</p>
      <p class="para" id="0002">Paragraph 2</p>
      <p class="para" id="0003">Paragraph 3</p>
   </section>
   <p class="para" id="0004">Paragraph 4</p>
   <p class="para" id="0005">Paragraph 5</p>
   <p class="para" id="0006">Paragraph 6</p>
</book>

第二个选项是,您可以使用preceding::para ...

来计算xsl:number
<xsl:template match="para">
    <p class="para">
        <xsl:attribute name="id">
            <xsl:number format="0000" level="any"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </p>
</xsl:template>

答案 1 :(得分:2)

这是您需要的xsl

 <xsl:template match="doc">
    <book>
        <section>
            <xsl:for-each select="section/para">
                <xsl:variable name="count" select="position()"/>
                <xsl:variable name ="seq">
                    <xsl:value-of select="format-number($count,'0000')"/>
                </xsl:variable>
                <p class="para" id="{$seq}">
                    <xsl:value-of select="."/>
                </p>
            </xsl:for-each>
        </section>
        <xsl:variable name="secCount" select="count(section/para)"/>
        <xsl:for-each select="para">
            <xsl:variable name="count" select="position()"/>
            <xsl:variable name ="seq">
            <xsl:value-of select="format-number( number($count) + number($secCount),'0000')"/>
        </xsl:variable>
            <p class="para" id="{$seq}">
                <xsl:value-of select="."/>
            </p>
        </xsl:for-each>
    </book>
</xsl:template>