首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用xsl:number检索nth元素

用xsl:number检索nth元素
EN

Stack Overflow用户
提问于 2017-01-08 17:27:10
回答 1查看 203关注 0票数 0

在我的XML文档中,我有note调用和endnote,显然不在文档中相同的位置。我可以肯定,<notecalls>元素和<endnote>元素的数量完全相同。对于XSL,我想检索相应的<endnote>的内容(即:第一个<notecall>元素与第一个<endnote>元素一起使用)来创建一个由数字和尾注的内容组成的新元素。

为此,我使用了xsl:number函数,如下所示;但是检索到的注释的内容始终是第一个尾注元素,尽管在输出文件中重编号是正确的。我在这里错过了什么?

下面是我的XML结构看起来是什么样子的:

代码语言:javascript
运行
复制
<main_text>Some text<notecall>1</notecall> some other text </main_text>
<main_text>More and more long text<notecall>2</notecall> and more even</main_text>
<main_text>And some more again <notecall>3</notecall> etc…<main_text>

<endnote>The content of the first endnote</endnote>
<endnote>The content of the second one</endnote>
<endnote>The content of the third one</endnote>

以及XSL文件的相关部分:

代码语言:javascript
运行
复制
<xsl:template match="notecall">
    <xsl:variable name="posit">
        <xsl:number level="any"/>
    </xsl:variable>
    <seg>
        <xsl:value-of select="$posit"/>
        <note><xsl:value-of select="(//endnote)[$posit]"/></note>
    </seg>
</xsl:template>

我想要:

代码语言:javascript
运行
复制
<p>Some text<seg>1<note>The content of the first endnote</note></seg> some other text</main_text>
<p>More and more long text<seg>2<note>The content of the second one</note></seg> and more even</main_text>
<p>And some more again <seg>3<note>The content of the third one</note></seg>  etc…</main_text>

但我得到的是:

代码语言:javascript
运行
复制
<p>Some text<seg>1<note>The content of the first endnote</note></seg> some other text</main_text>
<p>More and more long text<seg>2<note>The content of the first endnote</note></seg> and more even</main_text>
<p>And some more again <seg>3<note>The content of the first endnote</note></seg>  etc…</main_text>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-08 17:50:14

如果使用XSLT2.0或更高版本,则更改

代码语言:javascript
运行
复制
<xsl:variable name="posit">
    <xsl:number level="any"/>
</xsl:variable>

代码语言:javascript
运行
复制
<xsl:variable name="posit" as="xs:integer">
    <xsl:number level="any"/>
</xsl:variable>

否则,将<xsl:value-of select="(//endnote)[$posit]"/>更改为<xsl:value-of select="(//endnote)[position() = $posit]"/>,因为您的变量是结果树片段,而不是数字,因此要使用它作为需要显式比较的位置,或者将其转换为与<xsl:value-of select="(//endnote)[number($posit)]"/>对应的数字。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41535567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档