2

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?

David Thielen
  • 28,723
  • 34
  • 119
  • 193

2 Answers2

2

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)

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
2

"is there a way [..] JSON can be structured where a query would naturally return a map?": anything in JSON being an "object"

https://www.json.org/json-en.html: "An object is an unordered set of name/value pairs. An object begins with {left brace and ends with }right brace")

maps (pun intended) to an XDM map.

So in JSON both arrays and objects are fundamental and in the XDM you can represent a JSON array as an XDM array and a JSON object as an XDM map.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110