[英]How to write a xQuery to extract two fields based on the conditions of others value?
I have an XML document: 我有一个XML文档:
<resultsets>
<row>
<emp_no>10001</emp_no>
<first_name>Georgi</first_name>
<last_name>Facello</last_name>
</row>
<row>
<emp_no>10002</emp_no>
<first_name>Bezalel</first_name>
<last_name>Simmel</last_name>
</row>
</resultset>
I want to write a xQuery using FLWOR to extract emp_no and first_name where emp_no is 10001 with the coressponding first name like this: 我想使用FLWOR编写一个xQuery来提取emp_no和first_name,其中emp_no是10001,其核心响应的名字是这样的:
<row>
<emp_no>10001</emp_no>
<first_name>Georgi</first_name>
</row>
The code I wrote: 我写的代码:
for $id in doc("employees.xml")//emp_no
for $first in doc("employees.xml")//first_name
where ($id/text() = '10001')
return
<row>
<id>{upper-case($id/text())}</id>
<first>{$first/text()}</first>
</row>
however, it returns a cartesian product of $id and $first, 但是,它将返回$ id和$ first的笛卡尔乘积,
<row>
<emp_no>10001</emp_no>
<first_name>Georgi</first_name>
</row>
<row>
<emp_no>10001</emp_no>
<first_name>Bezalel</first_name>
</row>
Do you know how to fix this using xQuery FLWOR? 您知道如何使用xQuery FLWOR修复此问题吗? Thanks!
谢谢!
I would simply select the row
and then use a where clause on the emp_no
child: 我只需选择该
row
,然后对emp_no
子项使用where子句:
for $row in /resultsets/row
where $row/emp_no = '10001'
return
<row>
<id>{$row/emp_no/data()}</id>
<first>{$row/first_name/data()}</first>
</row>
http://xqueryfiddle.liberty-development.net/bFukv89 http://xqueryfiddle.liberty-development.net/bFukv89
It is not quite clear whether you want to change the element names from emp_no
to id
and from first_name
to first
, if that is not needed you can of course simply copy them: 尚不清楚您是否要将元素名称从
emp_no
为id
,从first_name
更改为first
,如果不需要,您当然可以简单地复制它们:
for $row in /resultsets/row
where $row/emp_no = '10001'
return
<row>
{$row/emp_no, $row/first_name}
</row>
http://xqueryfiddle.liberty-development.net/bFukv89/1 http://xqueryfiddle.liberty-development.net/bFukv89/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.