8

输入 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>
4

2 回答 2

15

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

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>
于 2012-11-26T08:22:18.753 回答
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>
于 2012-11-26T07:45:27.440 回答