XML technologies / XSLT / XSLT Kochbuch / Selecting and traversing / Selecting nodes by context

(Excerpt from "XSLT Kochbuch" by Sal Mangano)

Problem

You want to select nodes bounded by preceding and following nodes.

Solution

XSLT 1.0

There are several ways to solve this in XSLT 1.0. The easiest to understand calculates the positions of the nodes to select at each step. For the following unstructured document, you could use this technique to select paragraphs between headings:

<doc>
  <heading>Struktur, Ich brauche keine Struktur</heading>
  <para>1.1</para>
  <para>1.2</para>
  <para>1.3</para>
  <para>1.4</para>
  <heading>Bekenntnisse eines Flacherdlings</heading>
  <para>2.1</para>
  <para>2.2</para>
  <para>2.3</para>
  <heading>Flache Hierarchien retten Bäume!</heading>
  <para>3.1</para>
  <para>3.2</para>
</doc>

<xsl:template match="/doc">
  <xsl:copy>
    <!-- Zuerst werden die einklammernden Elemente ausgewählt -->
    <xsl:apply-templates select="heading"/>
  </xsl:copy>
</xsl:template>
<!-- Filtern der einklammernden Elemente -->
<xsl:template match="heading">
  <!-- Es wird berechnet, wie viele der gewünschten Elemente (para) dieser Überschrift folgen -->
  <xsl:variable name="numFollowingPara" select="count(following-sibling::para)"/>
  <!-- Es wird berechnet, wie viele der gewünschten Elemente (para) der nächsten Überschrift folgen, und dieser Wert wird vom vorhergehenden Zähler abgezogen, um die Position des letzten Absatzes (para) in dieser Gruppe zu erhalten -->
  <xsl:variable name="lastParaInHeading" select="$numFollowingPara - count(following-sibling::heading[1]/following-sibling::para)"/>
  <!-- Sie können nun die gewünschten Elemente anhand ihrer Position relativ zur aktuellen Überschrift auswählen -->
  <xsl:apply-templates select="following-sibling::para[position( ) &lt;= $lastParaInHeading]"/>
</xsl:template>

XSLT 2.0

This problem is ideally suited to the instruction for-each-group. In particular, the attribute group-starting-with would be used here:

<xsl:template match="/doc">
  <xsl:copy>
    <xsl:for-each-group select="*" group-starting-with="heading">
      <!--Auswählen der para-Elemente in der Gruppe, die durch eine Überschrift eingeklammert sind -->
      <xsl:apply-templates select="current-group( )[self::para]"/>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

Discussion

Selecting nodes by their position relative to other nodes is common in document-oriented XML transformations where structure is implicit rather than encoded in the document hierarchy. Of course, if each group of a heading and its paragraphs were enclosed in a parent element, such as a section element, the problem would be trivial. This is a classic trade-off between convenience for document creators and convenience for those transforming documents. The introduction of for-each-group in XSLT 2.0 removes the need for this trade-off, making unstructured documents much easier to handle.

See also

The recipe Deepening an XML hierarchy shows applications of this technique to transform implicitly structured documents into explicitly structured ones. It also presents other approaches to the problem in XSLT 1.0 and 2.0.

  

 

 

 

back to top