472,796 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

Transform XML to XML using XSLT

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

Jun 5 '06 #1
1 1460
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
Jun 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
8
by: Luther Miller | last post by:
I am using the XML tranform functionality in .NET to transform data in a DataSet into XMLSS using an XSLT file I have created. There are about 100 columns and only about 120 rows in the data...
3
by: Jason S | last post by:
Hello Group, I am just about tearing my hair out with this one and thought someone may have some insight. I have a transform that wasn't working so I grabbed the nearest debugger (xselerator)...
4
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt...
3
by: Andy | last post by:
Hi all, I'm having a problem doing an Xslt transform in code. I've done it before, so I'm not really sure why its not working. The problem is that the result of the transform is an empty...
1
by: Danny Lesnik | last post by:
Hi i have my XML file c:\prd.xm <?xml-stylesheet type="text/xsl" href="prd.xsl"?><products><product><a>2</a><b>3</b></product><product><a>4</a><b>2</b></product></products This is my XSL file...
3
by: Peter Row | last post by:
Hi, I have 2 XML files and 1 XSLT file. The second XML file has the following declarative 1st line: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> ....the 1st one (the one to be...
4
by: WStoreyII | last post by:
I wish to know how to set it up so that when an xml webservice is called that instead of displaying the xml in the browser it will render it with a xslt file the problem is i dont know how to do...
1
by: Steve | last post by:
Using VB.NET 2.0 I have a simple routine that attempts transforms an XmlDocument with an XSLT stylesheet into HTML. Under the old 1.1 framework with XslTransform, everything worked fine. Now...
12
by: das | last post by:
Hello all, I am using .NET XSLT to transform an XML into another XML file. All this is fine with small files, but when tested with big files (30MB) it is taking between 1hr-2hrs to just transform...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.