XPath 1.0 to find if an element's value is in a list of values

后端 未结 2 1189
广开言路
广开言路 2020-12-03 02:29

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[Sta         


        
2条回答
  •  一个人的身影
    2020-12-03 03:03

    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, ' '))]
    

提交回复
热议问题