Skip to content

YAML Path versus XPath

William W. Kimball, Jr., MBA, MSIS edited this page Jun 2, 2021 · 5 revisions

Introduction

To get the obvious out of the way: XPath and YAML Path operate against exclusively different data transport formats. Whereas XPath operates only against XML content, YAML Path operates against YAML, JSON, and compatible content. Setting that aside, XPath (and Hiera) influenced the development of YAML Path. XPath specifies not only a means to address content within hierarchically structured data but also of literally hundreds of functions. While YAML Path offers a very similar means to address content within structured data, it supplies merely a handful of functions, so far. XPath is also notably more complex to use than YAML Path, owing largely to its sheer size and the disparate nature of content to which it enables access (elements, attributes, text, namespaces, and more).

On the surface, YAML Path shares much syntax with XPath:

There are some notable differences:

  • XPath uses | to indicate a union of two paths. YAML Path uses (PATH)+(PATH) for the same.
  • XPath uses a double forward-slash (//) to recursively evaluate every descendant node. YAML Path uses a double-star (**) for the same.
  • XPath permits relative paths by omitting the leading /. YAML Path permits relative paths only through the use of () pairs wherein the first character of a forward-path delimited YAML Path must still be a / (unless using . rather than / to separate segments); though the sub-path's "root" will be the context of the preceding outer path, making the sub-path relative.
  • XPath stipulates that the first element of any list be indexed as 1. YAML Path uses 0 for the same.
  • XPath uses () to select elements from Arrays. YAML Path uses [] (explicit) or a bare integer (implicit) as a path segment for the same.

And there are some XPath features that are not present in YAML Path:

  • Axes like child::*
  • Most XPath functions, though YAML Path offers some of them as Search Keywords.
  • Math operators: + and - (except when used for Collector Math), *, div, mod
  • Logic operators: and, or
  • Variable creation for use only within the Path expression (including on-the-fly Maps and Arrays), though YAML Path does permit on-the-fly edits to the underlying document by specifying a path to nodes which do not already exist (tool permitting)
Clone this wiki locally