Reputation: 2338
I want to XML to other format XML so I used XSLT. But tt became a bad result.
XML:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.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>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<root>
<items>
<xsl:for-each select="catalog/cd">
<item>
<xsl:value-of select="artist"/>
</item>
</xsl:for-each>
</items>
</root>
</xsl:template>
</xsl:stylesheet>
Result I want (in browser):
<?xml version="1.0" encoding="utf-8"?>
<root>
<items>
<item>Empire Burlesque</item>
<item>Hide your heart</item>
<item>Greatest Hits</item>
</items>
</root>
Real result (in browser):
Empire Burlesque Hide your heart Greatest Hits
What is wrong my XSLT?
Upvotes: 0
Views: 214
Reputation: 12729
It seems like you are interested in how it renders in the browser. In that case, maybe this is what you want...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<xsl:text disable-output-escaping="yes"><!DOCTYPE html></xsl:text>
<html>
<head>
<meta charset="utf-8" />
<title>List of CDs</title>
</head>
<body>
<ul>
<xsl:for-each select="catalog/cd">
<li><xsl:value-of select="artist"/></li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 1603
You can change you xsl to this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<root>
<xsl:for-each select="catalog/cd">
<items>
<item>
<xsl:value-of select="title"/>
<xsl:text xml:space="preserve"> </xsl:text>
</item>
</items>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
And that will generate in the browser this:
Empire Burlesque
Hide your heart
Greatest Hits
And if you click on it, in firefox, and you select web developer > view source > view generated source
you will get this:
<root><items><item>Empire Burlesque
</item></items><items><item>Hide your heart
</item></items><items><item>Greatest Hits
</item></items></root>
which is what you said you want.
Remember that in the browser you will see the text, all the tags that are not html, are discarded. If you check the source code, you will see the xml file since that is what you loaded. If you check the generated source is what the transformation is telling the browser to show.
By default, the rendering engine of the browser, works with html, that's why it ignores any other tag.
Bye
Upvotes: 2
Reputation:
I would wager that you are using Firefox, and its trying to render it as HTML which means its removing tags it doesn't understand. Try right clicking thpage and viewing the source and seeing if the source of the page is correct.
Upvotes: 2