2014-12-29 71 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