我正在尝试使用XML / XSL和Javascript为大学作业创建一个购物篮。我已经使用XML / XSL创建了一个库存清单,但是当我在浏览器中运行代码时,表格显示但未填充。我认为这是我的xpath的一个问题,但我不确定如何解决它。
我的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 :(得分:1)
您的模板与/
匹配,而您的根元素为/data-set
或者:
<xsl:template match = "/data-set">
<xsl:template match = "data-set">
,然后从现有的<xsl:apply-templates select='data-set'>
模板中调用<xsl:template match = "/">
。顺便说一下,您还可以考虑从for-each重构basket
映射到其自己的模板,然后通过apply-templates
调用。这允许重构在其他语言中允许的相同益处,例如,允许您重复使用此模板。