2009-12-02 93 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

相關問題