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...
Then you'd select all element nodes with an @id -attribute-value equal to 'Passwd' in the whole document. Just add //* in the XPath -- it highlights --- various page elements. This would select all element nodes in the whole document.
$xml->xpath("(//person)[firstname[contains(., 'Kerr')]]"); then it works fine.
To define an XPath expression that checks if a string element is empty, you must use the operator != . This example shows how to define an XPath expression that evaluates to true when a repeating element, which is referred to as a sequence, is empty. The effective Boolean value of an empty sequence is false.
/bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00. /bookstore/book[price>35.00]/title. Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00.
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, ' '))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With