To use XPath effectively requires an understanding of the XPath Core Function Library. The Oxygen XML XPath expression field of the current editor toolbar can be used to aid you in XML document development.
In Oxygen XML an XPath
1.0 or XPath 2.0 expression is typed and executed on the current document from the menu . Both XPath 2.0 basic and XPath 2.0
schema aware expressions can be executed in the XPath console. XPath 2.0 schema aware also
takes into account the Saxon EE or from the toolbar button XML Schema
version option. The XPath console features a syntax highlight mechanism that allows
you to better identify the components of the XPath expression. You can customize the color
scheme from the
Syntax Highlight
options page.
The content completion assistant that helps entering XPath expressions in attributes of XSLT stylesheets elements is also available in the XPath console. It offers context dependent proposals according with the cursor position in the edited document. The set of XPath functions proposed by the assistant also depends on the XPath version selected from the drop-down menu of the XPath button (1.0 or 2.0).
In the following figure the content completion assistant offers element names from the current document and all XPath 2.0 functions:
Content Completion in the XPath console
The evaluation of the XPath expression tries to resolve the locations of documents referred in the expression through the XML catalogs which are configured in Preferences and the current XInclude preferences. An example is evaluating the collection(URIofCollection) function (XPath 2.0). If you need to resolve the references from the files returned by the collection() function with an XML catalog set up in the Oxygen XML Editor plugin preferences you have to specify the class name of the XML catalog enabled parser for parsing these collection files. The class name is ro.sync.xml.parser.CatalogEnabledXMLReader and you specify it like this:
let $docs := collection(iri-to-uri( "file:///D:/temp/test/XQuery-catalog/mydocsdir?recurse=yes;select=*.xml; parser=ro.sync.xml.parser.CatalogEnabledXMLReader"))
The results of an XPath query are displayed in a view that allows grouping them as a tree. Clicking a record in the result list highlights the matched nodes within the text editor panel with a character level precision. Results are returned in a format that is a valid XPath expression:
- /node[value]/node[value]/node[value] -
XPath results highlighted in editor panel with character precision
When using the grid editor, clicking a result record will highlight the entire node.
XPath results highlighted in the Grid Editor
The popup menu of the history list of the XPath dialog contains Remove and Remove All actions for removing expressions from the history list.
The following examples are taken from a DocBook book based on the DocBook XML DTD. The book contains a number of chapters. To return all the chapter nodes of the book you should enter //chapter into the XPath expression field then press (Enter). This will return all the chapter nodes of the DocBook book, in the results view. Each record when clicked will locate and highlight the corresponding chapter element and all its children nodes.
If you want to find all example nodes contained in the sect2 nodes of a DocBook XML document you should use the following XPath expression: //chapter/sect1/sect2/example. For each example node found in any sect2 node, a result will be added to the results view.
For example if one of the results of the previous XPath query is:
- /chapter[1]/sect1[3]/sect2[7]/example[1]
it means that in the edited file, first chapter, third section level 1, seventh section level 2, the found example node is the first in the section.
To define default mappings between prefixes that can be used in the XPath console and
namespace URIs go to the
XPath Options user preferences
panel and enter the mappings in the Default prefix-namespace
mappings table. The same preferences panel allows
the
configuration of the default namespace used in XPath 2.0 expressions entered into the XPath
toolbar and if
different
results tabs should
be created
for
each executed XPath
query.
To apply an XPath expression relative to the element on which the caret is positioned use the following actions:
The popup menu available in the Expression panel of the XPath expressions dialog offers the usual edit actions: Cut, Copy, Paste, Select All.