Transform XML to XML using XSLT

Discussion in 'XML' started by adi, Jun 5, 2006.

  1. adi

    adi Guest

    hello all,
    seems like a simple issue, but having tried several approaches with
    no success I am posting this question,

    I have a XML in one format, I want to now convert into another XML
    format using XSLT.

    FirstType.XML: (In this XML the States sub elements appear within USA
    element)
    ------------------------------------------------
    <?xml version="1.0" encoding="utf-8"?>
    <ROOT>
    <Country>
    <USA>
    <State>
    <City1>6000</City1>
    <City2>100</City2>
    <City3> </City3>
    </State>
    <State>
    <City1>1000</City1>
    <City2>10</City2>
    <City3>2</City3>
    </State>
    </USA>
    </Country>
    </ROOT>
    ------------------------------------------------

    DestinationXML: (I want the above XML to appear this way, note the
    element <Type> is something I want to add - and is not in the originial
    XML)
    ------------------------------------------------
    <?xml version="1.0" encoding="utf-8"?>
    <Region>
    <Area>
    <Type> Original </Type>
    <City1>6000</City1>
    <City2>100</City2>
    </Area>
    <Area>
    <Type> Original </Type>
    <City1>1000</City1>
    <City2>10</City2>
    </Area>
    </Region>
    ------------------------------------------------

    My XSLT (Obviously broken :)
    ------------------------------------------------
    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <Region>
    <Area>
    <xsl:apply-templates select="/"> <xsl:sort
    select="ROOT/Country/USA/State/City1"/>
    <xsl:sort select="ROOT/Country/USA/State/City2"/>
    </xsl:apply-templates>
    </Area>
    </Region>
    </xsl:template>

    <xsl:template match="USA">
    <xsl:for-each select="ROOT/Country/USA/State">
    <xsl:attribute name="Type"> <xsl:text>1</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="City1"> <xsl:value-of select="City1"/>
    </xsl:attribute>
    <xsl:attribute name="City2"> <xsl:value-of select="City2"/>
    </xsl:attribute>
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>
    ------------------------------------------------

    I am using .NET classes to do the transformation. Please advice on what
    I can do it fix this.

    thanks for all help.
    adi
     
    adi, Jun 5, 2006
    #1
    1. Advertising

  2. Try stating it in English...

    You want to create a new Region document whose Areas correspond to the
    States in the original document, and which contain only the City1 and
    City2 tags.

    As with any programming language, there are many possible ways to
    organize the details of that task; which is best depends in part on what
    you expect you're going to want to do with the data in the future. One
    possibility might be:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <Region>
    <xsl:apply-templates select="//State"/>
    </Region>
    </xsl:template>

    <xsl:template match="State">
    <Area>
    <Type> Original </Type>
    <xsl:copy-of select="City1"/>
    <xsl:copy-of select="City2"/>
    </Area>
    </xsl:template>

    </xsl:stylesheet>


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jun 6, 2006
    #2
    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. Isambella  via DotNetMonster.com

    Transform XML string using XSLT file

    Isambella via DotNetMonster.com, Aug 1, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    9,769
    Isambella via DotNetMonster.com
    Aug 2, 2005
  2. Igor
    Replies:
    1
    Views:
    4,991
    Dimitre Novatchev
    Jul 20, 2003
  3. Replies:
    0
    Views:
    520
  4. Replies:
    1
    Views:
    686
    Joris Gillis
    Feb 2, 2005
  5. Water Cooler v2

    Transform an XML file using XSLT

    Water Cooler v2, Aug 1, 2006, in forum: XML
    Replies:
    1
    Views:
    761
    Joe Kesselman
    Aug 2, 2006
Loading...

Share This Page