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]
"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>
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.
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.
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/>