Brian Myers Brian Myers - 4 months ago 7
Java Question

In xPath, how do you select all nodes not matching a subset?

I'm trying to grab a large set of xml nodes all at the same level (who's element names are all different) while excluding a certain subset of nodes. For example:

<root>
<foo>...</>
<foo>...</>
<bar>...</>
<bar>...</>
<baz>...</>
<spam>...</>
<eggs>...</>
<toast>...</>
</root>


so I want an xPath expression to give me the spam eggs and toast node list while excluding the foo, bar and baz nodes. I know the nodes I want to exclude ahead of time but have no clue what nodes will be left over.

Answer

Combine three Xpathes

/root/spam | /root/eggs | /root/toast

Or write the same in different way

/root/*[contains("spam|eggs|toast", name())]

UPDATE:

To exclude a set of nodes spam, eggs and toast

/root/*[not(contains("spam|eggs|toast", name()))]