XSLT的'count'函数中的元素顺序

问题描述 投票:0回答:1

我在我的XSLT文件中有一条指令,它对节点进行计数,并在附加注释的单词旁边输出一个超链接的上标数字,以及注释本身(带编号的超链接):

<xsl:template match="//tei:body//tei:note">
    <a>
        <xsl:attribute name="name">
            <xsl:text>footnoteref</xsl:text>
            <xsl:number level="any" count="//tei:body//tei:note" format="1"/>
        </xsl:attribute>
        <xsl:attribute name="href">
            <xsl:text>#footnote</xsl:text>
            <xsl:number level="any" count="//tei:body//tei:note" format="1"/>
        </xsl:attribute>
        <sup>
            <xsl:number level="any" count="//tei:body//tei:note" format="1"/>
        </sup>
    </a>
</xsl:template>

<xsl:template match="tei:note" mode="footnote">
    <br>
        <a>
            <xsl:attribute name="name">
                <xsl:text>footnote</xsl:text>
                <xsl:number level="any" count="//tei:body//tei:note" format="1"/>
            </xsl:attribute>
            <xsl:attribute name="href">
                <xsl:text>#footnoteref</xsl:text>
                <xsl:number level="any" count="//tei:body//tei:note" format="1"/>
            </xsl:attribute>
            <xsl:number level="any" count="//tei:body//tei:note" format="1"/>.

        </a>
        <xsl:text> </xsl:text>
        <xsl:apply-templates/>
        </i>
    </br>
</xsl:template>

[...]

<xsl:apply-templates select="//tei:body//tei:note" mode="footnote"/>

一切正常。不过,我想控制<note>元素的计数顺序。例如,如果这是我的.xml:

<body>
    <div>
        <p>Bla<note>This is a second note</note> blabla</p>
    </div>
    <div>
        <p>Bla2<note>This is a first note</note> bla bla...</p>
    </div>
</body>

正如我在文中所说的那样,我想为“这是第一个音符”音符输出'1',为“这是第二个音符”音符输出'2',即使在音符中也是如此。 xml文件以不同的顺序排列。

在输出方面,我已经在我的XSLT文件开头按照我想要的顺序输出文本了:

<!-- Order of execution -->
<xsl:template match="tei:text/tei:body/tei:div">
    <xsl:apply-templates select="tei:opener/tei:dateline"/>
    <xsl:apply-templates select="tei:opener/tei:salute"/>
    <xsl:apply-templates select="tei:p [not(tei:stamp)] [not(tei:address)] [not(tei:postscript)]"/>
    <xsl:apply-templates select="tei:closer/tei:salute"/>
    <xsl:apply-templates select="tei:closer/tei:signed"/>
    <xsl:apply-templates select="tei:closer/tei:dateline"/>
    <xsl:apply-templates select="tei:postscript"/>
    <xsl:apply-templates select="tei:stamp"/>
</xsl:template>

结果是文本按我想要的顺序排列,例如,音符编号从文档的顶部开始,从1到8,在'5'和'6'之间有一个'9' ,因为它的相应注释在tei:dateline中。

我怎么能告诉'count'按照我想要的顺序计算<note>元素,不管它们出现在XML文件中的顺序如何?

xml xslt xpath xslt-2.0 xpath-2.0
1个回答
2
投票

我认为你需要一个双程解决方案。在第一阶段按您想要的顺序生成输出,然后在第二阶段进行编号。

在XSLT中有两种方法进行两遍转换:您可以将第一阶段的结果放入变量,然后处理变量内容;或者您可以使用两个单独的样式表。哪种方法最好取决于您希望两次传球的紧密耦合程度。

© www.soinside.com 2019 - 2024. All rights reserved.