XSLT 'count' 函数中要计算的元素顺序
Order of elements to becounted in XSLT's 'count' function
我的 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>
正如我在文中所说,我想为 "This is a first note" 音符输出“1”,为 "This is a second note" 音符输出“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”之间有一个“9”和'6',因为它对应的音符在tei:dateline).
如何告诉 'count' 按我想要的顺序计算 <note>
元素,而不考虑它们在 XML 文件中出现的顺序?
我认为您需要一个两步解决方案。第一阶段按照你想要的顺序生成输出,然后在第二阶段进行编号。
在XSLT中进行二次转换有两种方法:将第一阶段的结果放入一个变量中,然后处理变量内容;或者您可以使用两个单独的样式表。哪种方法最好取决于您希望两次传递的紧密耦合程度。
我的 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>
正如我在文中所说,我想为 "This is a first note" 音符输出“1”,为 "This is a second note" 音符输出“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”之间有一个“9”和'6',因为它对应的音符在tei:dateline).
如何告诉 'count' 按我想要的顺序计算 <note>
元素,而不考虑它们在 XML 文件中出现的顺序?
我认为您需要一个两步解决方案。第一阶段按照你想要的顺序生成输出,然后在第二阶段进行编号。
在XSLT中进行二次转换有两种方法:将第一阶段的结果放入一个变量中,然后处理变量内容;或者您可以使用两个单独的样式表。哪种方法最好取决于您希望两次传递的紧密耦合程度。