Creating a map in XQuery
Asked Answered
A

1

5

I am trying to create a hash-map/ key-value pair like structure in xquery. I am aware a map like structure exists in xquery: http://www.w3.org/2005/xpath-functions/map/

and even found documentation in Saxon: http://www.saxonica.com/html/documentation/functions/map/

However I am both unsure how to create a map or use it.

Here's my code so far:

declare namespace map="http://www.w3.org/2005/xpath-functions/map";
let $a := map:map()

But I get an error:

Cannot find a matching 1-argument function named
  {http://www.w3.org/2005/xpath-functions/map}map()

So how exactly do I use maps in xquery?

Allman answered 30/11, 2015 at 17:50 Comment(0)
N
7

The syntax is in XSLT 3.0 and XQuery 3.1 and has been through a few iterations as the working drafts have evolved. The current syntax (supported in Saxon 9.7) allows

map{}

for an empty map

map{'a':1, 'b':2}

for a map with a known number of entries (both the keys and the values can be arbitrary expressions), and

map:merge(for $x in //emp return map{$x!name : $x!@salary})

for a map with a statically-unknown number of entries.

Nonstop answered 30/11, 2015 at 18:24 Comment(4)
This seems to be on the right track but now I get the following error: To use XPath 3.1 syntax, you must configure the XPath parser to handle it. How do I do thisAllman
Depends on the interface you are using: command line? s9api API? XQJ?Nonstop
I think I managed that issue but what does: $x!name : $x!@salary part mean. I never seen this syntax with the "!". What does it mean?Allman
The "!" operator is new in XPath 3.0. A!B means roughly the same as for $a in A return B, except that B is evaluated with $a as the context item. So another way of thinking about it is that it's the same as A/B, except that A doesn't have to consist only of nodes, and there is no sorting into document order. I have got into the habit of using it in situations like this where "/" would do just as well.Nonstop

© 2022 - 2024 — McMap. All rights reserved.