Xquery 循环产生太多结果。 "for" 声明错误?
Xquery loop producing too many results. Bad "for" statement?
我有数千个 XML 文件需要对其执行 XQuery。
我查询的XML是这样的,格式一致:
<Archive xmlns="http://www.xxxx.com/egAr/1.0" Location="file://xxxxxx/Digital_Archive/773592.zip" Version="1.0">
<General Application="xxxx 16.0.1 - Build 1108 - Sep 30 2016" Author="xxxx@xxxx1" ModificationDate="2018-02-07T08:04:10-05:00" Server="xxxxxxx"/>
<Job CSREmail="" Category="Digital" Category2="MCSD904LAYOUT" Category3="Mat HSLPETG10022 / S257 petg 50m" Category4="No" Category5="xxxxx" Category6="2" Category7="xxxxxx" Created="2016-03-07T09:33:15-05:00" CreatedFrom="xxxxxx" CustomerID="xxxxxx" CustomerJobReference="xxxxxxx" JobFolder="xxxxxxx" Name="773592" OrderID="773592" ProjectID="773592" SubOrderID="">
<Description>xxxxxxx</Description>
</Job>
<Customer ID="332420" Name="xxxxxxx"/>
<JobParams>
<JobParam Name="steprepeat.No Across" Value="0"/>
<JobParam Name="steprepeat.No Around" Value="0"/>
<JobParam Name="steprepeat.Spacing across" Value="0"/>
<JobParam Name="steprepeat.Spacing around" Value="0"/>
<JobParam Name="steprepeat.Fronts around" Value="0"/>
<JobParam Name="steprepeat.Fronts across" Value="0"/>
<JobParam Name="steprepeat.Backs across" Value="0"/>
<JobParam Name="steprepeat.Backs around" Value="0"/>
</JobParams>
</Archive>
这是我的 Xquery:
for $t in //@Category2,
$v in //@OrderID
let $d := "MCHT65780LAYOUT"
where $t = $d
order by $v
return concat ("Job: ",$v, " Die: ", $t)
它或多或少地产生了我正在寻找的东西,但我的 "for" 循环有问题,我得到了无限的结果,而 BaseX 在 500,000 个结果处停止。如果我只在 "for" 语句中放入一项,我会得到正确数量的结果,但我需要在结果中看到 "OrderID" 和 "Category2"。
基本上我想搜索 $d 并查看哪些作业使用了该值。
Michael Kay 在下面的回答非常有效。我还想出了如何通过 "contains" 函数的部分匹配产生结果:
declare namespace a="http://www.xxxx.com/egAr/1.0";
for $j in //a:Job
let $t := $j/@Category2
let $v := $j/@OrderID
let $d := "67"
order by $v
where ($t [contains (.,$d)])
return concat ("Job: ",$v, " Die: ", $t)
我怀疑您不想考虑出现在文档中任何位置的所有 @Category2
和 @OrderId
对,而只考虑出现在同一作业元素上的对。
你可以这样写
declare namespace a="http://www.xxxx.com/egAr/1.0";
for $j in //a:Job
let $t := $j/@Category2
let $v := $j/@OrderID
let $d := "MCHT65780LAYOUT"
where $t = $d
order by $v
return concat ("Job: ",$v, " Die: ", $t)
但我可能会写成:
declare namespace a="http://www.xxxx.com/egAr/1.0";
for $j in //a:Job[@Category2 = "MCHT65780LAYOUT"]
order by $j/@OrderID
return $j/concat("Job: ", @OrderID, " Die: ", @Category2)
我有数千个 XML 文件需要对其执行 XQuery。
我查询的XML是这样的,格式一致:
<Archive xmlns="http://www.xxxx.com/egAr/1.0" Location="file://xxxxxx/Digital_Archive/773592.zip" Version="1.0">
<General Application="xxxx 16.0.1 - Build 1108 - Sep 30 2016" Author="xxxx@xxxx1" ModificationDate="2018-02-07T08:04:10-05:00" Server="xxxxxxx"/>
<Job CSREmail="" Category="Digital" Category2="MCSD904LAYOUT" Category3="Mat HSLPETG10022 / S257 petg 50m" Category4="No" Category5="xxxxx" Category6="2" Category7="xxxxxx" Created="2016-03-07T09:33:15-05:00" CreatedFrom="xxxxxx" CustomerID="xxxxxx" CustomerJobReference="xxxxxxx" JobFolder="xxxxxxx" Name="773592" OrderID="773592" ProjectID="773592" SubOrderID="">
<Description>xxxxxxx</Description>
</Job>
<Customer ID="332420" Name="xxxxxxx"/>
<JobParams>
<JobParam Name="steprepeat.No Across" Value="0"/>
<JobParam Name="steprepeat.No Around" Value="0"/>
<JobParam Name="steprepeat.Spacing across" Value="0"/>
<JobParam Name="steprepeat.Spacing around" Value="0"/>
<JobParam Name="steprepeat.Fronts around" Value="0"/>
<JobParam Name="steprepeat.Fronts across" Value="0"/>
<JobParam Name="steprepeat.Backs across" Value="0"/>
<JobParam Name="steprepeat.Backs around" Value="0"/>
</JobParams>
</Archive>
这是我的 Xquery:
for $t in //@Category2,
$v in //@OrderID
let $d := "MCHT65780LAYOUT"
where $t = $d
order by $v
return concat ("Job: ",$v, " Die: ", $t)
它或多或少地产生了我正在寻找的东西,但我的 "for" 循环有问题,我得到了无限的结果,而 BaseX 在 500,000 个结果处停止。如果我只在 "for" 语句中放入一项,我会得到正确数量的结果,但我需要在结果中看到 "OrderID" 和 "Category2"。
基本上我想搜索 $d 并查看哪些作业使用了该值。
Michael Kay 在下面的回答非常有效。我还想出了如何通过 "contains" 函数的部分匹配产生结果:
declare namespace a="http://www.xxxx.com/egAr/1.0";
for $j in //a:Job
let $t := $j/@Category2
let $v := $j/@OrderID
let $d := "67"
order by $v
where ($t [contains (.,$d)])
return concat ("Job: ",$v, " Die: ", $t)
我怀疑您不想考虑出现在文档中任何位置的所有 @Category2
和 @OrderId
对,而只考虑出现在同一作业元素上的对。
你可以这样写
declare namespace a="http://www.xxxx.com/egAr/1.0";
for $j in //a:Job
let $t := $j/@Category2
let $v := $j/@OrderID
let $d := "MCHT65780LAYOUT"
where $t = $d
order by $v
return concat ("Job: ",$v, " Die: ", $t)
但我可能会写成:
declare namespace a="http://www.xxxx.com/egAr/1.0";
for $j in //a:Job[@Category2 = "MCHT65780LAYOUT"]
order by $j/@OrderID
return $j/concat("Job: ", @OrderID, " Die: ", @Category2)