RE: [xsl] flatten a hierachy and change positions

Subject: RE: [xsl] flatten a hierachy and change positions
From: "Andreas L. Delmelle" <a_l.delmelle@xxxxxxxxxx>
Date: Wed, 24 Sep 2003 23:09:27 +0200
> -----Original Message-----
> From: Elke Naraschewski
>
> I looked up the list and books but still don't get a clue which
> method to choose.  I want to flatten a hierachy and change
> positions, the way the following example shows:
>

Not an XSLT-expert, but I'm gonna try anyway. (So to the real experts:
please read the *whole* message before correcting the mistakes in the
suggested solution.)

I have to make some suppositions first, however...

You have:
>
  <root>
> <g1>
> <u1><s></s></u1>
  </g1>   <!-- I presume... -->
> <g2>
> <u2></u2>
> </g2>
  </root>

>
You need:
>
> <u1></u1>
> <s></s>
> <g1></g1>
> <u2></u2>
> <g2></g2>
  <root></root>

Correct? Needs some kind of a recursive template, I think.

If so, something like:

<xsl:template match="/">
  <xsl:call-template name="one">
    <xsl:with-param name="parnode" select="."/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="one">
  <xsl:param name="parnode" select="node()"/>

  <xsl:for-each select="$parnode/node()">
  <xsl:call-template name="one">
    <xsl:with-param name="parnode" select="."/>
  </xsl:call-template>
  <xsl:call-template name="another">
    <xsl:with-param name="parname" select="name(.)"/>
  </xsl:call-template>
  </xsl:for-each>

</xsl:template>

<xsl:template name="another">
  <xsl:param name="parname" select="empty"/>

  <xsl:element name="{$parname}">
    <xsl:value-of select="$parname"/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

may just help you out here.

I'm not 100% certain ( have tested this on the input as completed above, and
the result is

<s>s</s>
<u1>u1</u1>
<g1>g1</g1>
<u2>u2</u2>
<g2>g2</g2>
<root>root</root> <!-- last one left out when you set the parameter in the
first template call
                       to 'node()' instead of '.' -->

Needs some adjustments, as I received error messages complaining about
'Illegal values being used for attribute name' (Xalan-J 2.5.1).

Perhaps one of the gurus can take a look at this and correct this error? I
would certainly appreciate knowing what flaws there are in my logic here...

Greetz,


Andreas Delmelle


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread