0

I'd like to know how to search for a string within an xml document. The object type is System.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

1
  • The question is not well-defined. When you say "anything with the document", are you including element names and attribute names? Or only text content/value of elements and attributes? Also, what information do you need back? The name of the attribute or closest element containing the text? the full ancestry path? Commented Sep 13, 2010 at 16:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just ran into another issue. How can I include this xpath query to also search through comments in the xml doc?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.