I’m looking to select a collection of elements based on an array of ID names. I’m currently using a giant OR statement essentially:
//*[@id='apple']|//*[@id='orange']|//*[@id='banana']
But building that string manually seems messy. Is there something like a nice SQL-esque "WHERE IN [a,b,c]" operator that I could be using?
I am using the HTTPAgilityPack for ASP.Net which I think equates to XPath1.o (feel free to correct me on that.)
Thanks.
First, you could simplify this by using
or. This avoids repeating the//*multiple times although you till specify the@id=part multiple times:A more elegant solution is to check against a list of acceptable ids. Now if you’re using XPath 1.x then you’ll have to do a bit of gymnastics to get contains() to do your bidding. Specifically, notice that I’ve got spaces on both ends of the first string, and then concatenate spaces to each end of
@idbefore looking for a match. This is to prevent an@idof"range"from matching, for example.If you have are using XPath 2.0 then the way forward is simpler thanks to the addition of sequences to the language: