Re: [xsl] putting all the elements with a given name one level up

Subject: Re: [xsl] putting all the elements with a given name one level up
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 28 Jun 2004 19:54:22 -0400
At 2004-06-29 00:07 +0100, Pedro Castro wrote:
I have this xml:
...
And I want to transform it to this one:
(The difference is that all the ccc elements, and it's children are removed
from the location where they are and putted below the root element)

This can be done in a straightforward fashion by modifying the identity transformation.


I hope the example below helps. I've turned on indentation for illustration that may not apply in your case ... remember to remove the template at the end of the example that eats white-space-only text nodes.

.............. Ken

T:\ftemp>type pedro.xml
<root>
            <aaa>
                        <bbb/>
                        <ccc/>
                        <bbb/>
            </aaa>
            <aaa>
                        <ccc>
                                   <abc>test</abc>
                        </ccc>
                        <bbb/>
                        <bbb/>
            </aaa>
</root>

T:\ftemp>type pedro.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="root">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    <xsl:copy-of select="//ccc"/><!--collect all ccc elements-->
  </xsl:copy>
</xsl:template>

<xsl:template match="ccc"/><!--ignore ccc elements when found-->

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<!--this cleans up the output when using indent="yes"-->
<xsl:template match="text()[not(normalize-space())]"/>

</xsl:stylesheet>
T:\ftemp>saxon pedro.xml pedro.xsl
<?xml version="1.0" encoding="utf-8"?>
<root>
   <aaa>
      <bbb/>
      <bbb/>
   </aaa>
   <aaa>
      <bbb/>
      <bbb/>
   </aaa>
   <ccc/>
   <ccc>

<abc>test</abc>

   </ccc>
</root>
T:\ftemp>


-- Public training 3 days XSLT & 2 days XSL-FO: Phoenix,AZ 2004-08-23 World-wide on-site corporate, govt. & user group XML/XSL training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread