Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Transform XML to XML using XSLT

Reply
Thread Tools

Transform XML to XML using XSLT

 
 
adi
Guest
Posts: n/a
 
      06-05-2006
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"
xmlnssl="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

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      06-06-2006
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"
xmlnssl="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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform XML string using XSLT file Isambella via DotNetMonster.com ASP .Net 4 08-02-2005 07:19 AM
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM
How to use XSLT to transform XML according to the data in another XML ai2003lian@yahoo.com XML 1 02-02-2005 05:07 PM
How to use XSLT to transform XML according to the data in another XML ai2003lian@yahoo.com XML 0 02-02-2005 04:57 PM
Transform XML using XSLT based on element position Igor XML 1 07-20-2003 08:16 PM



Advertisments