4
votes

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?

2

2 Answers

1
votes

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.

2
votes

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.