开发者

Selecting data from XML using XSLT?

开发者 https://www.devze.com 2023-02-02 00:21 出处:网络
Ok so i have product.xml and product.xsl In product.xml say I have two bits of data <productInfo productID=\"Product1\">

Ok so i have product.xml and product.xsl

In product.xml say I have two bits of data

<productInfo productID="Product1">
<title>Product One</title>
</productInfo>

<productInfo productID="Product2">
<title>Product Two</title>
</productInfo>

In my product.xsl is it possible to display only one set of data depending on the productID parameter?

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开发者_StackOverflow中文版:value-of select="$productIDParam"/>

Is what I am trying to do even possible by just using XML and 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?

Before initiating the transformation you need to obtain the value of the "productId" query variable and then pass this value as the value of a global-level, external parameter.

Different XSLT processors have different API for achieving this. For example the .NET XslCompiledTransform processor achieves this using instance(s) of the XsltArgumentList class passed as arguments of its Transform() method.

Here is a complete code example:

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();

  }

}

Therefore, you need to read the documentation of your XSLT processor for a description how to pass external parameters to the transformation.


XSLT allows you to define global parameters with top-level xsl:param elements, these are supposed to be set from outside the XSLT processor, mostly programmatically with the API of the XSLT processor. If you want to read out query string parameters in a URL you would need to do that with a client-side or server-side language of your choice (i.e. mainly Javascript on the client or various frameworks/languages like ASP.NET, Servlet, PHP on the server) and then run the transformation with any API exposed to that language or by that framework). That way you could pass on query string parameters to your XSLT stylesheet.

0

精彩评论

暂无评论...
验证码 换一张
取 消