We help IT Professionals succeed at work.

XSLT function example

vrosas_03
vrosas_03 asked
on
Medium Priority
483 Views
Last Modified: 2012-05-12
can someone provide me with a working example xsl file with a user defined function writen in XSLT 2.0
along with the corresponding xml file it manipulates?
Comment
Watch Question

Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
With the below stylesheet

this XML
<root>foo</root>

will become

"faa"

The function replaces every "o" with a "a"

to note with functions
- you need to declare a namespace for the functions
- number of arguments are fixed
- no implicit passing of context as with named templates
- default values for function parameters are not allowed
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:pfn="urn:private:xslt:functions"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="root">
        <xsl:value-of select="pfn:o-to-a(.)"/>
    </xsl:template>
    <xsl:function name="pfn:o-to-a">
        <xsl:param name="arg"/>
        <xsl:value-of select="translate($arg, 'o', 'a')"/>
    </xsl:function>
    
</xsl:stylesheet>

Open in new window

Author

Commented:
I added a line to  the top line so it could reference the xsl file

<?xml-stylesheet href="ee.xsl" type="text/xsl"?>
<root>foo</root>

But I got this errror.  
Error during XSLT transformation: An unknown XPath extension function was called.
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
an xml stylesheet reference is only supported in browsers,
browsers only support XSLT1, not XSLT2
You need an XSLT2 processor (such as www.Saxonica.com) in order to use XSLT2 functionality

Author

Commented:
I think I read that there is a way to simulate a function in XSLT1.  
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
There are ways, but

let me first repeat your question
"with a user defined function writen in XSLT 2.0"
you explicitely asked for XSLT2 functionality

There are various options for emulating functions in XSLT1
- you can write a named template, if the required functionality of the "function" is XSLT1 supported
- you can have extension functions to a processor
- you can use a different language inside your processor to write the function

the second and third option are processor specific, so can't be reused with other processors
and are not at all recommended if you want to use this in a browser

Author

Commented:
Thanks for the extra info,  I assumed it would be supported by now since a lot of the tutorials were written a long time ago.  Also I wasn't sure because I couldn't find the info anywhere.  Now I can focus on using the other method
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
welcome,

XSLT2 has been there for ages, but adoption in processors is really slow, for many reasons
- building an XSLT2 processor is extremely difficult and takes a large amount of time if you start from teh XSLT1 processor source code
- there is an extremely good processor already available, everyone uses it
- browser vendors look at eachother, if noone does it, it does make sense to build one, since web developers expect interoperability between browsers, noone moves, so noone will ever move
- database vendors have a main interest in XML, but they point their energy at XQuery, not XSLT for now

A lot of articles out there on the web name XSLT and XPath functionality but fail to mention version specifics (w3schools fails for years in that aspect)
and XSLT experts tend to only discuss XSLT2 and forget about the many XSLT users in browsers and other platforms that only support XSLT1

Explore More ContentExplore courses, solutions, and other research materials related to this topic.