Domanda

Sto usando il seguente per abbinare tutti i <section>s con un attributo di revisione set. <section>can apparire a molti diversi livelli della struttura del documento, sempre contenuta all'interno <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>

Come dicono i commenti, ho bisogno di fare ogni iterazione for-each consapevoli del capitolo a cui il precedente abbinato apparteneva. So che sono in realtà <xsl:variable>s statici una volta insieme, e che <xsl:param> si applica soltanto a chiamare i modelli.

Questo essere Docbook, posso prelevare numero del capitolo di una sezione con:

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

ma penso che può essere fatto con puramente XPath.

Tutte le idee? Grazie!

È stato utile?

Soluzione

Non so se mi unterstood vostre esigenze 100%, ma ...

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

Tuttavia, ho il sospetto tutta questa cosa può essere risolto più elegantemente con un approccio <xsl:template> / <xsl:apply-templates>. Ma senza vedere il vostro input e output atteso questo è difficile da dire.

Altri suggerimenti

posizione () restituirà la vostra posizione all'interno della corrente per-ogni iterazione. La prima iterazione, IIRC, tornerà 0, quindi il test per "position () = 0" vi dirà se siete nella prima iterazione.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top