Tell me who are you and what are you processing

| 3 Comments | No TrackBacks

This is small trick for newbies looking for a way to get URI of a source XML and the stylesheet from within XSLT stylesheet.

Unfortunately neither XPath 1.0 nor XSLT1.0 don't provide any solutions for the problem. Usual answer is "pass it as a parameter". That's a good one, but not always suitable (e.g. when transforming client side with <?xml-stylesheet?> PI). Next answer is "use Saxon's saxon:system-id() extension function or write your own". Latter is what I'm going to illustrate.

Simple, ain't it:

<xsl:stylesheet version="1.0" 

  <msxsl:script language="javascript" 
    function uri(nodelist) {
      return nodelist[0].url;
  <xsl:template match="/">
    <p>Currently processing XML document URI is 
      <tt><xsl:value-of select="ext:uri(/)"/></tt></p>
    <p>Currently processing XSLT stylesheet URI is 
      <tt><xsl:value-of select="ext:uri(document(''))"/></tt></p>
The result (try is:
Currently processing XML document URI is

Currently processing XSLT stylesheet URI is

PS. Of course extension functions are not portable and above works only in IE/MSXML3+.

PPS. Of course it only works well when XML documents are loaded from a URI, not generated on the fly.

PPPS. XPath 2.0 will fix the problem providing fn:document-uri and fn:base-uri() functions.

Related Blog Posts

No TrackBacks

TrackBack URL:


John, as you probably know Firefox 3.0 might have some support for extension functions, but currently - I think that's IE-only feature.

And about #1 - basically that is fragment identifier, so probably MSXML doesn't consider it a part of document url. Again, I don't think there is any control over it.

In your example

returns the ?1


does not return the #1

Any way to modify the script to get the #1

Do you know any tricks for getting the uri inside XSLT in other browsers than IE?

The parameters from a uri can be very useful during the XSLT transformation when processing static xml on the server.

Thank you for your very useful tip.

John Perkins uses this this tip. Major uses to come.

Leave a comment