declare own functions
Added by Anonymous over 19 years ago
Legacy ID: #3219478 Legacy Poster: pfc_caw (pfc_caw)
Hi! I want to declare my own function to use it in xpath expressions like this: //img[toolong_alt(100,@alt)] define function toolong_alt ($num as xs:decimal, $alt as xs:node) as xs:boolean { if (string-length($alt) > $num) then true() else false() } i've been having a look at javadoc & examples and i havent seen how to do it. Could you help me? thanks a lot
Replies (1)
RE: declare own functions - Added by Anonymous over 19 years ago
Legacy ID: #3219977 Legacy Poster: Michael Kay (mhkay)
You can do this in XQuery - your syntax is a little out of date: declare function local:toolong_alt ($num as xs:decimal, $alt as node()) as xs:boolean { if (string-length($alt) > $num) then true() else false() } //img[local:toolong_alt(100,@alt)] or more concisely declare function local:toolong_alt ($num as xs:decimal, $alt as node()) as xs:boolean { string-length($alt) > $num } You could also write the function in XSLT. However, you can't define functions in XPath itself, and you can't call functions written in XSLT or XQuery from an XPath expression invoked using the XPath API. I suggest you use XQuery instead. Michael Kay
Please register to reply