我想將XML轉換爲其他格式的XML,因此我使用了XSLT。但結果不好。我的XSLT有什麼問題?
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>
結果我想(在瀏覽器):
<?xml version="1.0" encoding="utf-8"?>
<root>
<items>
<item>Empire Burlesque</item>
<item>Hide your heart</item>
<item>Greatest Hits</item>
</items>
</root>
真實的結果(瀏覽器):
Empire Burlesque Hide your heart Greatest Hits
什麼是錯的我的XSLT?
你看過來源嗎?可能您的瀏覽器正在應用默認樣式表來格式化XML。 – Xint0
根據你的實際代碼,我得到的是'Bob DylanBonnie TylerDolly Parton',因爲你對藝術家做了一個'foreach' – PatomaS