Mark Tranchant wrote:
I'm struggling to find a way to achieve the following transformation:
<x a="1" b="2" c="3" ... />
into
<y b="2" c="3" ... > <z a="1" /> </y>
In other words, I want to pull out a specific attribute into one result
element (easy) and copy the remaining arbitrary list of elements into
another (help needed!).
Any experts care to point me into the right direction?
You usually start with the identity transformation which copies every
node and recursively processes the child nodes, then you add templates
matching those nodes you want to treat differently, in that case your
<x> element.
For instance with the example XML being
<?xml version="1.0" encoding="UTF-8"?>
<root>
<god>Kibo</god>
<x a="1" b="2" c="3" />
<devil>Xibo</devil>
</root>
and the XSLT stylesheet being
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="x">
<y>
<xsl:apply-templates select="@*[local-name() != 'a']" />
<z a="{@a}" />
</y>
</xsl:template>
</xsl:stylesheet>
the result document is
<?xml version="1.0" encoding="UTF-8"?>
<root>
<god>Kibo</god>
<y b="2" c="3"><z a="1"/></y>
<devil>Xibo</devil>
</root>
--
Martin Honnen
http://JavaScript.FAQTs.com/