我使用下面与设置修订属性匹配所有<section>s。 <section>can出现在许多不同的层面文档树的,总是包含<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,我可以retreive一个部分的章节号以:

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

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

任何想法?谢谢!

有帮助吗?

解决方案

不知道如果我unterstood您的要求为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>

不过,我怀疑这件事是可以解决的更优雅,配有<xsl:template> / <xsl:apply-templates>方法。但是没有看到你的输入和预期的输出,这是很难说的。

其他提示

位置()将当前换每次迭代内返回的位置。在第一次迭代,IIRC,将返回0,所以测试为“位置()= 0”将告诉您是否是在第一次迭代。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top