xpathsaxonxpath-3.1

What is the purpose/use of a map in XPath 3.1?


I understand the need for an array type in XPath 3.1 as they're fundamental to JSON. And yes I understand you can create a literal map() in an XPath query.

But is there a way XML or JSON can be structured where a query would naturally return a map on an XPath query against the underlying document? Or does it exist solely for the case where converting results into a map to then operate on is of benefit?


Solution

  • Probably the main use cases I've seen for maps are

    (a) to capture the result of parsing JSON input, when the input data is in JSON

    (b) to construct a structure that can be serialized as JSON, when JSON output is required.

    (c) to provide complex input parameters to functions (like the fn:transform() or fn:serialize() functions)

    (d) to capture multiple results or compound results from functions, e.g. a function that computes both the min and max of a sequence. If maps had been available at the time, they could have been used to get the namespace context of an element much more elegantly than the in-scope-prefixes/namespace-uri-for-prefix mechanism.

    (e) a map whose entries are functions can be used like an object in OO languages, to achieve polymorphism -- especially useful in XQuery which lacks XSLT's template rule despatch mechanism. The fn:random-number-generator() function design illustrates the idea.

    (f) a map can act as a simple struct for compound values, e.g. complex numbers. (It could have been used for date/time/duration/QName if available, or for the error information available in a catch clause)