views:

42

answers:

1

Is there a way to construct an XPath that evaluates whether an element's value is in a predefined list of values? Something akin to this:

/Location/Addr[State='TX or AL or MA']

Which would match nodes whith State elements for Texas, Alabama, or Massachusetts? I know that I can unpack the expression:

/Location/Addr[State='TX] or  /Location/Addr[State='AL'], etc...

But this is a bit cumbersome since the xpaths are quite long, as is the list of values. My google-fu isn't turning up much on the issue...

+3  A: 

You can check multiple conditions inside the same square brackets:

/Location/Addr[State='TX' or State='AL' or State='MA']

Or if you have a really long list, you can create a list of states and use the contains() function.

/Location/Addr[contains('TX AL MA', State)]

This will work fine for two-letter state abbreviations. If you want to make it more robust for longer strings you could add some spaces on the ends and check for _TX_, _AL_, etc. (where the underscores are spaces).

/Location/Addr[contains(' TX AL MA ', concat(' ', State, ' '))]
John Kugelman
Good answer (+1).
Dimitre Novatchev