views:

2949

answers:

3

When using a browser to transform XML (Google Chrome or IE7) is it possible to pass a parameter to the XSLT stylesheet through the URL?

example:

data.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<root>
    <document type="resume">
        <author>John Doe</author>
    </document>
    <document type="novella">
        <author>Jane Doe</author>
    </document>
</root>

sample.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"&gt;
    <xsl:output method="html" />
    <xsl:template match="/">
        <xsl:param name="doctype" />
        <html>
 <head>
                <title>List of <xsl:value-of select="$doctype" /></title>
            </head>
            <body>
                <xsl:for-each select="//document[@type = $doctype]">
                    <p><xsl:value-of select="author" /></p>
                </xsl:for-each>
            </body>
        </html>
</<xsl:stylesheet>
+2  A: 

You can generate the XSLT server-side, even if the transformation is client-side.

This allows you to use a dynamic script to handle the parameter.

For example, you might specify:

<?xml-stylesheet type="text/xsl"href="/myscript.cfm/sample.xsl?paramter=something" ?>

And then in myscript.cfm you would output the XSL file, but with dynamic script handling the query string parameter (this would vary depending on which scripting language you use).

Peter Boughton
Part two of the question would be is it possible client-side only?
A: 

Unfortunately, no - you can't pass through parameters to the XSLT on the client-side only. The web-browser takes the processing instructions from the XML; and directly transforms it with the XSLT.


It is possible to pass values via the querystring URL and then read them dynamically using JavaScript. However these wouldn't be available to use in the XSLT (XPath expressions) - as the browser has already transformed the XML/XSLT. They could only be used in the rendered HTML output.

leekelleher
+2  A: 

Just add the parameter as an attribute to the XML source file and use it as an attibute with the stylesheet.

xmlDoc.documentElement.setAttribute("myparam",getParameter("myparam"))

And the java script function is as follows:

//Get querystring request paramter in javascript
function getParameter (parameterName ) {

   var queryString = window.top.location.search.substring(1);

   // Add "=" to the parameter name (i.e. parameterName=value)
   var parameterName = parameterName + "=";
   if ( queryString.length > 0 ) {
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
         end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
   }
   // Return "null" if no parameter has been found
   return "null";
   }
}
Tawani