1

输入和预期输出的示例 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>

上面是作为输入的示例 XML,下面是 XQuery:

for $b in /DashboardXML where count($b/Column) > 0 order by count($b/Column) return <li>{count($b/Column)} </li>

查询产生以下输出(示例):

    <li>3</li>
    <li>3</li>
    <li>2</li>
    <li>4</li>
    <li>2</li>

现在的问题是如何将输出分组如下:

2 = 2 (counts)
3 = 2 (counts)
4 = 1 (counts)
4

1 回答 1

1

假设您正在使用支持 XQuery 3.0 的查询处理器,请使用该group by子句。

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>

对于给定的输入(请注意缺少的DashboardName元素,并且我更改了where子句以允许任何非零数量的Column子代)。

于 2014-01-29T12:25:51.970 回答