XPath queries

An XML document consists of a hierarchy of elements. An XPath query is used to navigate through elements and attributes in an XML document.

For example to search for the paragraphs in the following XML document:

<body>
   <p>The quick brown fox.</p>
   <p>Jumped over the lazy dogs.</p>
</body>

The XPath search returns two results for the following XPath query:

body/p

If you have specified an attribute value for one of the elements:

<body>
   <p>The quick brown fox.</p>
   <p audience = "admin">Jumped over the lazy dogs.</p>
</body>

Include the attribute value in the search:

body/p[@audience='admin']

The XPath search returns one result.

The following table contains a list of sample XPath queries:

XPath Query

Locates

/task

The <task> element where it is a top-level node in an XML file

//task

The <task> element anywhere in an XML file

//task[@id='shovellingsnow']

A <task> element where the id attribute has the value shovellingsnow

task/title

All the <title> elements that are direct decedents of <task> elements

task//title

All the <title> elements that occur anywhere in a <task> element

For more information on XPath, see http://www.w3.org/TR/xpath/

XPath toolbar

1)Open the XPath toolbar (View > Toolbars > XPath).

XPath toolbar

frm_xpathtb_sa.png

2)In the XPath field, enter the query and click Run.

The results of the query display in the XPath Builder pod.

XPath Builder pod

frm_xpathbldr_sa.png

The Query Builder is also a convenient interface to build XPath queries. In the Query Builder, you can create (using Auto-Suggest functionality) and run an XPath query.

You can also specify the scope of the search:

Current File

All Open Files

Selected folder

DITA Map or Book - This option is available if you select a DITA Map or book in the Resource Manager

Auto-Suggest

The Auto-Suggest feature provides suggested components that you can add to the XPath query as you create the query.

For example, in the following XML:

<body>
   <p>The quick brown fox.</p>
   <p audience = "admin">Jumped over the lazy dogs.</p>
</body>

1)In the Query Builder field start with entering a forward slash (/).

As soon as you enter the forward slash, the following suggestions display:

Auto-Suggestions

frm_qbsuggest01_sa.png

2)Use the up and down arrow keys to select the option and press enter to insert the component into the query.

3)To add another element to the query, enter a forward slash.

Alternatively, to add an attribute, enter an opening square bracket ([).

The Auto-Suggest list contains the following components of an XPath query:

Element (Orange indicator)

Elements at the current position in the current document.

Attribute (Blue indicator)

Attributes at the current position in the current document.

Axes (Green indicator)

An axis is a node definition relative to the current node. For example, parent, child, ancestor.

Click the drop-down list to the right of the Enable Auto-Suggest option and un-check the Axes option to hide the available axes in the Auto-Suggest list.

note:    You can choose to disable the Auto-Suggest feature in the Query Builder.

Click the Save icon to save the current results of the XPath query.


September 30, 2016

Legal Notices | Online Privacy Policy