XSLT

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • srini

    #1

    XSLT

    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

    #2
    Re: XSLT



    srini wrote:
    [color=blue]
    > 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.[/color]

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

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" encoding="UTF-8" />

    <xsl:template match="item[@itemno = '2b']">
    <xsl:copy>
    <xsl:attribut e name="itemno">3 b</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/

    Comment

    • srini ganesan

      #3
      Re: XSLT



      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!

      Comment

      Working...