XPath Powershell query for a string
I'd like to know how to search for a string within an xml document. The object type is S开发者_如何转开发ystem.Xml.XmlNode.XmlDocument. The string can be anything with the document. I.e. attribute or element.
I tried
Select-Xml -Xml $xml -XPath "./Test"
but got no results
The pattern you are trying to use selects root nodes named Test
.
You could use the pattern (//text()|//@*)[contains(string(), "test")]
, that selects the attributes that contain the string test
or the text nodes that contain it (i.e. not the elements).
But you want to select the elements, right? Using (//*|//@*)[contains(., "test")]
does that, but it selects elements that contain the string test
, even if it is through some child element, which is not what is wanted either.
So I guess you'll have to use something like (//*[contains(text(), "test")]|//@*[contains(., "test")])
, which gives you what you want, but is not very pretty.
精彩评论