模板内自增

Auto increment inside template

我有以下 XML.

<?xml version="1.0" encoding="UTF-8"?>
<uaeccc>
    <nd>
    <AN>-2-0001</AN>
        <h2>Ttle2</h2>
    </nd>
    <nd>
        <h1>Ttle1</h1>
        <h2>Ttle2</h2>
        <h3>Part 1 Contracts</h3>
        <h4>par2 sub contractts</h4>
    </nd>
    <nd>
        <h1>Ttle1</h1>
        <h2>Ttle2 </h2>
        <h3>Part 1 Contracts</h3>
        <h4>Part 2 Sub contracts</h4>
    </nd>
    <nd>
        <h1>Ttle1</h1>
        <h2>Ttle2 </h2>
        <h3>Part 1 Contracts</h3>
        <h4>part 2 sub contracts</h4>
    </nd>
</uaeccc>

我在这里尝试递增一个数字,但它只在该部分递增。

下面是我的 XSLT。

<xsl:template match="uaeccc">
    <xsl:for-each select="nd">
        <xsl:apply-templates select="."/>
    </xsl:for-each>
</xsl:template>

<xsl:template name="section" match="nd">
    <xsl:apply-templates select="./node()[1][self::page]" mode="first"/>
    <!-- Variables-->
    <xsl:variable name="getChap">
        <xsl:value-of select="substring-before(substring-after((//AN)[1],'-'),'-')"/>
    </xsl:variable>
    <xsl:variable name="count">
        <xsl:number level="any"/>
    </xsl:variable>
    <xsl:variable name="classname">
        <!--Get name attribute of current node -->
        <xsl:value-of select="concat('section-sect','1')"/>
    </xsl:variable>

    <xsl:variable name="Chn">
        <xsl:value-of select="format-number($getChap,'00')"/>
    </xsl:variable>
    <xsl:variable name="chapternumber">
        <xsl:value-of select="$Chn"/>
    </xsl:variable>
    <xsl:variable name="sectnum">
        <xsl:number level="any" count="section" format="1"/>
    </xsl:variable>
    <!--Create a string variable by concat string method  -->
    <xsl:variable name="sectionname">
        <xsl:value-of select="concat('CH_',$chapternumber,'-SEC-', $count)"/>
    </xsl:variable>
    <!-- Template Content  -->
    <div class="{$classname}">
        <a name="{$sectionname}"> </a>
        <div class="section-title">
            <xsl:apply-templates select="h3"/>
        </div>
        <xsl:if test="./h4">
            <div class="{$classname}">
                <a name="{$sectionname}"> </a>
                <div class="section-title2">
                    <xsl:apply-templates select="h4"/>
                </div>
            </div>
        </xsl:if>
        <xsl:apply-templates select="child::node()[not(self::h4| self::h3)]"/>
    </div>
</xsl:template>

<xsl:template match="AN"/>

<xsl:template match="h1"/>

<xsl:template match="h2"/>

我当前的输出是

  <div class="section-sect1">
        <a name="CH_02-SEC-1"></a>
        <div class="section-title"></div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-2"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-2"></a>
            <div class="section-title2">
                <h4>par2 sub contractts</h4>
            </div>
        </div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-3"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-3"></a>
            <div class="section-title2">
                <h4>Part 2 Sub contracts</h4>
            </div>
        </div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-4"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-4"></a>
            <div class="section-title2">
                <h4>part 2 sub contracts</h4>
            </div>
        </div>
    </div>

预期输出为

 <div class="section-sect1">
        <a name="CH_02-SEC-1"></a>
        <div class="section-title"></div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-2"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-3"></a>
            <div class="section-title2">
                <h4>par2 sub contractts</h4>
            </div>
        </div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-4"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-5"></a>
            <div class="section-title2">
                <h4>Part 2 Sub contracts</h4>
            </div>
        </div>
    </div>
    <div class="section-sect1">
        <a name="CH_02-SEC-6"></a>
        <div class="section-title">
            <h3>Part 1 Contracts</h3>
        </div>
        <div class="section-sect1">
            <a name="CH_02-SEC-7"></a>
            <div class="section-title2">
                <h4>part 2 sub contracts</h4>
            </div>
        </div>
    </div>

这里我想在我的 XSLT 中每次有 $classnamecalled.But 时增加 SEC- 之后的数字,增加发生但在小节的情况下也是相同的。

请告诉我如何修复它。

这是一个Working Demo

我无法理解你的代码。 FWIW,将以下样式表应用于您的示例输入时,将产生所需的输出(将结果包装在单个根元素中除外):

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/uaeccc">
    <root>
        <xsl:apply-templates/>
    </root>
</xsl:template>

<xsl:template match="nd">
    <div class="section-sect1">
        <xsl:variable name="sectionnum">
            <xsl:number count="nd|h4" level="any"/>
        </xsl:variable>
        <a name="CH_02-SEC-{$sectionnum}"/>
        <div class="section-title">
            <xsl:copy-of select="h3"/>
        </div>
        <xsl:apply-templates select="h4"/>
    </div>
</xsl:template>

<xsl:template match="h4">
    <div class="section-sect1">
        <xsl:variable name="sectionnum">
            <xsl:number count="nd|h4" level="any"/>
        </xsl:variable>
        <a name="CH_02-SEC-{$sectionnum}"/>
        <div class="section-title2">
            <xsl:copy-of select="."/>
        </div>
    </div>
</xsl:template>

</xsl:stylesheet>