Construct an XPath query which filters nodes based on the quantity of its children?

Viewed 40

I have the following XML:

<person-group person-group-type="author">
    <name>
        <surname>Chen</surname>
        <given-names>J-Y</given-names>
    </name>
</person-group>
<person-group person-group-type="author">
    <name>
        <surname>Cisse</surname>
        <given-names>F</given-names>
    </name>
</person-group>
<person-group person-group-type="author">
    <name>
        <surname>Dahou</surname>
        <given-names>T</given-names>
    </name>
    <name>
        <surname>Foucher</surname>
        <given-names>V</given-names>
    </name>
</person-group>

I am trying to design an XPath query which will only return elements only if they have more than one element.

I've tried everything within my knowledge, and also combed through XPath and XQuery documentation for the past few hours. Nothing I've read tells me if I can filter nodes based on the quantity of their children.

Thanks.

1 Answers

Use the following XPath expression:

//person-group[count(name) > 1]

It selects all person-group elements which have more than one name child.


If you want to query the amount of all element children, you can generalize the above query to

//person-group[count(*) > 1]
Related