我正在做一个项目,该项目需要我将 xml 文档加载到 xslt 并将其转换为显示在 html 表中。问题是因为我使用 LINQ 创建 XML,所有元素标签都是 XElement 标签,所以“xsl:value-of select”属性不会读取 XElement。我想知道是否有办法将 XElement 转换为 XmlElement 或者只是让 XSLT 读取 XElements 而不是 XMLElements?
这是我通过 LINQ 将数据加载到 xml 文件中的代码:
List<Prod> Products = utils.getProducts();
XElement xml = new XElement("Products", Products.Select(x => new XElement("Product",
new XElement("ProductID", x.ProductID),
new XElement("ProductName", x.ProductName),
new XElement("SupplierID", x.SupplierID),
new XElement("CategoryID", x.CategoryID),
new XElement("QuantityPerUnit", x.QuantityPerUnit),
new XElement("UnitPrice", x.UnitPrice),
new XElement("UnitInStock", x.UnitInStock),
new XElement("UnitsOnOrder", x.UnitsOnOrder),
new XElement("ReorderLevel", x.ReorderLevel))));
xml.Save("C:/Users/Aaron/Documents/Visual Studio 2012/WebSites/INFO451Final/Part_B/Prod.xml");
这是我的 XSLT 的代码:
<xsl:template match="/">
<html>
<body>
<h2>Products</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>ProductID</th>
<th>ProductName</th>
<th>SupplierID</th>
<th>CategoryID</th>
<th>QuantityPerUnit</th>
<th>UnitPrice</th>
<th>UnitInStock</th>
<th>UnitsOnOrder</th>
<th>ReorderLevel</th>
</tr>
<xsl:for-each select="Product">
<tr>
<td>
<xsl:value-of select="ProductID"/>
</td>
<td>
<xsl:value-of select="ProductName"/>
</td>
<td>
<xsl:value-of select="SupplierID"/>
</td>
<td>
<xsl:value-of select="CategoryID"/>
</td>
<td>
<xsl:value-of select="QuantityPerUnit"/>
</td>
<td>
<xsl:value-of select="UnitPrice"/>
</td>
<td>
<xsl:value-of select="UnitInStock"/>
</td>
<td>
<xsl:value-of select="UnitsOnOrder"/>
</td>
<td>
<xsl:value-of select="ReorderLevel"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
所以,现在所有的负载都是标题,因为“xsl:for-each”也不起作用。
任何帮助将不胜感激。