2014-12-29 108 views
1

我正在嘗試使用XML/XSL和Javascript爲大學任務創建購物籃。我用XML/XSL創建了一個股票列表,但是當我在瀏覽器中運行代碼時,該表顯示但未填充。我認爲這是我的xpath的問題,但我不確定如何解決它。XML/XSL表不會填充

我的XML是:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="basket.xsl"?> 
<data-set> 
<basket id="001"> 
<Product>Shorts (F) </Product> 
<Description>Stone Wash Denim Shorts</Description> 
<stockLevel>20</stockLevel> 
<price>£25.90</price> 
</basket> 
<basket id="002"> 
<Product>Bag (F)</Product> 
<Description>Leather Shoulder Bag</Description> 
<stockLevel>4</stockLevel> 
<price>£50.45</price> 
</basket> 
<basket id="003"> 
<Product>Blouse (F)</Product> 
<Description>Vintage Blue Silk Polka Dot Blouse</Description> 
<stockLevel>8</stockLevel> 
<price>£45.99</price> 
</basket> 
<basket id="004"> 
<Product>Boots (F)</Product> 
<Description>Soft Leather Brown Ankle Boots</Description> 
<stockLevel>3</stockLevel> 
<price>£65.35</price> 
</basket> 
<basket id="005"> 
<Product>Belts (F)</Product> 
<Description>Woven Finish Fashion Belt</Description> 
<stockLevel>15</stockLevel> 
<price>£21.99</price> 
</basket> 
<basket id="006"> 
<Product>Shirt (M)</Product> 
<Description>Jacquard Pattern Wrangler Western Shirt</Description> 
<stockLevel>19</stockLevel> 
<price>£34.87</price> 
</basket> 
<basket id="007"> 
<Product>Shoes (M) </Product> 
<Description>Suede Ankle Boots</Description> 
<stockLevel>6</stockLevel> 
<price>£55.00</price> 
</basket> 
<basket id="008"> 
<Product>Trousers (M)</Product> 
<Description>Izod Peach Chinos</Description> 
<stockLevel>23</stockLevel> 
<price>£31/75</price> 
</basket> 
<basket id="009"> 
<Product>Belt (M)</Product> 
<Description>Suede Casual Belt</Description> 
<stockLevel>4</stockLevel> 
<price>£22.98</price> 
</basket> 
<basket id="010"> 
<Product>Hat (M)</Product> 
<Description>Trilby Style Brown Woven Fix</Description> 
<stockLevel>2</stockLevel> 
<price>£67.80</price> 
</basket> 
</data-set> 

和我的XSL代碼:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method = "html" omit-xml-declaration = "no" doctype-system = "http://www.w3.org.TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public = "-//W3C//DTD XHTML 1.0 Strict//EN" /> 

<xsl:template match = "/"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> My Shopping Basket </title> 
</head> 
<body> 
<table border="1" bgcolor="White"> 
<thead> 
<tr> 
<th>Item Name</th> 
<th>Item Description</th> 
<th>Price</th> 
<th>Quantity</th> 
</tr> 
</thead> 
<xsl:for-each select="basket"> 
<tr> 
<td><xsl:value-of select="@id"/></td> 
<td><xsl:value-of select="Product" /></td> 
<td><xsl:value-of select="Description" /></td> 
<td><xsl:value-of select="stockLevel" /></td> 
<td><xsl:value-of select="price" /></td> 
</tr> 
</xsl:for-each> 
</table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

這段代碼生成是一個空白表。它擁有所有的標題和列(項目名

項目說明

價格

數量)

但沒有數據被填充到它。

+0

如果您發佈錯誤消息,並且可能產生的HTML看起來如此,您將在此處獲得更多答覆。不要讓我們運行你的轉變,看看你所看到的。 – duffymo

回答

1

你的模板相匹配/,而您的根元素是/data-set

或者:

  • 改變你的模板元素<xsl:template match = "/data-set">
  • 或創建一個額外的模板<xsl:template match = "data-set">,然後調用<xsl:apply-templates select='data-set'>從現有<xsl:template match = "/">模板。

順便說一句,你也可以考慮重構的basket映射出的對,每到自己的模板,然後通過apply-templates調用。這允許重構在其他語言中允許的相同優點,例如,允許您重複使用此模板。

+0

我將模板元素更改爲,但它仍然會生成一個空白表。 – SilentUK

+0

@SilentUK適合我和網上?這裏演示:http://xsltransform.net/3Nqn5Yo/2 – StuartLC

+0

沒關係我真的很愚蠢。感謝您的幫助。我試圖打開錯誤的文件。 – SilentUK