Skip to content

YAML Path versus XPath

William W. Kimball, Jr., MBA, MSIS edited this page Jun 1, 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:

  • / separates navigation segments ("steps" in XPath), though YAML Path alternatively permits . to be used.
  • [] filters nodes by a predicate expression.
  • * selects every node that is an immediate child of the present, though YAML Path permits "skeletons" to employ this character.

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 descendent 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 /; 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.

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

  • Axes like child::*
  • Most XPath functions
  • Math operators: + and - (except when used for Collector Math), *, div, mod
  • Logic operators: and, or
Clone this wiki locally