XPath is a language for addressing specific parts of an XML document. XPath, like the Document Object Model (DOM), models an XML document as a tree of nodes. An XPath expression is a mechanism for navigating through and selecting nodes from the XML document. An XPath expression is in a way analogous to a Structured Query Language (SQL) query used to select records from a database.
XPath models an XML document as a tree of nodes. There are different types of nodes, including element nodes, attribute nodes and text nodes. XPath defines a way to compute a string-value for each type of node.
XPath defines a library of standard functions for working with strings, numbers and Boolean expressions.
Examples:
child: : * Select all children of the root node.
.//name Select all elements having the name "name", descendants of the current node.
/catalog/cd[price>10.80]Selects all the cd elements that have a price element with a value larger than 10.80
To find out more about XPath, the following URL is recommended: http://www.w3.org/TR/xpath
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.
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 example 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, for example when 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 in the query which is the parameter of the collection() function the name of the class 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 can check the button
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.
![]() | Note |
---|---|
XPath 2.0 queries are executed using Saxon 9 B transformation engine and they are not schema aware. If you try to impose type restrictions in a XPath 2.0 query they are ignored. |
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.
Example 10.1. XPath Utilization with DocBook DTD
Our example is taken from a DocBook book based on the DocBook XML DTD. The book contains a number of chapters. DocBook defines that chapters as have a <chapter> start tag and matching </chapter> end tag to close the element. To return all the chapter nodes of the book enter //chapter into the XPath expression field, then Enter. This will return all the chapter nodes of the DocBook book, in the Message Panel. If your book has six chapters, their 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 we used XPath to query for all example nodes contained in the section 2 node of a DocBook XML document we would 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.
In our example an XPath query on the file oxygen.xml
determined that:
- [oxygen.xml] /chapter[1]/sect1[3]/sect2[7]/example[1]
Which means:
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.
![]() | Note |
---|---|
If your project is comprised of a main file with ENTITY references to other files, you can use XPath to return all the name elements of a certain type by querying the main file. The result list will query all referenced files. |
![]() | Note |
---|---|
When the edited document is of type XSL the XPath expression typed in the XPath console is applied over the XML document specified in the transformation scenario associated with the XSL document. <oXygen/> provides a user preference to be set if you want to apply the XPath expression over the XSL document itself. |
![]() | Important |
---|---|
If the document defines a default namespace then <oXygen/> will bind this namespace to the first free prefix from the list: default, default1, default2, etc. For example if the document defines the default namespace xmlns="something" and the prefix default is not associated with a namespace then you can match tags without prefix in a XPath expression typed in the XPath console by using the prefix default. For example to find all the level elements when the root element defines a default namespace you should execute in the XPath console the expression: //default:level |
To define default mappings between prefixes that can be used in the XPath console and namespace URIs go to the and enter the mappings in the user preferences panelDefault 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 results 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 action Ctrl+Alt+.) (also available on the context menu of the main editor panel) to copy the XPath expression of the element to the clipboard and the action of the contextual menu of the XPath console to paste this expression in the console. Then add your relative expression and execute the resulting complete expression.
→ → (On Windows the context menu can be displayed with the mouse on a right click or with the keyboard by pressing the special context menu key available on Windows keyboards.
Complex XPath expressions can be composed with the help of the content completion assistant available for XPath expressions in a special view called XPath Builder. Also the expressions can be tested in the view by execution on the edited document. The view is opened from menu Perspective -> Show View.
The Execute button runs the expression on the edited document and takes into account the value selected in the combo box with the XPath version number: 1.0 or 2.0. The XPath preferences panel is accessible from the shortcut button near the Execute button. A history list with the XPath expressions evaluated in the past on all documents opened in the current <oXygen/> session is also available in the bottom area of the view so that new expressions can be composed based on old ones without re-entering the whole expression.
The
button enables the XPath Builder view to display the XPath expression at the current cursor position when navigating in the document.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, for example when evaluating the collection(URIofCollection
) function (XPath 2.0).
The results of the XPath query are displayed in the same Message Panel as for the XPath console and are computed with the same character level precision.
The usual edit actions (Cut, Copy, Paste, Select All, Undo, Redo) are available in the popup menu of the top part of the view, where XPath expressions are entered. For the history list area of the view the popup menu contains two actions:
Execute - to execute again the expression selected in the list.
Remove - to remove the selected expression from the list.