Page 1 of 1

Add more functions to the XPath builder

Posted: Wed Jul 24, 2019 5:44 pm
by Oleksii
Is it possible? For instance, I'd like to ads this one : http://www.xsltfunctions.com/xsl/funct ... match.html or any own function.
I checked out the options of the XPath builder and the documentation: https://www.oxygenxml.com/doc/versions/ ... lbar.html but it only says that if the function is not implemented you get a warning.

best Oleksii

XML Editor 20.1, build 2018122403

Re: Add more functions to the XPath builder

Posted: Thu Jul 25, 2019 10:17 am
by Martin Honnen
If you use XQuery instead of XPath in the builder then you can use and import other XQuery modules with functions I think:

Code: Select all

import module namespace functx = "http://www.functx.com" at "http://www.xqueryfunctions.com/xq/functx-1.0-doc-2007-01.xq";

functx:substring-before-match('abc-def-ghi', '[dg]')
As for pure XPath, with XPath 3 and later (and support for higher-order functions/function variables) you can write code like

Code: Select all

let
  $substring-before-match := 
      function($arg as xs:string? ,  $regex as xs:string )  as xs:string { tokenize($arg,$regex)[1] }
return $substring-before-match('abc-def-ghi', '[dg]')
I don't know whether the oXygen editor allows you to add/declare functions outside the builder window as long as you use pure XPath.

Re: Add more functions to the XPath builder

Posted: Thu Jul 25, 2019 11:09 am
by Radu
Hi Oleksii,

In addition to what Martin replied, you seem to want to call in the XPath a function implemented in XSLT content. We do not have the APIs available in the Saxon XSLT processor we use for running the XPath to provide for such a feature.

Regards,
Radu

Re: Add more functions to the XPath builder

Posted: Thu Jul 25, 2019 12:18 pm
by Martin Honnen
If you use Saxon EE for XPath (probably requires 9.8 or later) then you could also try to use the function "load-xquery-module":

Code: Select all

let $functxNs := 'http://www.functx.com',
$functx := load-xquery-module($functxNs, map { 'location-hints' : 'http://www.xqueryfunctions.com/xq/functx-1.0-doc-2007-01.xq' }),
$substring-before-match := $functx?functions (QName($functxNs, 'substring-before-match'))?2
return $substring-before-match('abc-def-ghi', '[dg]')
That is still importing the XQuery library of functx, if you want to use the XSLT library I am not sure there is a direct way in pure XPath, in XQuery you could import or include it and use the "transform" function to run the function:

Code: Select all

let $functxNs := 'http://www.functx.com',
    $functxLoc := 'http://www.xsltfunctions.com/xsl/functx-1.0-doc-2007-01.xsl',
    $libPackage := <xsl:package
        name="http://example.com/import-functx"
        package-version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="3.0"
        xmlns:functx="http://www.functx.com">
        <xsl:expose
            component="function"
            names="functx:*"
            visibility="public"/>
        <xsl:import
            href="{$functxLoc}"/>
    </xsl:package>
return
    transform(
    map {
        'stylesheet-node': $libPackage,
        'delivery-format': 'raw',
        'initial-function': QName('http://www.functx.com', 'substring-before-match'),
        'function-params': ['abc-def-ghi', '[dg]']
    })?output
Tested with Saxon 9.8 and 9.9 for XQuery.

Re: Add more functions to the XPath builder

Posted: Mon Jul 29, 2019 5:37 pm
by Oleksii
Hello Martin,

thank you for the examples. I saved the code for the xPath "load-xquery-module" as favorite in the builder, so I can recall it when I need.
Originally, the question appeared when I was writing an XSLT and wanted to use a functx:function and to check in the builder if the concept will work as I expect. Currently, I am not familiar with the differences in this library for XQuery / XPath, but I will consider it when testing.

best Oleksii