Technologies XML / XSLT / XSLT Kochbuch / Sélection et parcours / Effectuer un parcours en postordre

(Extrait de « XSLT Kochbuch" de Sal Mangano)

Problème

Vous souhaitez traiter récursivement les enfants d’un élément avant de traiter l’élément lui-même.

Solution

Les solutions de cette recette suivent la forme générale suivante :

<xsl:template match="node( )">
  <!--Verarbeiten der Kinder -->
  <xsl:apply-templates/>
  <!-- Irgendetwas mit dem aktuellen Knoten tun -->
</xsl:template>

Discussion

Le terme postordre (parcours postfixe) est un terme informatique désignant un parcours d’arbre dans lequel on visite récursivement les enfants de la racine en postordre avant de visiter la racine elle-même. Cet algorithme produit une feuille de style qui traite les nœuds feuilles les plus éloignés, puis remonte vers la racine du document.

Vous pouvez appliquer un parcours en postordre à l’organigramme (orgchart.xml) pour expliquer qui rend compte à qui, en commençant par le bas, comme le montre l’exemple suivant. L’exemple qui suit présente la sortie.

Exemple : feuille de style.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="/employee" priority="10">
    <xsl:apply-templates/>
    <xsl:value-of select="@name"/><xsl:text> ist der Chef des Unternehmens. </xsl:text>
    <xsl:call-template name="reportsTo"/>
    <xsl:call-template name="HimHer"/> <xsl:text>. </xsl:text>
    <xsl:text>&#xa;&#xa;</xsl:text>
  </xsl:template>
  <xsl:template match="employee[employee]">
    <xsl:apply-templates/>
    <xsl:value-of select="@name"/><xsl:text> ist ein Manager. </xsl:text>
    <xsl:call-template name="reportsTo"/>
    <xsl:call-template name="HimHer"/> <xsl:text>. </xsl:text>
    <xsl:text>&#xa;&#xa;</xsl:text>
  </xsl:template>
  <xsl:template match="employee">
    <xsl:text>Niemand berichtet </xsl:text>
    <xsl:value-of select="@name"/><xsl:text>. &#xa;</xsl:text>
  </xsl:template>
  <xsl:template name="HimHer">
    <xsl:choose>
      <xsl:when test="@sex = 'male' ">
        <xsl:text>ihm</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>ihr</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template name="reportsTo">
    <xsl:for-each select="*">
      <xsl:choose>
        <xsl:when test="position() &lt; last() 1 and last() &gt; 2">
          <xsl:value-of select="@name"/><xsl:text>, </xsl:text>
        </xsl:when>
        <xsl:when test="position() = last() - 1 and last() &gt; 1">
          <xsl:value-of select="@name"/><xsl:text> and </xsl:text>
        </xsl:when>
        <xsl:when test="position() = last() and last() = 1">
          <xsl:value-of select="@name"/><xsl:text> berichtet </xsl:text>
        </xsl:when>
        <xsl:when test="position() = last()">
          <xsl:value-of select="@name"/><xsl:text> berichten </xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@name"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Exemple : sortie.

Niemand berichtet Phill McKraken. Niemand berichtet Betsy Ross. Ima Little ist ein Manager. Betsy Ross berichtet ihr.

Nancy Pratt ist ein Manager. Phill McKraken und Ima Little berichten ihr.

Niemand berichtet Walter H. Potter. Niemand berichtet Craig F. Frye. Niemand berichtet Hardy Hamburg. Niemand berichtet Rich Shaker. Wendy B.K. McDonald ist ein Manager. Craig F. Frye, Hardy Hamburg und Rich Shaker berichten ihr. Jane Doe ist ein Manager. Walter H. Potter und Wendy B.K. McDonald berichten ihr.

Niemand berichtet Allen Bran. Niemand berichtet Frank N. Berry. Niemand berichtet Jack Apple. Cindy Post-Kellog ist ein Manager. Allen Bran, Frank N. Berry und Jack Apple berichten ihr.

Niemand berichtet R.P. McMurphy. Jack Nicklaus ist ein Manager. R.P. McMurphy berichtet ihm.

Niemand berichtet Forrest Gump. Niemand berichtet Andrew Beckett. Tom Hanks ist ein Manager. Forrest Gump und Andrew Beckett berichten ihm.

Niemand berichtet Helen Prejean. Susan Sarandon ist ein Manager. Helen Prejean berichtet ihr.

Oscar A. Winner ist ein Manager. Jack Nickolas, Tom Hanks und Susan Sarandon berichten ihm.

Mike Rosenbaum ist ein Manager. Cindy Post-Kellog und Oscar A. Winner berichten ihm.

Jil Michel ist der Chef des Unternehmens. Nancy Pratt, Jane Doe und Mike Rosenbaum berichten ihr.

  

 

 

 

haut de page