好的,我有 product.xml 和 product.xsl
在 product.xml 中说我有两位数据
<productInfo productID="Product1">
<title>Product One</title>
</productInfo>
<productInfo productID="Product2">
<title>Product Two</title>
</productInfo>
在我的 product.xsl 中是否可以根据 productID 参数只显示一组数据?
如果 product.xml 加载为 product.xml?productID=Product1,我如何才能只显示 Product1 数据?
我试图从 URL 中获取 productID 的值,但这不起作用..
<xsl:param name="productID" />
<xsl:value-of select="$productIDParam"/>
仅使用 XML 和 XSLT 是否可以实现我正在尝试做的事情?
最佳答案
So if product.xml was loading up as product.xml?productID=Product1
how can I only show Product1 data?
I tried to get the value of productID from the URL but this does not work..
<xsl:param name="productID" /> <xsl:value-of select="$productIDParam"/>
Is what I am trying to do even possible by just using XML and XSLT?
在开始转换之前,您需要获取“productId”查询变量的值,然后将此值作为全局级外部参数的值传递。
不同的 XSLT 处理器有不同的 API 来实现这一点。例如.NET XslCompiledTransform 处理器使用 XsltArgumentList
的实例实现此目的类作为其 Transform()
的参数传递 方法。
这是一个完整的代码示例:
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public class Sample {
public static void Main() {
// Create the XslCompiledTransform and load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("discount.xsl");
// Create the XsltArgumentList.
XsltArgumentList argList = new XsltArgumentList();
// Calculate the discount date.
DateTime orderDate = new DateTime(2004, 01, 15);
DateTime discountDate = orderDate.AddDays(20);
argList.AddParam("discount", "", discountDate.ToString());
// Create an XmlWriter to write the output.
XmlWriter writer = XmlWriter.Create("orderOut.xml");
// Transform the file.
xslt.Transform(new XPathDocument("order.xml"), argList, writer);
writer.Close();
}
}
因此,您需要阅读 XSLT 处理器的文档,了解如何将外部参数传递给转换的说明。
关于xml - 使用 XSLT 从 XML 中选择数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4586219/