0

我正在使用以下内容将所有<section>s 与修订属性集匹配。<section>可以出现在文档树的许多不同级别,总是包含在<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>

正如评论所说,我需要让每次for-each迭代都知道前一个匹配部分所属的章节。我知道<xsl:variable>s 设置后实际上是静态的,并且<xsl:param>仅适用于调用模板。

这是 Docbook,我可以通过以下方式检索章节的章节编号:

<xsl:apply-templates select="ancestor::chapter[1]" mode="label.markup" />

但我认为它可以用纯粹的 XPath 来完成。

有任何想法吗?谢谢!

4

2 回答 2

1

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

<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>

但是,我怀疑这整个事情可以用<xsl:template>/<xsl:apply-templates>方法更优雅地解决。但是如果没有看到您的输入和预期输出,这很难说。

于 2009-12-02T17:52:29.913 回答
0

position() 将返回您在当前 for-each 迭代中的位置。第一次迭代 IIRC 将返回 0,因此对“position()=0”的测试将告诉您您是否处于第一次迭代中。

于 2009-12-02T17:13:50.817 回答