MaheshVarma
MaheshVarma

Reputation: 2125

Excluding child elements in xpath expression

HI have the following xml.

<filters>
    <filter>
        <field>
            <name>empId</name>
            <type>int</type>
        </field>
        <op>in</op>
        <value>eID</value>
        <valueType>PERI</valueType>
    </filter>
    <filter>
        <field>
            <name>desc</name>
            <type>string</type>
        </field>
        <op>in</op>
        <value>dbDesc</value>
        <valueType>PERI</valueType>
    </filter>
</filters>

I want to retrieve only "name" element, "op" and "value" elements. Iam using the below xpath expresssion. but unable to exclude the "type" nodes which are under "field" element.

//filter//*[not(self::valueType)] and [not(following-sibling::type)]//text()

My output should be:

empId
in
eID
desc
in
dbDesc

Any help?

Upvotes: 1

Views: 494

Answers (2)

kjhughes
kjhughes

Reputation: 111686

The following XPath expression will select the text from all name, op, or value elements:

(//name|//op|//value)/text()

Update per OP request in the comments:

I want name, op, value only from the filters element.

Adjust the XPath similar to as you mention in your comment:

(//filters//name|//filters//op|//filters//value)/text()

Or, more efficiently (provided tightening it up doesn't lose any matches in your general case):

(/filters/filter/field/name|/filters/filter/op|/filters/filter/value)/text()

Upvotes: 3

paul trmbrth
paul trmbrth

Reputation: 20748

This should work:

//filter//*[self::name or self::op or self::value]/text()

Upvotes: 2

Related Questions