To use XPath effectively requires at least an understanding of the XPath Core Function Library. If you have this knowledge the Oxygen XPath expression field part of the current editor toolbar can be used to aid you in XML document development.
In Oxygen a XPath 1.0 or XPath 2.0 expression is typed and executed on the current document from the XPath console available on the XPath toolbar for every open XML document.. 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 XML Schema version option.
The content completion assistant that helps in entering XPath expressions in attributes of XSLT stylesheets elements is also available in the XPath console and offers always proposals dependent of the current context of the cursor inside the edited document. The set of XPath functions proposed by the assistant 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 cursor is on a person element and the content completion assistant offers all the child elements of the person element and all XPath 2.0 functions:
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 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"))
If you want to see in the XPath console the XPath expression at the
current cursor position when navigating in the document you have to check the button
XPath update on caret move.
The results of an XPath query are returned in the message panel. Clicking a record in the result list highlights the nodes within the text editor panel with a character level precision. Results are returned in a format that is a valid XPath expression:
- [FileName.xml] /node[value]/node[value]/node[value] -
When using the grid editor, clicking a result record will highlight the entire node.
When the limit of long expressions is reached (60 characters) a dialog pops up and offers to switch the focus to the XPath builder view. This is a view specially designed to assist you with typing and testing complex XPath 1.0 / 2.0 expressions.
The following examples are taken from a DocBook book based on the DocBook XML DTD. The book contains a number of chapters. DocBook defines that chapters have a <chapter> start tag and a matching </chapter> end tag to close the element. 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 message panel. If your book has six chapters, they will be six records in the result list. Each record when clicked will locate and highlight the chapter and all sibling nodes contained between the start and end tags of the chapter.
If you want to find all example nodes contained in the section 2 nodes of a DocBook XML document you should use the following XPath expression: //chapter/sect1/sect2/example. If an example node is found in any section 2 node, a result will be returned to the message panel. For each occurrence of the element node a record will be created in the result list.
For example one of the results of the previous XPath query on the file oxygen.xml is:
- [oxygen.xml] /chapter[1]/sect1[3]/sect2[7]/example[1]
which means that in the file oxygen.xml, first chapter, third section level 1, seventh section level 2, the example node found 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 also the configuration of the
default namespace used in XPath 2.0 expressions entered into the XPath toolbar and the
creation of different message panels for XPath queries executed on different XML
documents.
To apply a XPath expression relative to the element on which the caret is positioned use the following actions: