Wiki source for XPathExtension


Show raw source

======XPath Extensions======

With Version 0.1.0.0 an XPath extension is available in xmlsh which allows calling into xmlsh from with any xpath context including xpath, xlst, xquery, and the builtin <[ ... ]> commands.



The namespace "xmlsh" is **predeclared** for the XML expression syntax <[ ... ]> as well as the [[CommandXQuery xquery]], [[CommandXPath xpath]] and [[CommandXslt xslt]] commands.

For example, the following works without using an explicit declaration
%%
echo <[ <foo/>/xmlsh:eval("xcat") ]>
%%

Similary the syntax to xquery is supported
%%
xquery -n -q 'xmls:eval("echo hi")'
%%



====Extension functions implemented====

===eval===

The **eval** xpath function evaluates its first argument as an xmlsh string (identical to the [[CommandEval eval]] command) and optionally its second argument as positional parameters. The return value of the eval function is the standard output of the command.

Examples
%%
xecho <[ xmlsh:eval("xecho $*" , ("foo" , <bar/> , 1 ) ) ]>
xquery -n 'xmlsh:eval("xls")'
xpath -n 'xmlsh:eval("xls $*" , "*.xml")'
var=<[ xmlsh:eval("xls") ]>
%%

===Calling XSLT from XQuery. ===

An interesting use of eval is to call xslt from within xquery or visa-versa. The context is passed to eval as the stdin and the output of the command becomes the result value of the expression. These examples are in xquery either via the <[ ]> syntax or the [[CommandXquery xquery]] command and require calling xquery from xmlsh to activate the extension function.

Example, in an XQuery expression, or file run from xmlsh
%%
/foo/bar/xmlsh:eval("xslt -f style.xsl")/spam
%%
calls "xslt" with the sytlesheet "style.xsl" passing the input context from /foo/bar as the input and then the output is further evaluated to extract the /spam child

An alternative of the above which can use variables for the stylesheet
%%
let $style := "style.xsl"
return
/foo/bar/xmlsh:eval("xslt" , ("-f" , $style) )/spam
%%

An alternative which passes the input context as a third argument
%%
let $style := "style.xsl"
return
xmlsh:eval("xslt" , ("-f" , $style), /foo/bar )/spam
%%


Valid XHTML :: Valid CSS: :: Powered by WikkaWiki