我遇到的问题是我的XML没有正确显示。基本上,我有一个充满链接的XML文档,我希望XSL样式表在有序列表中输出XML。到目前为止,一切正常,样式正确,但链接没有显示数据。你只看到风格化的背景。我正确地将XML连接到XSL和Dreamweaver,毫不费力地验证了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>
<?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>
答案 0 :(得分:2)
如果你有一个“for-each”块,那么该块中的所有指令都与你运行它们的元素有关。这意味着代替
<xsl:value-of select="country/au/open" />
你应该使用
<xsl:value-of select="open" />
另外,假设你真的想要&lt;和&gt;从“打开”和“关闭”块中的字符,您需要禁用这些链接上的输出转义。否则,您最终会在页面中使用转义码。
以下是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>