XSLT

S

srini

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
 
M

Martin Honnen

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>
 
S

srini ganesan

Thanks a lot for your solution Martin. It worked and I appreciate your
help.

Srini
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top