How can you use xpath functions with XSLT 2.0?

In order to use the xpath-functions (specifically the part fn

) I have included the appropriate namespace in the xslt stylesheet like

<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 stated by the W3C.

However, when I use fn:document-uri

, my XSLT engines tell me that I have 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 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:

XSLT transformation failed: An unknown XPath extension function was called.

And xsltproc

refuses conversion because of xslt 2.0.

So how do I define the namespace correctly fn

?

+3


source to share


2 answers


The problem is that you are using an XSLT 1.0 processor and the XSLT 1.0 processor does not (and should not) know anything about XPath 2.0 features .

If you're using a real XSLT 2.0 processor, you don't even need to specify the function namespace — this is the default namespace for any unlisted 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 XSelerator, it correctly renders the URLs of the original XML document and the stylesheet itself :

file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xml
file:/C:/Program%20Files/Java/jre6/bin/marrowtr.xsl

      

+4


source


you can use xpath function in php please refer to this link http://php.net/manual/en/simplexmlelement.xpath.php i think this example is helpful for you



-3


source







All Articles