hi,

Can any one tell me How to get url or uri from address bar dynamically in XSLT... It will helps me a lot.

Regards
Bhanu.

Dani AI

Generated

Two practical, reliable ways to get the current page URL into an XSLT stylesheet:

Declare a top-level parameter in the stylesheet and supply the URL from whatever runs the transform (most portable). This keeps the XSL clean and works the same in browsers and on servers. Example XSL snippet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:param name="pageUrl"/>
  <xsl:template match="/">
    <a href="{$pageUrl}">Current page</a>
  </xsl:template>
</xsl:stylesheet>

If the transform runs in the browser, set that parameter from JavaScript before transforming. Example with XSLTProcessor:

var proc = new XSLTProcessor();
proc.importStylesheet(xslDoc);
proc.setParameter(null, "pageUrl", window.location.href);
var frag = proc.transformToFragment(xmlDoc, document);
document.getElementById("out").appendChild(frag);

Server-side APIs can set the same parameter (portable across engines). Example with JAXP in Java:

Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource("style.xsl"));
t.setParameter("pageUrl", request.getRequestURL().toString());
t.transform(new StreamSource("input.xml"), new StreamResult(response.getWriter()));

Notes and troubleshooting: was right that embedding browser-only logic inside XSLT is possible but non-standard. Avoid vendor extensions (xsl:script / msxsl) unless running a known engine and you accept portability and security tradeoffs. If the parameter is empty, check exact name spelling, whether the host actually set it, and whether the transform method (transformToFragment vs transformToDocument) is supported by the browser. For server-side languages (PHP, .NET, Java), use the transformer API to set parameters from the request URL — this is the simplest, safest approach for and others.

Recommended Answers

All 4 Replies

You can't directly get the URL from an address bar in xslt unless it's in the input document. You can however use a javascript inside of an XSLT to get that.

Please post a sample of your input document and your desired output that you're trying to achieve.

Hi,

Thanks for the reply. I need to write code in xslt (I may use javascript or any thing that supports in xslt) to get the url from address bar can any one can tell me that...

Regards
Bhanu.

Here's the javqascript help. You integrate that into xslt using the xsl:script instruction.

No but you can mark the thread as solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.