1

I'm getting 0 results from my select in my xsl xml read. I suspect it's the dash/hyphen in the element path.

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>  

but the results I get are:

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

Where am I going wrong?

Jon Mason
  • 23
  • 3
  • This has nothing to do with hyphens and everything to do with *namespaces* - see: http://stackoverflow.com/questions/34758492/xslt-transform-doesnt-work-until-i-remove-root-node/34762628#34762628 – michael.hor257k May 17 '16 at 21:55

2 Answers2

2

1) You're copying the attribute itself rather than getting its value.

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

2) You're accessing the element names without saying which namespace they're in. Add a reference to the namespace in the stylesheet element (e.g. xmlns:rest="http://iemfsa.tivoli.ibm.com/REST"), then prefix any element names from that NS with this (e.g. /sa-rest/status-set/@action-name becomes /rest:sa-rest/rest:status-set/@action-name).

3) This one's optional. I prefer to always operate within a context; so rather than matching / in my template, I match /* (i.e. the root element itself), then in my select statements I access other elements relative to that context (e.g. replace /rest:sa-rest/rest:status-set/@action-name with ./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>  

Side Note

If you have an assumption about an issue, try testing that assumption. i.e. you thought the issue was caused by the hyphens; did you try changing your XML and XSLT to remove the hyphens from the element names and test without (e.g. status-set -> statusset)? That would prove or disprove that theory, ensuring you focus your investigation in the right place / don't spend long working based on false assumptions.

Hope that helps.

JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
0

Try declaring the xmlns="http://iemfsa.tivoli.ibm.com/REST" namespace in the xsl. Or specify a prefix for it xmlns:tivoli="http... and the use 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>
Stefan Hegny
  • 2,107
  • 4
  • 23
  • 26