How can you use xpath-functions with XSLT 2.0?

12,970

The problem is that you are using an XSLT 1.0 processor and an XSLT 1.0 processor doesn't know (and must not know) anything about XPath 2.0 functions.

If you use a real XSLT 2.0 processor, you don't even have to specify the function namespace -- it is a default namespace for any unprefixed function name.

For example, this XSLT 2.0 transformation:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
    <xsl:sequence select="document-uri(.)" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:sequence select="document-uri(document(''))" />
 </xsl:template>
</xsl:stylesheet>

when executed with Saxon 9.1.5 under the XSelerator, produces correctly the URLs of the source XML document and the stylesheet itself:

file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml
file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xsl
Share:
12,970
bitmask
Author by

bitmask

I am a computer scientist, enthusiast programmer, and activist. Accept policy If you wonder why I didn't accept your answer, although it is obviously the right thing: Usually, I like to wait at least an hour before accepting anything, often I wait a day or so. More random information about me kernel: linux distro: debian interface: xmonad + mate text: vim web: firefox mail: thunderbird programming: mainly c++, some python, occasionally haskell, svg speak: de, en, es (read-only), limited support: fr, ru rcs: git scifi: The Matrix, Star Trek, Doctor Who, Firefly First to earn the the-matrix bronze badge on scifi.SE. Flair Bad Wolf

Updated on June 04, 2022

Comments

  • bitmask
    bitmask almost 2 years

    In order to use xpath-functions (specifically the fn part), I included the respective namespace into my xslt stylesheet, like so:

    <xsl:stylesheet
      version="2.0"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fn="http://www.w3.org/2005/xpath-functions"
    >
    

    As specified by W3C.

    However, when I use fn:document-uri, my XSLT engines tell me I called an unknown function/extension:

      <xsl:variable name="uri" select="fn:document-uri()" />
    

    Opera says:

    This document had an invalid XSLT stylesheet. Error message from the XSLT engine:
    Error: XPath expression compilation failed: fn:document-uri()
    Details: compilation error (characters 1-17, "fn:document-uri()"): unknown function called: '{ http://www.w3.org/2005/xpath-functions, document-uri }'

    Firefox says:

    Error during XSLT transformation: An unknown XPath extension function was called.

    And xsltproc refuses transformation, because of xslt 2.0.

    So, how do I specify the fn namespace properly?