Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
XQuery(XPath)根据数字'对子元素和组进行计数;s_Xpath_Xquery_Flwor - Fatal编程技术网

XQuery(XPath)根据数字'对子元素和组进行计数;s

XQuery(XPath)根据数字'对子元素和组进行计数;s,xpath,xquery,flwor,Xpath,Xquery,Flwor,输入和预期输出的示例XML文件位于底部,需要帮助根据子元素的计数进行分组 <DashboardXML> <Column> <ColumnOrder>1</ColumnOrder> <ColLabel><![CDATA[test1]]></ColLabel> </Column>

输入和预期输出的示例XML文件位于底部,需要帮助根据子元素的计数进行分组

  <DashboardXML>
        <Column>
            <ColumnOrder>1</ColumnOrder>
            <ColLabel><![CDATA[test1]]></ColLabel>                
        </Column>
        <Column>
            <ColumnOrder>2</ColumnOrder>
            <ColLabel><![CDATA[t1est]]></ColLabel>       
        </Column>
          <Column>
            <ColumnOrder>3</ColumnOrder>
            <ColLabel><![CDATA[terst]]></ColLabel>       
        </Column>
    </DashboardXML>
    <DashboardXML>
        <Column>
            <ColumnOrder>1</ColumnOrder>
            <ColLabel><![CDATA[test1]]></ColLabel>                
        </Column>
        <Column>
            <ColumnOrder>2</ColumnOrder>
            <ColLabel><![CDATA[t1est]]></ColLabel>       
        </Column>
          <Column>
            <ColumnOrder>3</ColumnOrder>
            <ColLabel><![CDATA[terst]]></ColLabel>       
        </Column>
    </DashboardXML>
    <DashboardXML>
        <Column>
            <ColumnOrder>1</ColumnOrder>
            <ColLabel><![CDATA[test1]]></ColLabel>                
        </Column>
        <Column>
            <ColumnOrder>2</ColumnOrder>
            <ColLabel><![CDATA[t1est]]></ColLabel>       
        </Column>
    </DashboardXML>
    <DashboardXML>
        <Column>
            <ColumnOrder>1</ColumnOrder>
            <ColLabel><![CDATA[test1]]></ColLabel>                
        </Column>
        <Column>
            <ColumnOrder>2</ColumnOrder>
            <ColLabel><![CDATA[t1est]]></ColLabel>       
        </Column>
        <Column>
            <ColumnOrder>1</ColumnOrder>
            <ColLabel><![CDATA[test1]]></ColLabel>                
        </Column>
        <Column>
            <ColumnOrder>2</ColumnOrder>
            <ColLabel><![CDATA[t1est]]></ColLabel>       
        </Column>
    </DashboardXML>
    <DashboardXML>
        <Column>
            <ColumnOrder>1</ColumnOrder>
            <ColLabel><![CDATA[test1]]></ColLabel>                
        </Column>
        <Column>
            <ColumnOrder>2</ColumnOrder>
            <ColLabel><![CDATA[t1est]]></ColLabel>       
        </Column>
    </DashboardXML>

如果您使用的是支持XQuery 3.0的查询处理器,请使用
GROUPBY
子句

for $b in $xml/DashboardXML
let $count := count($b/Column)
where $count > 0
order by $count descending
group by $count
return <li>{$b/DashboardName/text()} = {$count} ({count ($b) }) </li>
$xml/DashboardXML中的$b的

let$count:=count($b/列)
其中$count>0
按$count降序排序
按$count分组
返回
  • {$b/DashboardName/text()}={$count}({count($b)})
  • 哪个会输出

    <li> = 1 (1) </li>
    <li> = 2 (2) </li>
    
  • =1(1)
  • =2(2)

  • 对于给定的输入(请注意缺少的
    仪表板名称
    元素,并且我更改了
    where
    子句以允许任何非零数量的
    子项)。

    请将代码和输入作为一个元素发布,以便我们可以复制它。您的XML无效(缺少根元素),并且不适合预期的输出。将原始文档发布到某个地方,或者将预期输出与提供的输入相匹配。您还应该告诉use您正在使用的XQuery的哪个版本和实现,因为XQuery 3.0知道一个
    group by
    子句XQuery 1不知道。我已经按照您的建议进行了更新-ThanksQuestion现在看起来很棒。您是否有与XQuery 3.0兼容的查询处理器,所以我解决了您的问题?然后您可以将其标记为已回答(问题左侧的复选标记)。
    2 = 2 (counts)
    3 = 2 (counts)
    4 = 1 (counts)
    
    for $b in $xml/DashboardXML
    let $count := count($b/Column)
    where $count > 0
    order by $count descending
    group by $count
    return <li>{$b/DashboardName/text()} = {$count} ({count ($b) }) </li>
    
    <li> = 1 (1) </li>
    <li> = 2 (2) </li>