|
Subject: Re: [xsl] reversing or swapping nested node hierarchy From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Mon, 20 Mar 2006 16:44:01 -0500 |
Thanks for your reply. My example may have been too simple for what I'm trying to do.
I should have specified that <a>, <b>, etc need to be transformed, so a simple node copy would not be enough.
What I'm essentially looking for is a way to change the node hierarchy during transformation.
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 ?
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.
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 |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] reversing or swapping nes, Chris Capon | Thread | Re: [xsl] reversing or swapping nes, Chris Capon |
| Re: [xsl] Conditional test is faili, Gowri Ratakonda | Date | RE: [xsl] Conditional test is faili, Michael Kay |
| Month |