XdmValue that contains a map…

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

XdmValue that contains a map…

Norman Walsh
Hello world,

Hopefully a simple question. I did try to find the answer in the docs
and by exploring the APIs, but to no avail. Feel free to simply point
me to the right place.

Holding in my left hand an XdmValue that I believe contains a (Saxon
9.7/XPath 3.1) map, how do I:

1. Test that this is the case?
2. Having determined that it is, retrieve the keys?
3. Now in posession of the keys, get the values?

Thanks, in advance.

                                        Be seeing you,
                                          norm

--
Norman Walsh <[hidden email]> | In a universe of electrons and selfish
http://nwalsh.com/            | genes, blind physical forces and
                              | genetic replication, some people are
                              | going to get hurt, other people are
                              | going to get lucky, and you won't find
                              | any rhyme or reason in it, nor any
                              | justice.--Richard Dawkins

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 

signature.asc (178 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: XdmValue that contains a map…

Michael Kay
Maps and arrays aren't well supported in the XDM classes at the moment. That's already been fixed for 9.8, which introduces XdmMap and XdmArray, but for the moment, I think you have to dive down into lower-level stuff.

Try the following:

if (value.size() == 1) ...

XdmItem item = value.itemAt(0);

item instanceof XdmFunctionItem

Map<XdmAtomicValue, XdmValue) map = item.asMap();

if (map != null) ...

You can then use the Java map interface to iterate over keys or key-value pairs in the usual way.

This does involve copying the entire map from Saxon's internal representation to a Java Map instance. If copying is too expensive, try:

net.sf.saxon.om.Item it = item.getUnderlyingValue();

if (it instanceof MapItem) ...

for (KeyValuePair pair : (MapItem)it) ...

AtomicValue pair.key
Sequence pair.value


Hope that helps,

Michael Kay
Saxonica







> On 12 Oct 2016, at 14:53, Norman Walsh <[hidden email]> wrote:
>
> Hello world,
>
> Hopefully a simple question. I did try to find the answer in the docs
> and by exploring the APIs, but to no avail. Feel free to simply point
> me to the right place.
>
> Holding in my left hand an XdmValue that I believe contains a (Saxon
> 9.7/XPath 3.1) map, how do I:
>
> 1. Test that this is the case?
> 2. Having determined that it is, retrieve the keys?
> 3. Now in posession of the keys, get the values?
>
> Thanks, in advance.
>
>                                        Be seeing you,
>                                          norm
>
> --
> Norman Walsh <[hidden email]> | In a universe of electrons and selfish
> http://nwalsh.com/            | genes, blind physical forces and
>                              | genetic replication, some people are
>                              | going to get hurt, other people are
>                              | going to get lucky, and you won't find
>                              | any rhyme or reason in it, nor any
>                              | justice.--Richard Dawkins
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, SlashDot.org! http://sdm.link/slashdot_______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: XdmValue that contains a map…

Michael Kay

> On 12 Oct 2016, at 19:19, Michael Kay <[hidden email]> wrote:
>
> Maps and arrays aren't well supported in the XDM classes at the moment. That's already been fixed for 9.8, which introduces XdmMap and XdmArray, but for the moment, I think you have to dive down into lower-level stuff.
>
> Try the following:
>
> if (value.size() == 1) ...
>
> XdmItem item = value.itemAt(0);
>
> item instanceof XdmFunctionItem
>
> Map<XdmAtomicValue, XdmValue) map = item.asMap();
>

Looking at this more closely, I realise this map is singularly useless, because XdmAtomicValue does not have a sensible equals() method.

Issue raised here: https://saxonica.plan.io/issues/2994

Michael



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: XdmValue that contains a map…

Norman Walsh
In reply to this post by Michael Kay
Michael Kay <[hidden email]> writes:

> If copying is too expensive,
> try:
>
> net.sf.saxon.om.Item it = item.getUnderlyingValue();
>
> if (it instanceof MapItem) ...
>
> for (KeyValuePair pair : (MapItem)it) ...
>
> AtomicValue pair.key
> Sequence pair.value
That, or something very close to it, worked. Thanks, Michael!

                                        Be seeing you,
                                          norm

--
Norman Walsh <[hidden email]> | Criminal: A person with predatory
http://nwalsh.com/            | instincts who has not sufficient
                              | capital to form a corporation.--Howard
                              | Scott

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 

signature.asc (178 bytes) Download Attachment