根据元素在Xquery 3.0中的位置以单独的顺序连接元素
我有2个序列,如下所示:根据元素在Xquery 3.0中的位置以单独的顺序连接元素,xquery,xquery-3.0,Xquery,Xquery 3.0,我有2个序列,如下所示: let $city := ('London', 'Tokyo') let $country := ('England', 'Japan') let $region := ('Europe','Asia') 我想做的是操纵2个序列,使其看起来像这样: <Data> <location> <city>London</city> <country>England</co
let $city := ('London', 'Tokyo')
let $country := ('England', 'Japan')
let $region := ('Europe','Asia')
我想做的是操纵2个序列,使其看起来像这样:
<Data>
<location>
<city>London</city>
<country>England</country>
<region>Europe</region>
</location>
<location>
<city>Tokyo</city>
<country>Japan</country>
<region>Asia</region>
</location>
</Data>
2) 连接序列中每个项目的第一个字符,并以某种方式对其进行操作,如下所示。请注意,这个代码不起作用,但我相信它可以起作用
let $city := ('1London', '2Tokyo')
let $country := ('1England', '2Japan')
let $region := ('1Europe','2Asia')
for $locCity in $city, $locCountry in $country, $locRegion in $region
where substring($locCity,1,1) = substring($locCountry ,1,1) and
substring($locCountry ,1,1) = substring($locRegion ,1,1)
let $location := ($locCity,$locCountry,$region)
return
$location
然而,这根本不起作用。我真的不知道该怎么做。我相信有更好的方法来解决这个问题。也许“分组”的方法可以奏效。任何帮助都将不胜感激。谢谢
干杯
Jay使用
max()
找出序列中哪些项最多,然后从1迭代到max()
,并为每个序列构造一个
元素。使用谓词“按位置选择”从每个序列中选择值:
xquery version "1.0";
let $city := ('London', 'Tokyo')
let $country := ('England', 'Japan')
let $region := ('Europe','Asia')
return
<Data>
{
for $i in 1 to max((count($city), count($country), count($region)))
return
<location>
<city>{ $city[$i] }</city>
<country>{ $country[$i] }</country>
<region>{ $region[$i] }</region>
</location>
}
</Data>
xquery版本“1.0”;
let$city:=(‘伦敦’、‘东京’)
let$country:=(‘英格兰’、‘日本’)
let$region:=(‘欧洲’、‘亚洲’)
返回
{
对于1中的$i,最多((计数($city)、计数($country)、计数($region)))
返回
{$city[$i]}
{$country[$i]}
{$region[$i]}
}
这是一个很好的XQuery 1.0解决方案,不过我建议去掉特定于MarkLogic的版本标识符。另外,对于OP的问题,max count并不是绝对必要的:(1到2)
应该足够了。当然,我调整了版本声明。使用max()
代替硬编码的数字更为通用,但如果值序列已知且保持静态,硬编码的数字将更为简单(并且可能更为有效)。
xquery version "1.0";
let $city := ('London', 'Tokyo')
let $country := ('England', 'Japan')
let $region := ('Europe','Asia')
return
<Data>
{
for $i in 1 to max((count($city), count($country), count($region)))
return
<location>
<city>{ $city[$i] }</city>
<country>{ $country[$i] }</country>
<region>{ $region[$i] }</region>
</location>
}
</Data>