1

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?

5
  • 2
    The XSL cannot by itself access URL variables - you would need to pass these in manually. What platform are you running XSL on? In PHP, for example, you'd use $process->setParameter. Then, as long as your XSL declared the variable, it would receive it, and you could reference it. Commented Jun 8, 2012 at 8:19
  • thanks for the reply! I am doing XML to HTML conversion using XSLT. I want to have 2 html files for which I am using Saxon. In the first file I have link and when I click on link, a value is sent to the url. For doing this I am using - <a href="index.html?value={$cd}"> Now when I click on the link I am able to get the value in url and now I need to use the value from the url in my XSLT to display other details. Commented Jun 8, 2012 at 8:30
  • 1
    That is not a URL, it is a Windows filename. Commented Jun 8, 2012 at 8:37
  • but still how to get the value from it. If I am running the files on my system. Commented Jun 8, 2012 at 8:40
  • 1
    I'm still unclear on what platform you're doing all this on. If it's Saxon, are you using Java or .NET? Or do you mean you're doing the XSL via JavaScript? Whichever one, you need to retrieve the URL vars and then manually pass them in to your XSL (in the case of JavaScript, this is not always possible, depending on browser). XSL cannot normally by itself access URL vars - they must be passed in. Here's a PHP example. Commented Jun 8, 2012 at 10:36

1 Answer 1

1

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, '?'), '&amp;'), '=')

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, '?'), '&amp;')
         [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
Sign up to request clarification or add additional context in comments.

9 Comments

i am still unclear how to do in this case - C:/Documents and Settings/Saxon/output1/index.html?value=65abc. I just need this part '65abc' from the url path
@user1402867: Don't you know how to use <xsl:value-of>?
i know how to use <xsl:value of> I did something like this - <xsl:variable name="pPath">output1/output3/index.html?value</xsl:variable> <xsl:variable name="temp" select="substring-after(substring-before(substring-after($pPath, '?'), '&amp;'), '=')" /> When I use this and run it. I don't get any output.
@user1402867: I have updated this answer with complete code samples.
@user1402867: I have done a series of edits to make the code formatted and readable.
|

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.