XSLT:将每个匹配项的值传递到下一个匹配项

发布于 2024-08-13 09:06:03 字数 868 浏览 6 评论 0原文

我使用以下内容将所有

与修订属性集进行匹配。
可以出现在文档树的许多不同级别,始终包含在内。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-08-20 09:06:03

不确定我是否 100% 理解您的要求,但是……

<xsl:variable name="sections" select="//section[@revision]" />

<xsl:for-each select="$sections">
  <xsl:variable name="ThisPos" select="position()" />
  <xsl:variable name="PrevMatchedSection" select="$sections[$ThisPos - 1]" />
  <xsl:choose>
    <xsl:when test="
      not($PrevMatchedSection)
      or
      generate-id($PrevMatchedSection/ancestor::chapter[1])
      !=
      generate-id(ancestor::chapter[1])
    ">
      <!-- Do one thing if this is the first section 
           matched in this chapter -->
    </xsl:when>
    <xsl:otherwise>
      <!-- Do something else if this section is in the same 
           chapter as the last section matched -->
    </xsl:otherwise>
  </xsl:choose>  
</xsl:for-each>

但是,我怀疑这整件事可以通过 / 更优雅地解决; 方法。但如果没有看到您的输入和预期输出,这很难说。

Not sure if I unterstood your requirements 100%, but…

<xsl:variable name="sections" select="//section[@revision]" />

<xsl:for-each select="$sections">
  <xsl:variable name="ThisPos" select="position()" />
  <xsl:variable name="PrevMatchedSection" select="$sections[$ThisPos - 1]" />
  <xsl:choose>
    <xsl:when test="
      not($PrevMatchedSection)
      or
      generate-id($PrevMatchedSection/ancestor::chapter[1])
      !=
      generate-id(ancestor::chapter[1])
    ">
      <!-- Do one thing if this is the first section 
           matched in this chapter -->
    </xsl:when>
    <xsl:otherwise>
      <!-- Do something else if this section is in the same 
           chapter as the last section matched -->
    </xsl:otherwise>
  </xsl:choose>  
</xsl:for-each>

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.

深海蓝天 2024-08-20 09:06:03

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文