【发布时间】:2013-05-01 22:36:34
【问题描述】:
我正在做一个项目,该项目需要我将 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”也不起作用。
任何帮助将不胜感激。
【问题讨论】:
-
我刚刚在网络上搜索了“将 XElement 转换为 XmlElement”,并收到了很多有希望的结果。你有没有尝试过,如果有,发生了什么?
-
我已经尝试过了,但是,我无法将解决方案实现到 html 表中,这是我需要能够做的。
-
嗯,这是由于无法将 XElement 转换为 XmlElement 还是其他原因?能够分离任务很重要。
-
常见的反应似乎是使用带有节点匹配的 xsl 模板。问题是xsl模板标签不能是html表中
标签的一部分,所以我卡住了。
标签: asp.net xml linq xslt xelement