3

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:value-of select="$productIDParam"/>

Is what I am trying to do even possible by just using XML and XSLT?

1
  • Good question, +1. See my answer for an explanation how a solution works and for a complete code example for the .Net XslCompiledTransform XSLT processor. :) Commented Jan 3, 2011 at 17:03

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Mac: No, you need this only if you need to pass an external parameter (data that is not in the XML document) to a transformation.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.