Pregunta

Estoy utilizando la siguiente para que coincida con todos los <section>s con un atributo de revisión establecido. <section>can aparecerá en muchos niveles diferentes de la estructura del documento, siempre contenida dentro <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>

Como dicen los comentarios, tengo que hacer cada iteración for-each consciente del capítulo al que el Anterior emparejado pertenecía. Sé que <xsl:variable>s son en realidad estática, una vez establecido, y que <xsl:param> sólo se aplica a llamar a las plantillas.

Este ser Docbook, puedo retreive número de capítulo de una sección con:

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

pero creo que se puede hacer con puramente XPath.

¿Alguna idea? Gracias!

¿Fue útil?

Solución

No estoy seguro si unterstood sus necesidades al 100%, pero ...

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

Sin embargo, sospecho que todo esto se puede resolver de manera más elegante con un enfoque <xsl:template> / <xsl:apply-templates>. Pero sin ver a su entrada y salida esperada esto es difícil de decir.

Otros consejos

Posición () volverá a su posición dentro de la corriente para-cada iteración. La primera iteración, IIRC, devolverá 0, lo que las pruebas de "posición () = 0" le dirá si usted está en la primera iteración.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top