2

is it possible to access a query string using xslt?

i have a url e.g

www.example.com/page.aspx?k=aa&lang=en

I want to do something like

if lang = en

<div>displaly stuff</div>

else

 <div>display other stuff</div>

can you show me how to do this using xslt?

1
  • The currently accepted answer is not correct. No extension function is required to access a query-string. It can be passed as a (global) parameter. This is to be preferred as it reduces the need for extensions and results in cleaner and more readable code. Commented May 1, 2009 at 17:00

3 Answers 3

8

is it possible to access a query string using xslt?

Yes, if the query string is passed as a parameter.

The code below shows that no extension function is required to access a query-string. It can be passed as a (global) parameter. This is to be preferred as it reduces the need for extensions and results in cleaner and more readable code.

Then one can perform tokenization (with the tokenize() function in XSLT 2.0 or in XSLT 1.0 using the str-split-to-words template of FXSL 1.x or a self-written recursive tokenization template.)

XSLT 1.0 solution:

<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="pQString" select=
     "'?login=userId&amp;tag=XSLT&amp;lang=en&amp;level=expert'"
     />


    <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="vLang" select=
      "substring-after(ext:node-set($vwordNodes)/*
                             [starts-with(.,'lang=')]
                               [last()],
                       'lang='
                      )
      "/>

      <xsl:value-of select="concat('lang = ', $vLang)"/>
    </xsl:template>
</xsl:stylesheet>

when the above transformation is applied on any XML document (will not be used), the wanted result is produced:

lang = en

Do note the use of the FXSL 1.x str-split-to-words template and the use of the EXSLT ext:node-set() extension function.

XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

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

     <xsl:param name="pQString" as="xs:string" select=
     "'?login=userId&amp;tag=XSLT&amp;lang=en&amp;level=expert'"
     />

    <xsl:template match="/">
      <xsl:variable name="vLang" as="xs:string" select=
      "substring-after(
                       tokenize($pQString, '\?|&amp;')
                                 [starts-with(.,'lang=')]
                                    [last()],

                       'lang='
                       )
      "/>

      lang = "<xsl:sequence select='$vLang'/>"
    </xsl:template>
</xsl:stylesheet>

When the above XSLT 2.0 transformation is performed, it produces the correct result:

  lang = "en"
Sign up to request clarification or add additional context in comments.

Comments

1

Not in native XSLT no but you can write extension objects that handle complex functions that are outside of the scope of native XSLT so for example in your XSLT you can insert a namespace into the stylesheet such as

xmlns:ex="my:Qs"

and then call

<xsl:variable name="qs" select="my:Qs('parameterName')"/>

Comments

1

No, because XSLT is not compiled code that runs on its own. It is a technology for transforming XML data and it is invoked by some XSLT processor.

So, it is up to you to provide the necessary variable data as parameters to your XSLT processor.

4 Comments

Actually, the statement " XSLT is not compiled code" is not always true. There are XSLT processors, such as the .NET XslCompiledTransform class that produced compiled CLR byte-code before the transformation is executed. Saxon has also an option for producing compiled (JVM( byte-code. And this has little to do with the OP's question.
Whether an XSLT transformation "runs by itself" has nothing to do with the question if and how it can access a query-string.
@Dimitre: I disagree. I think it is crucial point. And that's what your answer says too. The value MUST be passed as a parameter. An XSL file cannot load querystrings from a web request by itself.
Yes, but passing the value as a parameter occurs both with compiled stylesheets and with interpreted ones. It doesn't matter if the code is compiled or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.