2009-04-12 49 views
0

我有一個關於在XPATH中加載屬性的問題。 我寫短的XML代碼進行測試:在XPATH中加載屬性,問題

<?xml version="1.0" encoding="iso-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="testDate.xsl"?> 
<element attribute="1/1/2100"> 
    Hung 
</element> 

我的XSL代碼:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <!--Handle the document: set up HTML page--> 
    <xsl:template match="/"> 
    <html> 
    <head>  
    </head> 
    <body> 
    This is a test 
    <xsl:value-of select="[email protected]"/> 
    </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

爲什麼它加載樣式表時,會產生一個錯誤?你能幫我解釋一下嗎?謝謝

+0

這將是看你得到 – 2009-04-12 09:34:44

回答

2

我懷疑你的XPath屬性是錯誤的。我覺得應該是

element/@attribute 

即你應該用/

3

你需要把之前@斜線在<xsl:value-of />分離元件和@attribute。

由於[email protected]不是有效的XPath,所以出現錯誤。把在斜槓表示要:

  • 找到一個名爲element元素,然後
  • 這些元素中,找到一個名爲attribute的屬性。

以下修改樣式表對我的作品:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <!--Handle the document: set up HTML page--> 
    <xsl:template match="/"> 
    <html> 
    <head>  
    </head> 
    <body> 
       This is a test     
     <xsl:value-of select="element/@attribute"/> 
    </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet>