[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: reversing or swapping nested node hierarchy

Subject: Re: reversing or swapping nested node hierarchy
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 19 Mar 2006 14:01:09 -0500
xml nested nodes
At 2006-03-19 13:40 -0500, Chris Capon wrote:
In an XML document:
...
Is it possible to reverse the a/b hierarchy to produce b/a on output?
To complicate the problem, not all <a>'s have <b>'s.

So, in solving the problem, I've come up with something like this:
...
..which works, but the <b> template now has working knowledge of <a>.

I'd like to find a generic solution where <b> doesn't have to know about
<a> so that <b> can be nested inside other elements too and yet work the
same way.

I hope the code below helps. You don't say what you want done with attributes, so I didn't do anything with them myself.


. . . . . . . . . Ken

T:\ftemp>type capon.xml
<root>
  <a>
    <b />
  </a>
  <a />
  <d>
    <b />
  </d>
  <d/>
  <c>
    <b />
  </c>
</root>

T:\ftemp>xslt capon.xml capon.xsl con
<?xml version="1.0" encoding="utf-8"?><root>
  <b><a/></b>
  <a/>
  <b><d/></b>
  <d/>
  <b><c/></b>
</root>
T:\ftemp>type capon.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

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

<xsl:template match="*[b]">
  <b>
    <xsl:copy/>
  </b>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>

--
Upcoming XSLT/XSL-FO hands-on courses: Washington,DC 2006-06-12/16
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 Cancer Awareness Aug'05  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.