Solved

How to read a query string with XSL

Posted on 2004-08-09
7
604 Views
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.
0
Comment
Question by:bzak
  • 4
  • 3
7 Comments
 
LVL 26

Expert Comment

by:rdcpro
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:

http://dev.rdcpro.com/Members/rdcpro/snippets/xsltquerystrings/

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(window.location.search);
      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)
      }
      else
      {
            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'));



Regards,
Mike Sharp
0
 

Author Comment

by:bzak
ID: 11754000
How do I use:

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

in an xsl:if statement?
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 11754222
This:

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:

http://rdcpro.com/Members/rdcpro/snippets/filterandsort/

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

Regards,
Mike Sharp
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 26

Expert Comment

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

Regards,
Mike Sharp
0
 

Author Comment

by:bzak
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="fpForm_ST.xsl"/>
<xsl:import href="fpTemplate.xsl"/>
</xsl:stylesheet>

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.
0
 

Author Comment

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:url="uri:find-url.org"
  exclude-result-prefixes="msxsl">
 
  <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,'?')" />
    </xsl:if>
  </xsl:variable>
  <msxsl:script language="JScript" implements-prefix="url">
    function findURL(nodelist)
    {
      return nodelist.nextNode().url;
    }
  </msxsl:script>

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

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
0
 
LVL 26

Accepted Solution

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

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

Regards,
Mike Sharp
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RSS Feed Enclosure URL 1 127
Data at the root level is invalid. Line 1, position 1 8 148
Create XML 5 49
XSL FO - text wrapping and empty spaces 1 35
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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