Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

XPath

The grammar for specifying a selector or field is a subset of the XPath grammar. While this article does not go into details regarding this grammar, suffice it to say that selectors specify element types via a '/' delimited path. The field grammar is identical to the selector grammar, with the addition of '@' to prefix an attribute type. Note that the following special sub-expressions:

  • '.' denotes the current element type (much like a directory path)

  • '*' denotes "all" element types at a given level, as already demonstrated in the previous example.

  • '//' denotes all children. Like '*', but recursive. This is unlike directory paths, where '//' normally denotes root.

  • '@' is a prefix that indicates an attribute type. Only a field (not a selector) may specify an attribute type.

The next table provides a set of paths, descriptions, and corresponding XML as fairly encompassing examples of XPath for selectors and fields:

XPath

Description

Structure

.

The current element.

<current>foo</current>
Xyz

The child element xyz of the current element.

<current>
 <xyz>foo</xyz>
 <xyz>bar</xyz>
</current>
abc/xyz

The xyz child element of the abc child element of the current element.

<current>
 <abc>
  <xyz>foo</xyz>
 </abc>
 <abc>
  <xyz>bar</xyz>
 </abc>
</current>
.//xyz

Any xyz descendant element of the current element

<current>
 <xyz>foo</xyz>
 <abc>
  <xyz>bar</xyz>
 <abc/>
<current>
*/xyz

The xyz child of any child of the current element.

<current>
 <abc>
  <xyz>foo</xyz>
 </abc>
 <def>
  <xyz>bar</xyz>
 </def>
</current>
@attr

The attr attribute of the current element.

<current attr="1"/>
xyz/@attr

The attr attribute of the xyz child element.

<current>
 <xyz @attr="1">
 <xyz @attr="2"/>
</current>
.//@attr

Any attr attribute of any descendant element of the current element.

<current>
 <xyz @attr="1"/>
 <abc @attr="2">
  <xyz @attr="3"/>
 <abc/>
<current>
*/@attr

The attr attribute of any child of the current element.

<current>
 <abc @attr="1">
 <def @attr="2"/>
</current>

  • + Share This
  • 🔖 Save To Your Account