JaXL
JaXL

Reputation: 73

XSLT: Passing URL querystring as a parameter

I know this is an old question that has been passed around SO several times but I was wondering whether anyone can expand on whether a URL that has a querystring attached to it can be stripped out via XSLT 1.0 and can be used as a parameter for later use of the XSLT transformation.

For example, I have a URL of http://www.mydomain.com/mypage.htm?param1=a&param2=b

via XSLT, I am looking for a result of something along the lines of:

<xsl:param name="param1">a</xsl:param> and <xsl:param name="param2">b</xsl:param>

where both parameter name (param1, param2) and it's value (a, b) has been extracted from the quesrystring to allow me to use $param1 and $param2 later on say in an if condition

e.g. <xsl:if test="$param1 = 'a'> comes out true but if we use <xsl:if test="$param1 = 'b'> comes out false.

I have seen a similar question here: Retrieve page URL params or page URL in XSLT which uses the str-split-to-words template but I have unsuccessfully got it working (possibly due to me implementing it the wrong way) so any working examples of how it can be done in practice would be massively beneficial.

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://exslt.org/common">
<xsl:import href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/strSplit-to-Words.xsl"/>
<xsl:output indent="yes" method="html"/>

<xsl:template match="/">
<xsl:variable name="vwordNodes">
  <xsl:call-template name="str-split-to-words">
    <xsl:with-param name="pStr" select="$pQString"/>
    <xsl:with-param name="pDelimiters" select="'?&amp;'"/>
  </xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
</xsl:template>

<xsl:template match="word">
  <xsl:value-of select="."/>
  <xsl:text>&#xA;</xsl:text>
</xsl:template>

</xsl:stylesheet>

Upvotes: 2

Views: 8913

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

There are a few problems in your code:

  1. <xsl:import href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/strSplit-to-Words.xsl"/> I doubt that the wanted stylesheet can be imported directly from its SourceForge view page -- especially taking into account, that it itself imports other FXSL stylesheets. The correct way to use FXSL is to download it to the local computer and reference its stylesheets off the file location it resides in at the local computer.

...

.2. <xsl:with-param name="pStr" select="$pQString"/> This will produce a compilation error because you missed to define the $pQString global/external parameter. You need to define this parameter at global level. It can be given a default value (for example a particular URL) for easier testing. However, the idea of using this parameter is that the invoker of the transformation should pass this parameter to the transformation.

.3. The results of the transformation are written to the output. While this is good for demonstration purposes, you want to be able to use these results later in the transformation. The way to do this is to capture these results in a variable, make another variable from it, with a regular tree (from its RTF type) and then reference the nodes of this last variable.

Here is an example of the code you want (provided that you have downloaded FXSL, unzipped the distribution and saved this code in the same directory as the unzipped distribution of FXSL):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
 >

   <xsl:import href="strSplit-to-Words.xsl"/>

   <xsl:output indent="yes" omit-xml-declaration="yes"/>

   <xsl:param name="pUrl" select=
   "'http://www.mydomain.com/mypage.htm?param1=a&amp;param2=b'"/>

   <xsl:param name="pQString" select=
     "substring-after($pUrl, '?')"
     />


    <xsl:template match="/">
        <xsl:variable name="vwordNodes">
          <xsl:call-template name="str-split-to-words">
            <xsl:with-param name="pStr" select="$pQString"/>
            <xsl:with-param name="pDelimiters"
                      select="'?&amp;'"/>
          </xsl:call-template>
        </xsl:variable>

       <xsl:variable name="vrtfqueryParams">
         <xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
       </xsl:variable>

       <xsl:variable name="vqueryParams" select="ext:node-set($vrtfqueryParams)/*"/>

       <xsl:value-of select="$vqueryParams/@name[. ='param1']"/>
       <xsl:text> : </xsl:text>
       <xsl:value-of select="$vqueryParams[@name = 'param1']"/>

       <xsl:text>&#xA;</xsl:text>
       <xsl:value-of select="$vqueryParams/@name[. ='param2']"/>
       <xsl:text> : </xsl:text>
       <xsl:value-of select="$vqueryParams[@name = 'param2']"/>
    </xsl:template>

    <xsl:template match="word">
      <param name="{substring-before(.,'=')}">
        <xsl:value-of select="substring-after(.,'=')"/>
      </param>
    </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document (not used in this demo), the wanted, correct result -- the query-string parameters referenced of a results variable by name -- is produced:

param1 : a
param2 : b

Upvotes: 2

Related Questions