XSLT

Srini

Usenet Archive
Joined
Apr 22, 2002
Messages
131
Reaction score
0
Hello everyone,
I'm trying to transform this xml to another xml but couldn't quite figure it out.
Source.xml

<all>
<object name="objname" value="objvalue">
<elem1 att="value" att2="value"/>
<elem1 att="value" att2="value"/>
<object name="name" value="645">
<items>
<item itemno="1a" itemname="name"/>
<item itemno="1b" itemname="name"/>
<item itemno="1c" itemname="name"/>
</items>
</object>
<object name="name" value="646">
<items>
<item itemno="2a" itemname="name"/>
<item itemno="2b" itemname="name"/>
<item itemno="2c" itemname="name"/>
</items>
</object>
</object>
</all>

I want to transform the above document to another xml which looks exactly the same except the itemno with value 2b would have a different value say 3b. The <object> element under <all> can have any number of elements. here's the output <all> <object name="objname" value="objvalue"> <elem1 att="value" att2="value"/> <elem1 att="value" att2="value"/> <object name="name" value="645"> <items> <item itemno="1a" itemname="name"/> <item itemno="1b" itemname="name"/> <item itemno="1c" itemname="name"/> </items> </object> <object name="name" value="646"> <items> <item itemno="2a" itemname="name"/> <item itemno="3b" itemname="name"/> <item itemno="2c" itemname="name"/> </items> </object> </object> </all>

I'd appreciate any help from anyone.

Thanks,
Srini
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
srini said:
Hello everyone,
I'm trying to transform this xml to another xml but couldn't quite figure it out.
Source.xml

<all>
<object name="objname" value="objvalue">
<elem1 att="value" att2="value"/>
<elem1 att="value" att2="value"/>
<object name="name" value="645">
<items>
<item itemno="1a" itemname="name"/>
<item itemno="1b" itemname="name"/>
<item itemno="1c" itemname="name"/>
</items>
</object>
<object name="name" value="646">
<items>
<item itemno="2a" itemname="name"/>
<item itemno="2b" itemname="name"/>
<item itemno="2c" itemname="name"/>
</items>
</object>
</object>
</all>

I want to transform the above document to another xml which looks exactly the same except the itemno with value 2b would have a different value say 3b. The <object> element under <all> can have any number of elements. here's the output <all> <object name="objname" value="objvalue"> <elem1 att="value" att2="value"/> <elem1 att="value" att2="value"/> <object name="name" value="645"> <items> <item itemno="1a" itemname="name"/> <item itemno="1b" itemname="name"/> <item itemno="1c" itemname="name"/> </items> </object> <object name="name" value="646"> <items> <item itemno="2a" itemname="name"/> <item itemno="3b" itemname="name"/> <item itemno="2c" itemname="name"/> </items> </object> </object> </all>

I'd appreciate any help from anyone.

Use identity transformation for everying but <item itemno="2b" ... />:

<?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="item[@itemno = '2b']">
<xsl:copy>
<xsl:attribute name="itemno">3b</xsl:attribute>
<xsl:apply-templates select="@*[local-name != 'itemno']" /> <xsl:apply-templates select="node" /> </xsl:copy> </xsl:template>

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

</xsl:stylesheet>
 

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

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top