I think I have a solution that does not require interaction with namespaces.
Here you select all the relevant divs :
//div[@id[starts-with(.,"post_message")]]
But you said that you only want the βfirstβ (I assume you mean the first βhitβ on the whole page?). Here is a small modification that selects only the first match result :
(//div[@id[starts-with(.,"post_message")]])[1]
They use a dot to represent the id value in the starts-with() function. You may need to hide special characters in your language.
This works fine for me in PowerShell:
# Load a sample xml document $xml = [xml]'<root><div id="post_message_somenumber"/><div id="not_post_message"/><div id="post_message_somenumber2"/></root>' # Run the xpath selection of all matching div's $xml.selectnodes('//div[@id[starts-with(.,"post_message")]]')
Result:
id -- post_message_somenumber post_message_somenumber2
Or, just for the first match:
# Run the xpath selection of the first matching div $xml.selectnodes('(//div[@id[starts-with(.,"post_message")]])[1]')
Result:
id -- post_message_somenumber
Vimes source share