2016-05-17 71 views
1

我從我的xsl xml讀取選擇中獲得0個結果。我懷疑這是元素路徑中的破折號/連字符。xml與破折號/連字符元素在xsl中選擇

XML:

<sa-rest xmlns="http://iemfsa.tivoli.ibm.com/REST" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <status-set state="expired" action-name="Test Plan" /> 
</sa-rest> 

XSL:

<?xml version="1.0" encoding="utf-8"?> 
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
 
<xsl:output method="html" encoding="UTF-8" indent="yes"/> 
 
    <xsl:template match="/"> 
 
    <html> 
 
    <link rel="stylesheet" href="w3.css" /> 
 
    <head> 
 
    </head> 
 
    <body> 
 
    <table> 
 
    <tr><th>Name</th><th>State</th></tr> 
 
    <tr> 
 
    <td><xsl:copy-of select="/sa-rest/status-set/@action-name"/></td> 
 
    <td><xsl:copy-of select="/sa-rest/status-set/@state"/></td> 
 
    </tr> 
 
    </table> 
 
    </body> 
 
    </html> 
 
    </xsl:template> 
 
</xsl:stylesheet>

,但我得到的結果是:

<html> 
 
<link rel="stylesheet" href="w3.css"> 
 
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head> 
 
<body><table> 
 
<tr> 
 
<th>Name</th> 
 
<th>State</th> 
 
</tr> 
 
<tr> 
 
<td></td> 
 
<td></td> 
 
</tr> 
 
</table></body> 
 
</html>

我在哪裏出錯了?

+0

這與連字符和與*名稱空間*無關* - 請參閱:http://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 –

回答

1

1)你複製的屬性本身,而比獲得其價值。

<xsl:copy-of select="/sa-rest/status-set/@action-name"/> 應該 <xsl:value-of select="/sa-rest/status-set/@action-name"/

2)您所訪問的元素名稱不說他們在其中的命名空間。添加一個引用到樣式表元素的命名空間(如xmlns:rest="http://iemfsa.tivoli.ibm.com/REST" ),然後用這個(例如/sa-rest/status-set/@action-name變成/rest:sa-rest/rest:status-set/@action-name)。

3)這是可選的。我更喜歡總是在一種語境下運作;因此,我的模板中匹配/,我匹配/*(即根元素本身),然後在我的select語句中訪問與該上下文相關的其他元素(例如,用./rest:status-set/@action-name替換/rest:sa-rest/rest:status-set/@action-name)。

<?xml version="1.0" encoding="utf-8"?> 
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rest="http://iemfsa.tivoli.ibm.com/REST"> 
 
<xsl:output method="html" encoding="UTF-8" indent="yes"/> 
 
    <xsl:template match="/*"> 
 
    <html> 
 
    <link rel="stylesheet" href="w3.css" /> 
 
\t <head> 
 
\t </head> 
 
\t <body> 
 
\t <table> 
 
\t <tr><th>Name</th><th>State</th></tr> 
 
\t <tr> 
 
\t <td><xsl:value-of select="./rest:status-set/@action-name"/></td> 
 
\t <td><xsl:value-of select="./rest:status-set/@state"/></td> 
 
\t </tr> 
 
\t </table> 
 
\t </body> 
 
    </html> 
 
    </xsl:template> 
 
</xsl:stylesheet>

旁註

如果您有問題的假設,嘗試測試這個假設。即你認爲問題是由連字符引起的;您是否嘗試更改XML和XSLT以從元素名稱中刪除連字符並在沒有的情況下進行測試(例如status-set - >statusset)?這將證明或反駁該理論,確保您將調查的重點放在正確的地方,不要因爲錯誤的假設而長時間工作。

希望有所幫助。

0

嘗試在xsl中聲明xmlns =「http://iemfsa.tivoli.ibm.com/REST」命名空間。或指定一個前綴爲它的xmlns:宏福=「HTTP ...和使用的Tivoli:SA-REST /宏福:狀態設置等..

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:tivoli="http://iemfsa.tivoli.ibm.com/REST" 
exclude-result-prefixes="tivoli> 
<xsl:output method="html" encoding="UTF 
.... 
    <tr> 
    <td><xsl:copy-of select="/tivoli:sa-rest/tivoli:status-set/@action-name"/></td> 
    <td><xsl:copy-of select="/tivoli:sa-rest/tivoli:status-set/@state"/></td> 
    </tr> 
+0

我必須誠實,我不確定你的意思。你的意思是 ? –

+1

有沒有」或「在這裏:你*必須*使用前綴 –

+0

在你的XML你有xmlns =」http://iemfsa.tivoli.ibm.com/REST「這意味着你不能僅僅匹配sa-rest/status-set,因爲它有一個xml命名空間關聯 –