在xsl select中的元素中带有破折号/连字符的xml

时间:2016-05-17 20:47:29

标签: xml xslt

我的xsl xml读取中的选择得到0结果。我怀疑它是元素路径中的破折号/连字符。

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>

我哪里错了?

2 个答案:

答案 0 :(得分: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"),然后在该NS的前缀中添加任何元素名称(例如/sa-rest/status-set/@action-name变为/rest:sa-rest/rest:status-set/@action-name)。

3)这个是可选的。我更喜欢总是在一个环境中运作;所以不是在我的模板中匹配/,而是匹配/*(即根元素本身),然后在我的select语句中,我访问相对于该上下文的其他元素(例如,将/rest:sa-rest/rest:status-set/@action-name替换为./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" />
	<head>  
	</head>  
	<body>  
	<table>
	<tr><th>Name</th><th>State</th></tr>
	<tr>
	 <td><xsl:value-of select="./rest:status-set/@action-name"/></td>
	 <td><xsl:value-of select="./rest:status-set/@state"/></td>
	 </tr>
	 </table> 
	</body>  
   </html>  
  </xsl:template>  
</xsl:stylesheet>  

旁注

如果您对某个问题有假设,请尝试测试该假设。即你认为问题是由连字符引起的;您是否尝试更改XML和XSLT以从元素名称中删除连字符并进行测试(例如status-set - &gt; statusset)?这将证明或反驳这一理论,确保您将调查集中在正确的地方/不要根据错误的假设进行长时间的工作。

希望有所帮助。

答案 1 :(得分:0)

尝试声明xmlns =&#34; http://iemfsa.tivoli.ibm.com/REST" xsl中的命名空间。或者为它指定一个前缀xmlns:tivoli =&#34; http ...并使用tivoli:sa-rest / tivoli:status-set etc ...

<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>