XML technologies / XSLT / XSLT Kochbuch / Selecting and traversing / Performing an in-order traversal

(Excerpt from "XSLT Kochbuch" by Sal Mangano)

Problem

You have an XML document or fragment representing an expression that must be processed in-order.

Solution

An in-order traversal is usually applied to a binary tree. The general form of the algorithm in this case is as follows:

<xsl:template match="node( )">
  <!--Verarbeiten des linken Teilbaums -->
  <xsl:apply-templates select="*[1]"/>
  <!-- Irgendetwas mit dem aktuellen Knoten tun -->
  <!--Verarbeiten des rechten Teilbaums -->
  <xsl:apply-templates select="*[2]"/>
</xsl:template>

However, the following algorithm extends in-order traversal to n-ary trees:

<xsl:template match="node( )">
  <xsl:variable name="current-node" select="."/>
  <!--Verarbeiten des linken Teilbaums -->
  <xsl:apply-templates select="*[1]"/>
  <!-- Irgendetwas mit $current-node tun -->
  <!-- Rekursiv auf die mittleren Kinder anwenden -->
  <xsl:for-each select="*[position( ) &gt; 1 and position( ) &lt; last( )">
    <!-- Verarbeiten des "linken" Teilbaums -->
    <xsl:apply-templates select="."/>
    <!--Irgendetwas mit $current-node tun -->
  </xsl:for-each>
  <!--Verarbeiten des rechten Teilbaums -->
  <xsl:apply-templates select="*[last( )]"/>
</xsl:template>

The principle behind this algorithm becomes clearer in the following figure, which shows the binary equivalent of an n-ary tree. The generalized n-ary in-order traversal produces the same result as a binary in-order traversal of the equivalent binary tree.

N-ary equivalent of a binary tree

Figure: N-ary equivalent of a binary tree.

Discussion

This form of traversal has much more limited applicability than the other traversal examples in this chapter. One notable application, illustrated by the following two examples, is as part of a stylesheet that converts MathML markup into C- or Java-style infix expressions.

Example: Input MathML fragment.

<apply>
  <eq/>
  <apply>
    <plus/>
    <apply>
      <minus/>
      <ci>y</ci>
      <cn>2</cn>
    </apply>
    <apply>
      <times/>
      <cn>4</cn>
      <apply>
        <plus/>
        <ci>x</ci>
        <cn>1</cn>
      </apply>
    </apply>
    <cn>8</cn>
  </apply>
  <cn>0</cn>
</apply>

Example: In-order traversal of the MathML fragment to generate a C expression.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:C="http://www.ora.com/XSLTCookbook/nampespaces/C">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <!-- Tabelle zum Umwandeln der Namen der MathML-Operation in C-Operatoren -->
  <C:operator mathML="plus" c="+" precedence="2"/>
  <C:operator mathML="minus" c="-" precedence="2"/>
  <C:operator mathML="times" c="*" precedence="3"/>
  <C:operator mathML="div" c="/" precedence="3"/>
  <C:operator mathML="mod" c="%" precedence="3"/>
  <C:operator mathML="eq" c="= =" precedence="1"/>
  <!-- Laden der Tabelle mit den Operationskonvertierungen in eine Variable -->
  <xsl:variable name="ops" select="document('')/*/C:operator"/>
  <xsl:template match="apply">
    <xsl:param name="parent-precedence" select="0"/>
    <!-- Abbilden der mathML-Operation auf den Operatornamen und die Präzedenz -->
    <xsl:variable name="mathML-opName" select="local-name(*[1])"/>
    <xsl:variable name="c-opName" select="$ops[@mathML=$mathML-opName]/@c"/>
    <xsl:variable name="c-opPrecedence" select="$ops[@mathML=$mathML-opName]/@precedence"/>
    <!-- Es sind Klammern erforderlich, wenn die Präzedenz des äußeren Ausdrucks größer ist als die des aktuellen Teilausdrucks -->
    <xsl:if test="$parent-precedence &gt; $c-opPrecedence">
      <xsl:text>(</xsl:text>
    </xsl:if>
    <!-- Rekursive Verarbeitung des linken Teilbaums, der sich an Position 2 im MathML-apply-Element befindet-->
    <xsl:apply-templates select="*[2]">
      <xsl:with-param name="parent-precedence" select="$c-opPrecedence"/>
    </xsl:apply-templates>
    <!-- Verarbeitung des aktuellen Knotens (d.h. des Operators an Position 1 im MathML-apply-Element) -->
    <xsl:value-of select="concat(' ',$c-opName,' ')"/>
    <!-- Rekursive Verarbeitung der mittleren Kinder -->
    <xsl:for-each select="*[position() &gt; 2 and position() &lt; last()]">
      <xsl:apply-templates select=".">
        <xsl:with-param name="parent-precedence" select="$c-opPrecedence"/>
      </xsl:apply-templates>
      <xsl:value-of select="concat(' ',$c-opName,' ')"/>
    </xsl:for-each>
    <!-- Rekursive Verarbeitung des rechten Teilbaums-->
    <xsl:apply-templates select="*[last()]">
      <xsl:with-param name="parent-precedence" select="$c-opPrecedence"/>
    </xsl:apply-templates>
    <!-- Es sind Klammern erforderlich, wenn die Präzedenz des äußeren Ausdrucks größer ist als die des aktuellen Teilausdrucks -->
    <xsl:if test="$parent-precedence &gt; $c-opPrecedence">
      <xsl:text>)</xsl:text>
    </xsl:if>
  </xsl:template>
  <xsl:template match="ci|cn">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Example: output.

y - 2 + 4 * (x + 1) + 8 = = 0

Clearly, this stylesheet is not a full MathML-to-C translator. However, Querying XML discusses this problem in greater depth.

  

 

 

 

back to top