Transform XML to XML using XSLT

Adi (archive)

Usenet Archive
Joined
Mar 27, 2002
Messages
35
Reaction score
0
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
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
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
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top