Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 为什么XQuery表达式返回错误的元素计数?_Xml_Count_Xml Parsing_Xquery_Xquery 3.0 - Fatal编程技术网

Xml 为什么XQuery表达式返回错误的元素计数?

Xml 为什么XQuery表达式返回错误的元素计数?,xml,count,xml-parsing,xquery,xquery-3.0,Xml,Count,Xml Parsing,Xquery,Xquery 3.0,我要返回每个计划的L1和L0项目数,以及每个计划的警告数 这实际上是一种“如果”的情况 我尝试了下面的XQuery,它对L1、L0和警告进行计数,但对所有警告进行计数,而不是只对带有value=“yes”的警告进行计数 XML示例: <?xml version="1.0" encoding="UTF-8"?> <PROFILE name="profile1"> <SCHEDULE name="schedule1"> <L0>

我要返回每个计划的L1和L0项目数,以及每个计划的警告数

这实际上是一种“如果”的情况

我尝试了下面的XQuery,它对L1、L0和警告进行计数,但对所有警告进行计数,而不是只对带有
value=“yes”
的警告进行计数

XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<PROFILE name="profile1">
    <SCHEDULE name="schedule1">
        <L0>
            <ATTRIBUTE NAME="Warnings">
                <VALUE>No</VALUE>
            </ATTRIBUTE>
            <L1>
                <ATTRIBUTE NAME="Warnings">
                    <VALUE>No</VALUE>
                </ATTRIBUTE>
            </L1>
            <L1> 
                <ATTRIBUTE NAME="Warnings">
                    <VALUE>No</VALUE>
                </ATTRIBUTE>
            </L1>
            <L1>
                <ATTRIBUTE NAME="Warnings">
                    <VALUE>Yes</VALUE>
                </ATTRIBUTE>
            </L1>
            <L1></L1>
        </L0>
        <L0>
            <ATTRIBUTE NAME="Warnings">
                <VALUE>No</VALUE>
            </ATTRIBUTE>
            <L1></L1>
        </L0>
    </SCHEDULE>
    <SCHEDULE name="schedule2">
        <L0>
            <L1></L1>
            <L1></L1>
            <L1></L1>
            <L1></L1>
        </L0>
        <L0>
            <L1></L1>
        </L0>
        <L0>
            <L1></L1>
            <L1></L1>
            <L1></L1>
        </L0>
    </SCHEDULE>
</PROFILE>

不
不
不
对
不

事实上,L0警告也算错了——但碰巧的是,它们的结果是正确的

尝试返回
$schedule/L0/L1/ATTRIBUTE[@NAME=“Warnings”]/VALUE/string()=“Yes”
(无聚合)以了解问题所在。此子查询返回一个布尔值,如果左侧的任何值与右侧的任何值匹配(在本例中,该值仅为单个值),则该布尔值为true。换句话说,如果任何警告为
Yes
,则语句将变为
true
。如果没有匹配项,则返回
false
。在XQuery中,
=
具有一组语义

现在,您正在计算结果的数量,无论布尔结果是什么,都是
1

总而言之,代码中有两个问题:

  • 如果要比较单个元素,请不要使用
    =
    ,而是使用
    eq
    (还有
    lq
    gq
    le
    ge
    ne
    用于
    =/code>、
    !=
  • 不要比较结果序列(它将返回一个布尔值),而是使用谓词对其进行过滤
L0警告的正确子查询将是

count($schedule/L0/ATTRIBUTE[@NME = "Warnings"]/VALUE[string() eq "Yes"])
类似地,对于L1警告:

count($schedule/L0/L1/ATTRIBUTE[@NAME = "Warnings"]/VALUE[string() eq "Yes"])

这里要非常完整:这是现在运行良好的XQuery:

xquery version "3.0";

let $nl := "&#10;"
let $pipe := "&#124;"

for $profiles in doc("maik test.xml")/PROFILE
for $schedule in $profiles/SCHEDULE
let $schedulename := $schedule/@name
group by $schedulename
return ($nl, 
$schedulename, $pipe, "L0 count:", count($schedule/L0),
$pipe, "L0 Warnings:", count($schedule/L0/ATTRIBUTE[@NAME = "Warnings"]/VALUE[string() eq "Yes"]),
$pipe, "L1 count:", count($schedule/L0/L1),
$pipe, "L1 Warnings:", count($schedule/L0/L1/ATTRIBUTE[@NAME = "Warnings"]/VALUE[string() eq "Yes"]))

+1对于包含示例输入、您尝试过的内容和清晰的问题陈述的好问题。对于一个完美的问题,还要加上当前(错误)和预期的输出——但问题已经完全好了。事实上,我对这个问题有一个问题。当计划名称是唯一的时,为什么要按计划名称分组?您是否正在尝试编写一个查询,以处理计划名称不唯一的输入?如果是,则不清楚在本例中预期的输出是什么。@MichaelKay,如果没有分组,my count函数将无法正常工作?我想知道每个计划中“warnings value=yes”节点的数量。@Maik如果组的大小都是1,那么分组就完全没有意义了。感谢您的回答和努力!现在,这是完全有道理的。我不知道
值[string()eq“Yes”])
语法。真的很棒!丹麦和德国汉堡!
xquery version "3.0";

let $nl := "&#10;"
let $pipe := "&#124;"

for $profiles in doc("maik test.xml")/PROFILE
for $schedule in $profiles/SCHEDULE
let $schedulename := $schedule/@name
group by $schedulename
return ($nl, 
$schedulename, $pipe, "L0 count:", count($schedule/L0),
$pipe, "L0 Warnings:", count($schedule/L0/ATTRIBUTE[@NAME = "Warnings"]/VALUE[string() eq "Yes"]),
$pipe, "L1 count:", count($schedule/L0/L1),
$pipe, "L1 Warnings:", count($schedule/L0/L1/ATTRIBUTE[@NAME = "Warnings"]/VALUE[string() eq "Yes"]))