[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] reversing or swapping nested node hierarchy


Subject: Re: [xsl] reversing or swapping nested node hierarchy
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 20 Mar 2006 16:44:01 -0500

At 2006-03-20 16:06 -0500, Chris Capon wrote:
Thanks for your reply.  My example may have been too simple for what I'm
trying to do.

Writing a transformation specification by example is only as accurate as the example.


I should have specified that <a>, <b>, etc need to be
transformed, so a simple node copy would not be enough.

Fine, yes, you should have.


What I'm essentially looking for is a way to change the node hierarchy
during transformation.

But *that* is what XSLT is all about: building a new node hierarchy from an old one. That statement alone does not constitute a transformation specification.


So, if my document were:

<root>
  <a name="alpha">
    <b tag="beta" />
  </a>
</root>

and my templates were:

<template match="a">
  <aa>
    <copy-of select="@name" />
  </aa>
</template>
<template match="b">
  <bb>
    <copy-of select="@tag" />
  </bb>
</template>

would there be a way to produce this output:

<doc>
  <bb tag="beta">
    <aa name="alpha">
  </bb>
</doc>

where the node hierarchy a/b has been reversed to become bb/aa ?

Yeah, sure ... an example is below ...


The optimal solution would leave the "b" template generic so the element
could be included under other elements like  <a>, <c> and <d> in your
example.

Fine ... but your implication is that for every element there would be a new element, so I really don't know what it is you are trying to do. In XSLT you always provide the "new" element when triggered by the "old" element, even if it is the same element type, as XSLT's metaphor is "transformation by result construction". The code below is merely an extension of what I did before, so if this doesn't help you then I can't think of any other way to provide guidance. You don't say what it is about the earlier example that you did not understand ... are you looking for understanding, or are you looking for an answer?


I hope this helps.

. . . . . . . . . . Ken

T:\ftemp>type capon.xml
<root>
  <a name="alpha">
    <b tag="beta"/>
  </a>
  <a other="gamma"/>
  <d>
    <b tag="delta"/>
  </d>
  <d/>
  <c>
    <b test="epsilon"/>
  </c>
</root>

T:\ftemp>xslt capon.xml capon.xsl con
<?xml version="1.0" encoding="utf-8"?><doc>
  <bb tag="beta"><aa name="alpha"/></bb>
  <a other="gamma"/>
  <bb tag="delta"><dd/></bb>
  <d/>
  <bb test="epsilon"><cc/></bb>
</doc>
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="/">
  <xsl:apply-templates select="@*|node()" mode="identity"/>
</xsl:template>

<xsl:template match="@*|node()" mode="identity">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" mode="identity"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="root" mode="identity">
  <doc>
    <xsl:apply-templates select="@*|node()" mode="identity"/>
  </doc>
</xsl:template>

<xsl:template match="*[b]" mode="identity">
  <xsl:apply-templates select="b"/>
</xsl:template>

<xsl:template match="a">
  <aa>
    <xsl:copy-of select="@*" />
  </aa>
</xsl:template>

<xsl:template match="c">
  <cc>
    <xsl:copy-of select="@*" />
  </cc>
</xsl:template>

<xsl:template match="d">
  <dd>
    <xsl:copy-of select="@*" />
  </dd>
</xsl:template>

<xsl:template match="b">
  <bb>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates select=".."/>
  </bb>
</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
Keywords