M A.
M A.

Reputation: 949

Unable to get value from root node

I'm wrting xslt for my xml but I'm stuck while trying to read the ItemNumber from the root node. Here is the code snippet

XML

 <?xml version="1.0"?>
 <Items>
<Item ItemNumber="1251469">
    <ProductName>Cherub Baby 240ml Single - Light Blue</ProductName>
    <ProviderName>Cherub Baby</ProviderName>
    <Quantity>25</Quantity>
    <Price>7.99</Price>
</Item>
<Item ItemNumber="1148087">
    <ProductName>Dolby Metal-Black-Matte</ProductName>
    <ProviderName>Vestal Watches</ProviderName>
    <Quantity>4</Quantity>
    <Price>67.99</Price>
</Item>
</Items>

XSLT

  <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <xsl:for-each select="Items/Item">
    <table border="2">
      <tr bgcolor="#9acd32">
        <td>Provider: <xsl:value-of select="ProductName"/></td>
      </tr>
     <tr>
        <td>Item Number</td>
        <td>Quantity</td>
        <td>Unit Price</td>
        <td>Total</td>
    </tr>
    <tr>
        <td><xsl:value-of select="ItemNumber"/></td> <-- //unable to get the value
        <td><xsl:value-of select="Quantity"/></td>
        <td><xsl:value-of select="Price"/></td>
        <td><xsl:value-of select="Quantity * Price"/></td>
    </tr>
    <tr>
    <td style="border:none"></td>
    <td style="border:none"></td>
    <td style="border:none">Sub Total</td>
    <td><xsl:value-of select="Quantity * Price"/></td>
    </tr>
    </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Output

I need an output like this.I'm almost done but having an issue while getting an ItemNumber. Can anyone please guide me. How can I read that property. Help will be Appreciated.

Thanks

Upvotes: 0

Views: 53

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

Given <Item ItemNumber="1148087">, ItemNumber is an attribute node which you select with XPath as @ItemNumber (or attribute::ItemNumber, if you want to be verbose). Your attempt selects a child element, not an attribute.

Upvotes: 2

Related Questions