XSLT:将每个匹配项的值传递到下一个匹配项
我使用以下内容将所有 与修订属性集进行匹配。
可以出现在文档树的许多不同级别,始终包含在
内。
<xsl:for-each select="//section[@revision]">
<!-- Do one thing if this is the first section
matched in this chapter -->
<!-- Do something else if this section is in the same
chapter as the last section matched -->
</xsl:for-each>
正如评论所说,我需要让每个 for-each
迭代知道 previous 匹配部分所属的章节。我知道
一旦设置实际上是静态的,并且
仅适用于调用模板。
这是 Docbook,我可以使用以下命令检索章节的章节号:
<xsl:apply-templates select="ancestor::chapter[1]" mode="label.markup" />
但我认为这可以通过纯粹的 XPath 来完成。
有什么想法吗?谢谢!
I'm using the following to match all <section>
s with a revision attribute set. <section>
can appear at many different levels of the document tree, always contained within <chapter>
s.
<xsl:for-each select="//section[@revision]">
<!-- Do one thing if this is the first section
matched in this chapter -->
<!-- Do something else if this section is in the same
chapter as the last section matched -->
</xsl:for-each>
As the comments say, I need to make each for-each
iteration aware of the chapter to which the previous matched section belonged. I know that <xsl:variable>
s are actually static once set, and that <xsl:param>
only applies to calling templates.
This being Docbook, I can retreive a section's chapter number with:
<xsl:apply-templates select="ancestor::chapter[1]" mode="label.markup" />
but I think it can be done with purely XPath.
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我是否 100% 理解您的要求,但是……
但是,我怀疑这整件事可以通过
/更优雅地解决;
方法。但如果没有看到您的输入和预期输出,这很难说。Not sure if I unterstood your requirements 100%, but…
However, I suspect this whole thing can be solved more elegantly with a
<xsl:template>
/<xsl:apply-templates>
approach. But without seeing your input and expected output this is hard to say.position() 将返回您在当前 for-each 迭代中的位置。第一次迭代 IIRC 将返回 0,因此测试“position()=0”将告诉您是否处于第一次迭代中。
position() will return your position within the current for-each iteration. The first iteration, IIRC, will return 0, so testing for "position()=0" will tell you if you are in the first iteration.