lajarre
lajarre

Reputation: 5162

XPath - select all nodes which are only child of type

I have something like:

<fieldset>
    <field name='field1'></field>
</fieldset>
<fieldset>
    <field name='field2'></field>
    <field name='field3'></field>
</fieldset>
<fieldset>
    <field name='field4'></field>
    <text></text>
</fieldset>

I want to select all the field nodes which are only children on type field.

Ie. field1 matches, and field4 as well (the text node is not of type field).

I can't make it work using sibling and this neither:

//fieldset/field[not(count(../field))>1]

Upvotes: 1

Views: 344

Answers (1)

alecxe
alecxe

Reputation: 474161

You can use //fieldset[count(field)=1]/field xpath expression.

Demo using xmllint:

$ xmllint input.xml --xpath '//fieldset[count(field)=1]/field'
<field name="field1"/>
<field name="field4"/>

Upvotes: 3

Related Questions