XSLT

Discussion in 'XML' started by srini, Apr 5, 2004.

  1. srini

    srini Guest

    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
    srini, Apr 5, 2004
    #1
    1. Advertising

  2. srini wrote:

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

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 6, 2004
    #2
    1. Advertising

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

    Srini

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    srini ganesan, Apr 11, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    622
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,651
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    607
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    701
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    648
Loading...

Share This Page