Re: [xsl] Newbie Question: Convert "flat" hierarchy to nested hierarchy

Subject: Re: [xsl] Newbie Question: Convert "flat" hierarchy to nested hierarchy
From: Mikhail Edoshin <m.edoshin@xxxxxxx>
Date: Sun, 15 Oct 2006 14:28:42 +0400
Here's a quick and dirty method to convert your flat sample to nested
hierarchy. I didn't copy all the supposed markup here, just created
the hierarchy. My source file was:

<?xml version='1.0' encoding='utf-8'?>
   <flat lvl="1"> {markupA} </flat>
   <flat lvl="2"> {markupB} </flat>
   <flat lvl="2"> {markupC} </flat>
   <flat lvl="1"> {markupD} </flat>
   <flat lvl="2"> {markupE} </flat>
   <flat lvl="2"> {markupF} </flat>
   <flat lvl="3"> {markupG} </flat>
   <flat lvl="4"> {markupH} </flat>
   <flat lvl="4"> {markupI} </flat>
   <flat lvl="3"> {markupJ} </flat>
   <flat lvl="3"> {markupK} </flat>
   <flat lvl="2"> {markupL} </flat>
   <flat lvl="1"> {markupM} </flat>

The XSLT is:

<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='

<xsl:output method='xml' version='1.0' encoding='utf-8' indent='yes'/>

<xsl:template match="/">
  <xsl:call-template name="nest">
    <xsl:with-param name="flat" select="text/flat[1]" />

<xsl:template name="nest">
  <xsl:param name="flat" />
  <xsl:element name="nest">
    <xsl:value-of select="$flat" />
    <xsl:if test="$flat/following::flat[1]/@lvl &gt; $flat/@lvl">
      <xsl:call-template name="nest">
        <xsl:with-param name="flat" select="$flat/following::flat
[1]" />
  <xsl:variable name="next" select="$flat/following::flat[@lvl &lt;=
$flat/@lvl]" />
  <xsl:if test="$next[1]/@lvl = $flat/@lvl">
    <xsl:call-template name="nest">
      <xsl:with-param name="flat" select="$next[1]" />


Kind regards,
Mikhail Edoshin

On Oct 14, 2006, at 8:28 PM, Watch-O-Matic wrote:

Michael Kay wrote:

You'll find a paper that tackles this problem (using XSLT 2.0) at

The reverse transformation is much easier: you just process all the
elements in document order (//nest) and for each one compute its
level number as count(ancestor::*).

Thank you for your prompt answer.

Since I am a newbie at this, it'll take me a while to understand your
paper (and XSLT in general.) But I assume from your reply that the
transformation from 'flat' to 'nested' (which is of most interest to
me at the moment) is doable with XSLT 2.0.

Of course, would love to see an XSLT 2.0 transformation specific to
the 'flat' to 'nested' example, if someone here has the interest --
and a moment to spare. That will greatly help. It also seems "generic"
enough that the transformation may be useful to someone else in the

Again, much thanks.


-- - Same, same, but different

Current Thread