2009-12-02 39 views
0

我用下面与设置修改属性匹配所有<section>秒。 <section>可以出现在文档树的许多不同级别上,始终包含在<chapter> s之内。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迭代认识到的以前匹配部分所属的章。我知道<xsl:variable>实际上一旦设置就是静态的,而<xsl:param>只适用于调用模板。

这是Docbook的,我可以retreive用一节的章号:

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

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

任何想法?谢谢!

回答

1

不知道如果我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>的方法来解决。但是,如果没有看到您的意见和预期产出,这很难说。

+0

谢谢,我正在为此工作。但有一件事:'for-each'之前定义的'sections'变量在'for-each'循环本身中不可用。虽然只是不使用常量,但很容易修复。 – carillonator 2009-12-02 18:31:00

+0

当然是''内部可用的变量。唯一不正确的是'[position() - 1]'谓词,我现在已经解决了这个问题。 – Tomalak 2009-12-03 07:46:49

0

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

+0

迭代计数无关紧要;我需要将章节编号从一个迭代传递到下一个迭代。 – carillonator 2009-12-02 17:16:35