4

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231

1 Answers1

6

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • 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 this – Yahya Uddin Nov 30 '15 at 19:49
  • Depends on the interface you are using: command line? s9api API? XQJ? – Michael Kay Nov 30 '15 at 22:45
  • 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? – Yahya Uddin Dec 01 '15 at 01:52
  • 1
    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. – Michael Kay Dec 01 '15 at 09:11