Beginner: Q on using XSL to convert from XML to XML

My first time dealing with XML/XSLT

I have to take a XML file(that is HUGE), and convert it into another XML format (the purpose being to get rid of most of the unwanted information and retaining just the ones our program needs)

I was told that I can do this easily using XSL.

I took some examples from the web and tried it on my machine. (I am using notepad and IE6.0)

Here is the XML

<?xml version="1.0" encoding="utf-8"?>
<!-- Edited with XML Spy v4.2 -->
<?xml-stylesheet type="text/xsl" href="cdcatalog2.xsl"?>

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>

and so on..<catalog>

Here is the XSL

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<xsl:stylesheet
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<!--xsl:element name="catalog" -->
<xsl:for-each select="catalog/cd">
<xsl:element name ="CDSinger">
<xsl:value-of select="artist" />
</xsl:element>
<br />
</xsl:for-each>
<!-- /xsl:element -->
</xsl:template>
</xsl:stylesheet>

So, when I open the xml file in IE, I expect

<CDSinger>Bob Dylan<CDSinger>
<CDSinger>Bonnie Tyler<CDSinger>
<CDSinger>Dolly Parton<CDSinger>
<CDSinger>Gary Moore
<CDSinger>

whereas here is what I see

Bob Dylan
Bonnie Tyler
Dolly Parton
Gary Moore

I would appreciate if you could explain if I am missing something.

Thanks.

[3064 byte] By [mrvgson] at [2007-12-23]
# 1

What are you saving it as ? Perhaps IE is rendering it as HTML ? What does it look like in notepad ?

cgraus at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 2

"What does it look like in notepad ?"

This is how the cdcatalog.xml file looks in notepad.

<?xml version="1.0" encoding="utf-8"?>
<!-- Edited with XML Spy v4.2 -->
<?xml-stylesheet type="text/xsl" href="cdcatalog2.xsl"?>

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>

...

</catalog>

mrvgson at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 3

Your generated XML is not being saved, the transform is happening in the browser ? In that case, it will be shown as HTML, right click on it and 'view source' to see the source document in notepad, almost certainly with your tags in it. They are being ignored as invalid HTML by IE.

cgraus at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 4

The original XML file is cdcatalog.xml.

When I double click to open it, cdcatalog.xml opens in IE and looks as shown

Bob Dylan
Bonnie Tyler
Dolly Parton
Gary Moore

However, when I use "View Source", what I see is exactly what was reported in the previous post i.e.

<?xml version="1.0" encoding="utf-8"?>
<!-- Edited with XML Spy v4.2 -->
<?xml-stylesheet type="text/xsl" href="cdcatalog2.xsl"?>

<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>......

I also tried File>Save As from IE to create a temp.xml. It also looks the same. as above.

mrvgson at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 5

I guess IE keeps the original XML and the stylesheet. I still think that your transform is working, and IE is rendering it as HTML.

What version of visual studio do you have ? VS2005 allows you to run XSL transforms in the IDE ( in fact, you can debug them ). Also, you seem to have XML Spy, it should also allow you to run an xsl and see the results in the IDE.

cgraus at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 6

Thanks cgraus.

I have VS 2003 and I dont have xmlspy (the comment in my xml is there probably because I copied the code from somewhere)

mrvgson at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 7

XML Spy home edition is free, and there are plenty of other free apps that will run an XSL for you. You'll need to do something, you can't keep using IE to run your XSLT for you. The easiest way to prove/disprove my theory is change the tag you're creating to strong and see if your text comes out in italics. If so, then the tags are there, but they are being used to render HTML.

cgraus at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 8

Thanks a lot.

I tried the <STRONG> and can see the impact. I shall try to download xmlspy.

Thanks for the help.

mrvgson at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 9

No worries. XSLT is a lot of fun. Actually, you should go and print this:

http://www.mulberrytech.com/quickref/index.html

I have the second one laminated on my desk, for the odd times when I have to do an XSLT ( not often enough for me to remember it all, sadly )

cgraus at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 10

Tested the following xslt :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<!--xsl:element name="catalog" -->
<xsl:for-each select="catalog/cd">
<xsl:element name ="CDSinger">
<xsl:value-of select="artist" />
</xsl:element>
<br />
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

with following xml:

<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>

</catalog>

Following output gets generated:

<?xml version="1.0" encoding="UTF-8"?><CDSinger>Bob Dylan</CDSinger><br/><CDSinger>Bonnie Tyler</CDSinger><br/>

dvohra at 2007-8-30 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified