XSLT: help with attributes?

M

Mark Tranchant

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?
 
M

Martin Honnen

Mark said:
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:eek:utput 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top