Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: xslt / xpath question

From: Ben Edgington <usenet@-------.--->
To: NULL
Date: 8/5/2004 10:19:00 AM
Brian Schroeder <spam0504@b...> writes:
> I've got the following situation:
> 
> === a.xml ===
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <n categorie="A">
> 	<n categorie="B">
> 		<n categorie="C"/>
> 	</n>
> 	<n categorie="D"/>
> </n>
> ===
> 
> I want to transform this via xslt to the following
> 
> A
> A->B
> A->B->C
> A->D

Further to your other replies, rather than use the ancestor axis this
kind of thing is generally done more naturally in XSLT with recursion.

This stylesheet (I know it doesn't look more elegant at first sight,
but recursion is much nicer, honestly) produces the output you desire:

<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="n"/>
  </xsl:template>

  <xsl:template match="n">

    <xsl:param name="prefix"/>

    <!-- update the prefix -->
    <xsl:variable name="current">
      <xsl:choose>
        <xsl:when test="$prefix">
          <xsl:value-of select="concat($prefix,'->',@categorie)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@categorie"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- output the new prefix -->
    <xsl:value-of disable-output-escaping="yes" select="$current"/>
    <xsl:text>&#x0a;</xsl:text>  <!-- a newline -->

    <!-- recursively treat my child-nodes -->
    <xsl:apply-templates select="n">
      <xsl:with-param name="prefix">
        <xsl:value-of select="$current"/>
      </xsl:with-param>
    </xsl:apply-templates>

  </xsl:template>

</xsl:stylesheet>

-- 
Ben Edgington
  Mail to the address above is discarded.
  Mail to ben at that address might be read.
http://www.edginet.org/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent