XML提要上的简单XQuery-关于定义变量的帮助
我以前从未做过XQuery,我需要使用变量编写语句,而不是硬编码值,因为这是一个实时XML提要。(很抱歉提出这样一个基本的问题) 每个事件都有一个名称、占用的位置数和可用的空位置数。我不想在4和11中硬编码,而是想把它们变成一个特定事件的变量 活动是/event/name=“摇滚音乐会”,我想XML提要上的简单XQuery-关于定义变量的帮助,xml,variables,xquery,Xml,Variables,Xquery,我以前从未做过XQuery,我需要使用变量编写语句,而不是硬编码值,因为这是一个实时XML提要。(很抱歉提出这样一个基本的问题) 每个事件都有一个名称、占用的位置数和可用的空位置数。我不想在4和11中硬编码,而是想把它们变成一个特定事件的变量 活动是/event/name=“摇滚音乐会”,我想 nb_places = that of "Rock Concert" and nb_empty_places >= that of "Rock Concert" 这就是我到目前为止所做的: <
nb_places = that of "Rock Concert" and nb_empty_places >= that of "Rock Concert"
这就是我到目前为止所做的:
<bib>
{
for $n in doc("http://tinyurl.com/")/event
where $n/nb_places = 4 and $n/nb_empty_places >= 11
return
<result>
{ $n/nb_empty_places }
{ $n/nb_places }
{ $n/name }
</result>
}
</bib>
{
对于文件中的$n(“http://tinyurl.com/活动
其中,$n/nb_位置=4,$n/nb_空位置>=11
返回
{$n/nb_空位置}
{$n/nb_places}
{$n/name}
}
谢谢:)
XML示例:
<event>
<name>Rock Concert</name>
<nb_places>4</nb_places>
<nb_empty_places>11</nb_empty_places>
</event>
<event>
<name>Indie Concert</name>
<nb_places>4</nb_places>
<nb_empty_places>9</nb_empty_places>
</event>
<event>
<name>RnB Concert</name>
<nb_places>2</nb_places>
<nb_empty_places>14</nb_empty_places>
</event>
<event>
<name>Pop Concert</name>
<nb_places>3</nb_places>
<nb_empty_places>32</nb_empty_places>
</event>
<event>
<name>House Concert</name>
<nb_places>4</nb_places>
<nb_empty_places>20</nb_empty_places>
</event>
摇滚音乐会
4.
11
独立音乐会
4.
9
RnB音乐会
2.
14
流行音乐会
3.
32
家庭音乐会
4.
20
预期结果,与摇滚音乐会相同数量的场地,以及相同或更多的空位:
<result>
<nb_empty_places>32</nb_empty_places>
<nb_places>4</nb_places>
<name>Rock Concert</name>
</result>
<result>
<nb_empty_places>20</nb_empty_places>
<nb_places>4</nb_places>
<name>House Concert</name>
</result>
32
4.
摇滚音乐会
20
4.
家庭音乐会
这是一种可能的方法:
declare variable $nb_places := root/event[name='Rock Concert']/nb_places;
declare variable $nb_empty_places := root/event[name='Rock Concert']/nb_empty_places;
<bib>
{
for $n in root/event
where $n/nb_places = $nb_places and $n/nb_empty_places/number() >= $nb_empty_places
return
<result>
{ $n/nb_empty_places }
{ $n/nb_places }
{ $n/name }
</result>
}
</bib>
声明变量$nb_places:=root/event[name='Rock Concert']/nb_places;
声明变量$nb_empty_places:=root/event[name='Rock Concert']/nb_empty_places;
{
对于根/事件中的$n
其中$n/nb_places=$nb_places和$n/nb_empty_places/number()>=$nb_empty_places
返回
{$n/nb_空位置}
{$n/nb_places}
{$n/name}
}
演示:请发布一个简化的示例XML,以及给定该示例XML作为输入的确切预期输出。谢谢你的意思是,你想使用变量,比如
声明变量$nb\u places:=4代码>然后。。。where$n/nb_places=$nb_places…
?@har07所以我要找的是:where$nb_places=($nb_places where$event=“摇滚音乐会”在这种情况下,我的第一条评论与澄清这个问题非常相关。我们需要示例XML来明确想法。当然,我会发布一些东西,谢谢!啊,因为我希望获得与rock具有相同值的所有事件concert@missdevops既然你的问题非常清楚,我已经相应地更新了答案:)