2011-04-29 118 views
1

我遇到了一個問題,我的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>&lt;li&gt;&lt;a href="/contest/test/goto.php?id=0" target="_blank"&gt;</open> 
    <description>Win a Macbook!</description> 
    <close>&lt;/a&gt;&lt;/li&gt;</close> 
</au> 
<au> 
    <open>&lt;li&gt;&lt;a href="/contest/test/goto.php?id=1" target="_blank"&gt;</open> 
    <description>Win a trip to Las Vegas!</description> 
    <close>&lt;/a&gt;&lt;/li&gt;</close> 
</au> 
</country> 

teststyle.xsl

<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="test.xml" --> 
<!DOCTYPE xsl:stylesheet [ 
<!ENTITY nbsp "&#160;"> 
<!ENTITY copy "&#169;"> 
<!ENTITY reg "&#174;"> 
<!ENTITY trade "&#8482;"> 
<!ENTITY mdash "&#8212;"> 
<!ENTITY ldquo "&#8220;"> 
<!ENTITY rdquo "&#8221;"> 
<!ENTITY pound "&#163;"> 
<!ENTITY yen "&#165;"> 
<!ENTITY euro "&#8364;"> 
]> 
<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> 

回答

2

當你擁有「每個」模塊,那麼所有塊內的指令是相對於你的元素在運行它們。這意味着,而不是

<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 "&#160;"> 
<!ENTITY copy "&#169;"> 
<!ENTITY reg "&#174;"> 
<!ENTITY trade "&#8482;"> 
<!ENTITY mdash "&#8212;"> 
<!ENTITY ldquo "&#8220;"> 
<!ENTITY rdquo "&#8221;"> 
<!ENTITY pound "&#163;"> 
<!ENTITY yen "&#165;"> 
<!ENTITY euro "&#8364;"> 
]> 
<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> 
+0

哇, 謝謝!我今天剛開始學習XML/XSL,所以我仍然在學習繩索。 – RayMicro 2011-04-29 05:40:55

+0

+1最後回答阻礙DOE機制。 – 2011-04-29 13:39:29