xPath

Description

XSH2 can evaluate XPath expressions as defined in W3C recommendation at http://www.w3.org/TR/xpath with only a little limitation on use of syntactically ignorable whitespace. (Nice interactive XPath tutorials and references can be found at http://www.zvon.org.)

In order to allow XSH2 to use white-space as a command argument delimiter (which is far more convenient to type than, say, commas), the use of white-space in XPath is slightly restricted. Thus, in XSH2, white-space can only occur in those parts of an XPath expression, that are surrounded by either brackets, square brackets, single or double quotes. So, for example, otherwise valid XPath expression like

/ foo / bar [ @baz = "bar" ]

should in XSH2 be written as either of

/foo/bar[ @baz = "bar" ]

avoiding any white-space outside the square brackets, or completely enclosed in brackets as in

( / foo / bar [ @baz = "bar" ] ).

XSH2 provides a number of powerful XPath extension functions, listed below and described in separate sections. XPath extension functions by default belong to XSH2 namespace http://xsh.sourceforge.net/xsh/ with a namespace prefix set to xsh. A program may however call the xpath-extensions command to map XSH2 XPath extension functions into the default namespace, so that they may be used directly without any prefix.

XPath extension functions defined in XSH2: xsh:doc, xsh:filename, xsh:var, xsh:matches, xsh:match, xsh:grep, xsh:substr, xsh:reverse, xsh:lc, xsh:uc, xsh:lcfirst, xsh:ucfirst, xsh:same, xsh:max, xsh:min, xsh:strmax, xsh:strmin, xsh:sum, xsh:join, xsh:subst, xsh:sprintf, xsh:serialize, xsh:parse, xsh:current, xsh:path, xsh:if, xsh:new-attribute, xsh:new-element, xsh:new-element-ns, xsh:new-text, xsh:new-comment, xsh:new-pi, xsh:new-cdata, xsh:new-chunk, xsh:map, xsh:evaluate, xsh:split, xsh:times, xsh:id2, xsh:lookup, xsh:document, xsh:documents

Example 63. Open a document and count all sections containing a subsection

xsh $scratch/> $v := open mydocument1.xml;
xsh $v/> $k := open mydocument2.xml;
xsh $k/> count //section[subsection];# searches k
xsh $k/> count $v//section[subsection];# searches v