How to use xpath-functions in xslt stylesheets

Poster Content
nk4um User
Posts: 89
August 27, 2010 05:26Use XSLT 2.0 and its accessor
Hi, Mircea,

You seem to be trying to use XPath 2.0 functions in a XSLT 1.0 stylesheet. That won''t work, you''ll either have to use EXSLT functions or switch to XSLT 2.0. XSLT 2.0 is much easier for many tasks then XSLT 1.0, so that would be my advice.

Here is an XSLT 2.0 version of your example:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:template match="resultset">
        <expenses>
            <xsl:attribute name="total" select="max((1,2,3))"/>
            <xsl:apply-templates/>
        </expenses>
    </xsl:template>

</xsl:stylesheet>


Please notice the following changes:
- stylesheet version is now 2.0
- there is no need to declare the fn namespace prefix, the XSLT 2.0 processor will use that as default
- the max function takes a sequence, so in this case you need an extra set of brackets (you won''t need those of you select a nodeset using XPath expression)

You''ll need to use the XSLT 2.0 accessor in NK (see http://docs.netkernel.org/book/view/book:xml:saxon:book/doc:xml:saxon:xslt2 for NK4, for NK3 I couldn''t find a public website anymore but you can try http://localhost:1060/book/developerreference/doc_ura_xslt2 (notice it''s in the xquery module)).

If you''ve to stick to XSLT 1.0 you find information about EXSLT here: http://www.exslt.org/. It depends a bit on the XSLT processor of some of these functions are already by default available. I''m sorry I haven''t been using XSLT 1.0 for a long time, so I don''t know the current status ...

Hope this helps,

Menzo
nk4um User
Posts: 26
August 27, 2010 04:13How to use xpath-functions in xslt stylesheets
Hi,

I try to use xpath-functions in one of my xslt file in a NK4 module.

My xslt declares the "fn" namespace like this:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nk="http://1060.org/netkernel"
                        xmlns:fn="http://www.w3.org/2005/xpath-functions">


In the xslt file I have a template like this:
   <xsl:template match="resultset">
      <expenses>
         <xsl:attribute name="total">
            <xsl:value-of select="fn:max(1,2,3)"/>
         </xsl:attribute>
         <xsl:apply-templates/>
      </expenses>
   </xsl:template>


Note: Precisely, I need the "sum" function, so instead of <xsl:value-of select="fn:max(1,2,3)"/> above I''ll use:
<xsl:value-of select="fn:sum(//AMOUNT)"/>


NetKernel reports errors like:
(Location of error unknown)java.lang.NoSuchMethodException: For extension function, could not find method org.apache.xml.utils.NodeVector.su
m([ExpressionContext,] ).

or
(Location of error unknown)java.lang.NoSuchMethodException: For extension function, could not find method java.lang.String.sum([ExpressionCo
ntext,] ).

or
(Location of error unknown)java.lang.NoSuchMethodException: For extension function, could not find method java.lang.Double.sum([ExpressionCo
ntext,] #NUMBER, #NUMBER).


depending on what fn functions I try to use.

Is it possible to use xpath-functions in NetKernel? If yes, what library do I need to add and where?

Thanks,
Mircea