Re: [xsl] XPath expression that returns a sequence of pairs?

Subject: Re: [xsl] XPath expression that returns a sequence of pairs?
From: "C. M. Sperberg-McQueen cmsmcq@xxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 17 Jan 2023 22:37:57 -0000
"Roger L Costello costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> writes:

> Hi Folks,
>
> I have this XML document: ...
>
> I want an XPath expression that returns a sequence of
> (Target-Latitude, Target-Longitude) pairs; i.e., a pair for each
> <Observation> element. For the XML document shown above, the XPath
> should return this sequence:
>
> (10,20), (15,25)

As others have pointed out, this is not a sequence.

> A count of the number of items in the sequence should yield: 2

As long as you don't have any missing data, you'll get a list with an
even number of items.

> The following XPath is not correct:
>
> for $i in //Observation return ($i/Target-Latitude, $i/Target-Longitude)
>
> A count of the number of items returned by that XPath yields: 4
>
> Is there an XPath to do what I seek?

The XPath you have will work fine as long as you don't have missing
elements; when you work through your list of results, take one lat/long
pair off the front of the sequence at a time and process it.  Depending
on what you want to do with the lat/long pairs (and your tolerance for
working in languages that are not Lisp), that will either be a trivial
adjustment to how you write your code, or a slight annoyance, or a big
annoyance.

Alternatively, as others have pointed out, you could use arrays, or
maps, or XML elements to group the pairs.

If all you want to do is gather up the lat/long pairs and feed them
pairwise to something that draws maps or does something else with
geospatial data, then it may well be simplest to keep your existing
XPath expression and then work through the list two items at a time.

The minor complications of packing the lat/long pairs up as arrays and
then unpacking them to get the individual values will pay for themselves
if you are going to do a lot of things with the pairs that will be
significantly less convenient if you have to deal with a flat list with
an even number of items, but in many cases I think you may be best off
with the flat list.  (Recall that even in Lisp people often use lists
with even numbers of items in preference to lists of pairs, e.g. for
property lists.)


-- 
C. M. Sperberg-McQueen
Black Mesa Technologies LLC
http://blackmesatech.com

Current Thread