473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1487
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.