Transform XML to XML using XSLT

A

adi

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
 
J

Joe Kesselman

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>
 

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
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top