XSLT - recursively works out

I understand how to process this document (below) using XSLT from the user's outer element to the innermost one. But I was wondering if:

  • If it is possible to work with the deepest element.
  • That it would be similar to my example.
<?xml version="1.0" encoding="utf-8" ?>
<container>
  <person name="Larry">
    <person name="Moe">
      <person name="Curly">
        <person name="Shemp">

        </person>
      </person>
    </person>
  </person>
</container>
+3
source share
3 answers

Here is the most general way to do "reverse recursion" , which is independent of this particular problem and can be used in a variety of problems.

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:call-template name="backwardsRecursion">
        <xsl:with-param name="pList" select="//person"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template name="backwardsRecursion">
      <xsl:param name="pList"/>

      <xsl:if test="$pList">
        <xsl:apply-templates select="$pList[last()]"/>

        <xsl:call-template name="backwardsRecursion">
          <xsl:with-param name="pList" select=
           "$pList[position() &lt; last()]"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:template>

    <xsl:template match="person">
      <xsl:value-of select="concat(@name,'&#xA;')"/>
    </xsl:template>
</xsl:stylesheet>

when applied to the original XML document, it produces the desired result :

Shemp
Curly
Moe
Larry

, "backwardsRecursion", . , , , .

, , .

+2

:: .

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <xsl:apply-templates select="//person[not(person)]"/>
    </xsl:template>
  <xsl:template match="person">
    <xsl:value-of select="@name"/> -&gt; <xsl:apply-templates select="parent::person"/>
  </xsl:template>
</xsl:stylesheet>

, .

Shemp → Curly → Moe → Larry

+11

, teun, "person" :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:apply-templates select="//person[not(person)]"/>
    </xsl:template>

    <xsl:template match="person" name="tPerson">
       <xsl:value-of select="concat(@name,'&#xA;')"/>

       <xsl:apply-templates select=
          "ancestor::person[1]"/>
    </xsl:template>
</xsl:stylesheet>

XML- :

Shemp

Larry

It’s good to know that many problems of this type do not require recursion at all! This conversion gives exactly the same result and includes only iteration :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/">
       <xsl:for-each select="//person[not(person)]">
          <xsl:for-each select="ancestor::person | .">
            <xsl:sort select="position()" order="descending"/>
                <xsl:value-of select="concat(@name,'&#xA;')"/>
          </xsl:for-each>
       </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Notice the sorting that provides the reverse processing order.

+1
source

Source: https://habr.com/ru/post/1703036/


All Articles