xml - 使用 XSLT 从 XML 中选择数据?

标签 xml xslt

好的,我有 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/

相关文章:

xml - Postgresql:如何更改 XML 列中属性的值?

c# - ServiceStack反序列化xml到对象

android - 用android读取xml

javascript - HTML 中 document.getElementsByName 中的 for 循环

xml - XPath 联合运算符和上下文

Java XML 资源包和 HTML 内容

javascript - 如何从网络 worker 内部访问对象元素数据?

java - 单个 XSLT 文件能否解决这个问题 - 继续..?

Java 和 XSLT : How to specify multiple input files to xslt in java?

xml - 按键 : selecting last child of a preceding-sibling node