Domanda

The sample XML files inputting and the expected output is at the bottom, need help to group based on the count of child elememts.

  <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>

Above is the sample XML's as input and below is the XQuery:

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

The query yields the below output (sample):

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

Now the question is how to group the output like Below:

2 = 2 (counts)
3 = 2 (counts)
4 = 1 (counts)
È stato utile?

Soluzione

Given you're using an XQuery 3.0 capable query processor, use the group by clause.

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>

Which will output

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

for the input given (notice the missing DashboardName elements and that I changed the where clause to allow any non-zero number of Column children).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top