How to read a query string with XSL

Posted on 2004-08-09
Last Modified: 2012-05-07
I need to get a value from a query string and use it in an xsl:if statment.  How is this done.  I am running my app on Apache.
Question by:bzak
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 26

Expert Comment

ID: 11753831
I'm not sure this is possible directly in the XSLT with XSLT processors that would run on Apache, but here's one way to do it using MSXML:

The "right" way to do it is to pass the query string in server-side as a parameter to the transform.  But this would require some sort of server-side scripting/programming language, which you haven't mentioned.   Here's how I've done it client-side, using IE6:

function getParam(strParam)
      strQueryString = decode(;
      iTerm = strQueryString.indexOf(strParam + "=")
      if (iTerm != -1)
            strSubQuery = strQueryString.substring(iTerm + strParam.length + 1)
            iTermEnd = strSubQuery.indexOf("&")
            if (iTermEnd == -1)
                  iTermEnd = strSubQuery.length
            strSubQuery = strSubQuery.substring(0, iTermEnd)
            strSubQuery = null
      return strSubQuery
// URL Decodes the string
function decode(str) {
     return unescape(str.replace(/\+/g, " "));

Then when calling the transform, you add the parameter to the XSLT like:

xslProc.addParameter("pMyParam", getParam('foo'));

Mike Sharp

Author Comment

ID: 11754000
How do I use:

xslProc.addParameter("pMyParam", getParam('foo'));

in an xsl:if statement?
LVL 26

Expert Comment

ID: 11754222

xslProc.addParameter("pMyParam", getParam('foo'));

sets a parameter in the XSLT like:

<xsl:param name="pMyParam"/>

so that during transformation, the parameter functions as if it was created like:

<xsl:param name="pMyParam">bar</xsl:param>

Which is used like (for example, assuming the querystring was foo=bar):

<xsl:if test="$pMyParam = 'bar'">foo equals bar</xsl:if>

For a complete example of setting parameters during transformation, look at my dynamic filter and sort example:

This sets nodelists as parameters, as well, which has interesting implications.

Mike Sharp
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

LVL 26

Expert Comment

ID: 11754253
In any case, you'll need to use whatever flavor of a template processor you have available to Xalan or whatever.  Just make sure it has the ability to set parameters externally.

Mike Sharp

Author Comment

ID: 11754444
Ok, now for the tricky part....

I am importing two templates into a master....

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
<xsl:import href="fpForm_ST.xsl"/>
<xsl:import href="fpTemplate.xsl"/>

The query string variable needs to be read by fpTemplate.xsl.  This does not work.  It only works if I remove the import statement for fpForm.xsl.

Author Comment

ID: 11754943
Actually I was able to get it to work with your example:

<xsl:stylesheet version="1.0" xmlns:xsl=""
  <xsl:output method="html" indent="yes" encoding="iso-8859-1"/>
   <xsl:variable name="xmlpath" select="url:findURL(.)" />
  <xsl:variable name="search">
    <xsl:if test="contains($xmlpath,'?')">
      <xsl:value-of select="substring-after($xmlpath,'?')" />
  <msxsl:script language="JScript" implements-prefix="url">
    function findURL(nodelist)
      return nodelist.nextNode().url;

      <link href="style.css" rel="stylesheet" type="text/css" />
        <xsl:for-each select="planName">
          <xsl:text>The search string is: </xsl:text>
          <xsl:value-of select="$search" />

Bu I am not sure how to cut up the string....
I am getting:

The search string is: test1=Test&test2=test

I want to use the value of test1 in the if statement
LVL 26

Accepted Solution

rdcpro earned 350 total points
ID: 11755585
This should work:

<xsl:value-of select="substring-before(substring-after($search, 'test1'), '&amp;')"/>

Mike Sharp

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Path of Workbook 3 87
Detect file exist or not 3 202
Configure a Bean in an XML file 4 49
DTD and JAVA versions 1 32
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

733 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