user1402867
user1402867

Reputation: 189

How to dynamically fetch value from url in XSLT

I have a url of type C:/Documents and Settings/Saxon/output1/index.html?value=65abc

Now i need to fetch this part from url '65abc' in my xslt. I am getting this value from the previous page when click on a link.

Any idea of how to do it?

Upvotes: 1

Views: 4718

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

Use:

substring-after($pPath, '=')

where $pPath is a reference to the global external xsl:param that contains the value of the url-like) file path, passed from the invoker of the transformation.

In case that pPath contains more than one query-string parameter and you want to access the value of first one, then use:

substring-after(substring-before(substring-after($pPath, '?'), '&'), '=')

If you are using XSLT 2.0 (XPath 2.0), then you can access the value of the the query-string-parameter named $pQName using:

 substring-after
   (tokenize(substring-after($pPath, '?'), '&')
         [starts-with(., concat($pQName, '='))],
   '='
   )

Here are complete code examples:

  1. The simplest case:

. . .

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="pPath" select=
     "'C:/Documents and Settings/Saxon/output1/index.html?value=65abc'"/>

 <xsl:template match="node()|@*">
     <xsl:sequence select="substring-after($pPath, '=')"/>
 </xsl:template>
</xsl:stylesheet>

when this is applied on any XML document (not used), the wanted result is produced:

65abc

.2. When this transformation is performed on any XML document (not used):

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="pPath" select=
     "'C:/Documents and Settings/Saxon/output1/index.html?value=65abc&amp;x=1&amp;y=2'"/>
  <xsl:param name="pQName" select="'x'"/>   

 <xsl:template match="node()|@*">
     <xsl:sequence select=
     "substring-after
       (tokenize(substring-after($pPath, '?'), '&amp;')
             [starts-with(., concat($pQName, '='))],
      '='
      )"/>
 </xsl:template>
</xsl:stylesheet>

the wanted string (the value for the query-string parameter named x) is produced:

1

Upvotes: 1

Related Questions