XPath选择器联接元素



我需要使用XPath来选择并返回元素的值"经度;以及";纬度;在下面的XML中。

<LocationsList>
<Locations>
<Id>31dbwlph0yi--1</Id>
<Latitude>45.352304</Latitude>
<Longitude>-75.724945</Longitude>
</Locations>
<Locations>
<Id>600001-0-0--1</Id>
<Latitude>45.33142</Latitude>
<Longitude>-79.96399</Longitude>
</Locations>
</LocationsList>

以下是我需要XPath返回的内容(其中一个列表项同时返回Latitude和Longitude(:

1. 45.352304 -75.724945
2. 45.33142 -79.96399

不幸的是,我遇到了麻烦。以下是我尝试过的。。。

XPathLocationsList/Locations返回:

1. 31dbwlph0yi--1 45.352304 -75.724945
2. 600001-0-0--1 HWY 400 45.33142 -79.96399

XPathLocationsList/Locations/(Latitude | Longitude)返回:

1. 45.35304
2. -75.724945
3. 45.33142
4. -79.96399

XPathLocationsList/Locations/concat(Latitude, " ", Longitude)返回字符串:

Non-standard output:
45.352304 -75.724945
45.33142 -79.96399

XPath3.1之前,数据模型中没有数组数据类型。你能做的最好的事情就是返回一个序列,但注意,没有序列序列这回事;序列必然是一个平面列表。让我们看看这是如何与您的样本数据一起工作的:

<LocationsList>
<Locations>
<Id>31dbwlph0yi--1</Id>
<Latitude>45.352304</Latitude>
<Longitude>-75.724945</Longitude>
</Locations>
<Locations>
<Id>600001-0-0--1</Id>
<Latitude>45.33142</Latitude>
<Longitude>-79.96399</Longitude>
</Locations>
</LocationsList>

举个例子:

/LocationsList/Locations/concat(Latitude, " ", Longitude)

它生成一个由2个字符串组成的序列,每个字符串都包含一个纬度和一个经度值,由一个空格分隔:

("45.352304 -75.724945", "45.33142 -79.96399")

或者,您可以使用这样的表达式来选择LatitudeLongitude元素的序列,并将它们转换为数字:

LocationsList/Locations/(Latitude | Longitude)/number(.)

它返回一个由4个数字组成的序列,这些数字交替为纬度和经度值

(45.352304,-75.724945,45.33142,-79.96399)

最后,在XPath3.1中,您支持array数据类型,因此您可以迭代位置序列,并从每个位置构造一个数组,然后返回一个数组序列:

for $location in 
/LocationsList/Locations 
return 
[number($location/Latitude), number($location/Longitude)]

结果是这个数组序列,每个数组包含2个数字:

([45.352304,-75.724945], [45.33142,-79.96399])

或者,如果你想得到幻想,你可以返回一个数组:

fold-left(
for $location in 
/LocationsList/Locations 
return 
[number($location/Latitude), number($location/Longitude)],
[],
function($array, $location) {
Q{http://www.w3.org/2005/xpath-functions/array}append($array, $location)
}
)

给予:

[[45.352304,-75.724945],[45.33142,-79.96399]]

最新更新