?
Solved

How to get a URL-parameter in XSLT

Posted on 2005-04-09
5
Medium Priority
?
2,071 Views
Last Modified: 2012-06-27
I have the following xslt-stylesheet, called parameter.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="parameter" select="0" />
                  
<xsl:template match="/">
      <html>
            <head>
                  <title>URL Parameter</title>
                  <script language="Javascript" src="scripts.js">
                  </script>
            </head>
            <body>
                  <p>
                        ID: <xsl:value-of select="$parameter"/>
                  </p>
            </body>
      </html>
</xsl:template>

</xsl:stylesheet>

I use the following xml-page (parameter.xml) to call this stylesheet. The xml-data is not relevant here because it's not used in the stylesheet.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="parameter.xsl"?>

<test>
      test
</test>

I have a javascript function called getURLParam() that returns the value of the parameter.

function getURLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
   if ( strHref.indexOf("?") > -1 )
  {
    var strQueryString =  strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
      {
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 )
        {
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

Using the url http://localhost/parameter.xml?id=12345, getURLParam(id) would return 12345. How do I put the id-value in the $parameter variable?
0
Comment
Question by:grexx
  • 3
5 Comments
 
LVL 8

Expert Comment

by:anthonywjones66
ID: 13744572
Assuming IE the below ripped from MSDN XML DOM documentation:-

var xslt = new ActiveXObject("Msxml2.XSLTemplate");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
var xslProc;
xslDoc.async = false;
xslDoc.resolveExternals = false;
xslDoc.load("parameter.xsl");
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
xmlDoc.load("parameter.xml");
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.addParameter("parameter", getURLParam(id));
xslProc.transform();

var sHTML = xslProc.output

Of course in this case test.xml is superflous, it's stylesheet processing instruction is ignored hence you can change this line above:-

xmlDoc.load("parameter.xml");

to this:-

xmlDoc.loadXML("<anyoldthing />");

thereby eliminating parameter.xml

Anthony.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13746947
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:user="http://mycompany.com/mynamespace"
     exclude-result-prefixes="user msxsl">

<msxsl:script language="JScript" implements-prefix="user">
<![CDATA[

function getFilter(node)
{
     
     //return typeof(node.nextNode().ownerDocument);
     var url = node.nextNode().url;
     var value = "title";
     
     if (url.indexOf("?") != -1)
     {
          value = url.substring(url.indexOf("?") + 1);
     }
     return value;
     
}


function getUrl(node)
{
     var url = node.nextNode().url;
     if (url.indexOf("?") != 1)
     {
          url = url.substring(0,url.indexOf("?"));
     }
     return url;
}
]]>
</msxsl:script>

<xsl:template match="/">
<xsl:variable name="name" select="user:getFilter(.)" />
<xsl:variable name="url" select="user:getUrl(.)" />

</xsl:template>


</xsl:stylesheet>
0
 
LVL 1

Author Comment

by:grexx
ID: 13844013
Sorry for my late reply. I see both replies apply to Internet Explorer. I need it to work cross-browser. I was wondering whether I could change the xml instead of the xslt? And is that simpler? It's no problem to define an extra parameter in the xml if that's easier to update via the url.

The xml would then be something like:

<test>
      <parameters>
            <id>1234</id>
      </parameters>
</test>

So is it possible to update the id-value in the browser? If you can provide a completely worked out example that works in Firefox 1 and IE6, I'll double the points to 500. The example doesn't necessarily have to match my example. And it doesn't matter if it's via xml or xslt.
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 1000 total points
ID: 13844154
xml
==
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="data14.xsl"?>
<test>
     <parameters>
          <id>1234</id>
     </parameters>
</test>

xslt
===
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:variable name="parameter" select="//parameters/id" />
<xsl:template match="/">
     <html>
          <head>
               <title>URL Parameter</title>
               <script language="Javascript" src="scripts.js"></script>
          </head>
          <body>
               <p>
                    ID: <xsl:value-of select="$parameter"/>
               </p>
          </body>
     </html>
</xsl:template>
</xsl:stylesheet>

 

tested and works wih both MSIE 6 and Firefox 1.03
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question