2016-02-19 105 views
0

我正在爲xml寫xslt,但是我試圖從根節點讀取ItemNumber時卡住了。這裏是代碼片段無法從根節點獲取值

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

我需要一個像輸出幾乎this.I'm這樣做,但有一個問題同時獲取ItemNumber。任何人都可以請指導我。我如何閱讀該財產。幫助將被讚賞。

感謝

回答

2

鑑於<Item ItemNumber="1148087">ItemNumber是您使用XPath選擇爲@ItemNumber(或attribute::ItemNumber,如果你想成爲詳細)屬性節點。您嘗試選擇一個子元素,而不是屬性。