XML不能用XSL样式表显示
问题描述:
我遇到了一个问题,我的XML没有正确显示。基本上,我有一个充满链接的XML文档,我希望XSL样式表在有序列表中输出XML。到目前为止,一切工作正常,样式是正确的,但没有数据显示的链接。你只是看到了风格化的背景。我将XML正确地连接到XSL,并且Dreamweaver毫无问题地验证了XML代码。不知道我在这里错过了什么?XML不能用XSL样式表显示
的test.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="teststyle.xsl"?>
<country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<au>
<open><li><a href="/contest/test/goto.php?id=0" target="_blank"></open>
<description>Win a Macbook!</description>
<close></a></li></close>
</au>
<au>
<open><li><a href="/contest/test/goto.php?id=1" target="_blank"></open>
<description>Win a trip to Las Vegas!</description>
<close></a></li></close>
</au>
</country>
teststyle.xsl
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="test.xml" -->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Untitled Document</title>
</head>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="country/au">
<div style="background-color:teal;color:white;padding:4px">
<ol>
<span style="font-weight:bold"><xsl:value-of select="country/au/open" /><xsl:value-of select="country/au/description"/><xsl:value-of select="country/au/close"/></span>
</ol>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答
当你拥有“每个”模块,那么所有块内的指令是相对于你的元素在运行它们。这意味着,而不是
<xsl:value-of select="country/au/open" />
,你应该只使用
<xsl:value-of select="open" />
此外,假设您确实需要<和>“打开”和“关闭”块中的字符,则需要禁用这些链接上的输出转义。否则,您的页面中会出现转义码。
这是你的XSL的全部,工作版本:
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="test.xml" -->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Untitled Document</title>
</head>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="country/au">
<div style="background-color:teal;color:white;padding:4px">
<ol>
<span style="font-weight:bold"><xsl:value-of select="open" disable-output-escaping="yes" /><xsl:value-of select="description"/><xsl:value-of select="close" disable-output-escaping="yes"/></span>
</ol>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
不过,我强烈推荐把转义的HTML代码到您的XML这样的。目前还不清楚发生了什么事情,并且逃避所有角色涉及许多不必要的混乱。找出你真正需要的数据并使用XSL将数据转换为有效的HTML会更好。例如,如果你改变了你的XML数据文件,这一点:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="teststyle.xsl"?>
<country xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<au>
<url>/contest/test/goto.php?id=0</url>
<target>_blank</target>
<description>Win a Macbook!</description>
</au>
<au>
<url>/contest/test/goto.php?id=1</url>
<target>_blank</target>
<description>Win a trip to Las Vegas!</description>
</au>
</country>
那么这个XSL使行为更清楚一点(和你不需要处理任何逃避!):
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="test.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="country/au">
<div style="background-color:teal;color:white;padding:4px">
<ol style="font-weight:bold">
<a href="{url}" target="{target}"><xsl:value-of select="description"/></a>
</ol>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
哇, 谢谢!我今天刚开始学习XML/XSL,所以我仍然在学习绳索。 – RayMicro 2011-04-29 05:40:55
+1最后回答阻碍DOE机制。 – 2011-04-29 13:39:29