5

Following XLST code works fine :-

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0">
<xsl:template match="/">
    <xsl:for-each select="bookstore/book">
        <xsl:if test="starts-with(author, 'W')">    <!-- Line 1 -->
            <xsl:value-of select="title" />
            &#160; by 
            <xsl:value-of select="author" />
            <br/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Here I am directly using XPath String function starts-with() in Line 1.

Now, as per W3Schools, adding the namespace for XPath functions (http://www.w3.org/2005/xpath-functions), the following code does not work :-

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:fn="http://www.w3.org/2005/xpath-functions" version="1.0">
<xsl:template match="/">
    <xsl:for-each select="bookstore/book">
        <xsl:if test="fn:starts-with(author, 'W')">  <!-- Line 2 -->
            <xsl:value-of select="title" />
            &#160; by 
            <xsl:value-of select="author" />
            <br/>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Here, I am using the XPath function with its prefix attached to the namespace.

IE shows that "Error: Namespace 'http://www.w3.org/2005/xpath-functions' does not contain any functions" I checked the URL and it does have functions.

Where am I going wrong? And if I can use all XPath functions with Transform URL itself, then why is a separate URL for XPath functions is provided?

Developer
  • 231
  • 4
  • 19
whitehat
  • 2,381
  • 8
  • 34
  • 47

2 Answers2

2

It's because IE uses MSXML which supports XPath 1.0 only. In XPath/XSLT 1.0 there is no need to prefix standard XPath functions.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • I tried the code with Firefox. But it shows "An unknown XPath extension function was called". And with Chrome, none of the code seems to work. – whitehat Mar 20 '12 at 06:45
  • @HiralJhaveri, With prefix or without? – Kirill Polishchuk Mar 20 '12 at 06:48
  • "Without prefix" the code works fine with Firefox. But with prefix [fn:start-with()] it shows the above mentioned error. – whitehat Mar 20 '12 at 06:50
  • @HiralJhaveri, It's because Firefox doesn't support XSLT 2.0. You should avoid usage of prefixes in XPath functions. – Kirill Polishchuk Mar 20 '12 at 06:54
  • OK. And [this](http://stackoverflow.com/questions/6282340/which-browsers-support-xslt-2-0-already) says no browsers supports XSLT 2.0 as answered by Martin. So we would directly use all XPath functions without any explicit namespace for it. – whitehat Mar 20 '12 at 06:59
  • 2
    Even in XSLT 2.0, there is never a need to use the fn: prefix. It's only there for XQuery, and even there, it's not needed unless you prefer to make some different function library the default. – Michael Kay Mar 20 '12 at 09:52
2

Now, as per W3Schools, adding the namespace for XPath functions (http://www.w3.org/2005/xpath-functions), the following code does not work :-

...

Here, I am using the XPath function with its prefix attached to the namespace.

IE shows that "Error: Namespace 'http://www.w3.org/2005/xpath-functions' does not contain any functions"

Try to avoid "w3schools". See why at: http://w3fools.com/ .

The F & O namespace you attempted to use was created many years after the W3C XPath 1.0 and XSLT 1.0 recommendations were published. It only relates to XPath 2.0 functions and this namespace is unknown to XPath 1.0/XSLT 1.0 processors.

Even when using XPath 2.0/XSLT 2.0 it isn't necessary to use the namespace -- any non-prefixed function names are considered to be in this namespace.

Solution:

Simply don't prefix any standard XPath functions.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431