34 red...">

Xpath - selection based on several criteria

I have the following xml file:

<persons> <person name="shawn"> <age>34</age> <hair style="spikes">red</hair> </person> <person name="mike"> <age>36</age> <hair style="bald">red</hair> </person> <person name="shawn"> <age>38</age> <hair style="bald">red</hair> </person> </persons> 

Using XPath in C #, is it possible to select the person element, where the name is "Shawn" and the style is hair = "bald"?

I tried using:

 XElement.XPathSelectElement("//person[@name='shawn'] | //person/hair[@style='bald'] 

but it gives me a link to the hair element, not the human element.

Thanks in advance:)

Peter

+4
source share
1 answer

If you want someone called shawn and be bald (which suggests the name of the question), you want:

 //person[@name='shawn' and hair/@style='bald'] 

If you want people called shawn or bald (which is similar to your attempt in your question), you want:

 //person[@name='shawn' or hair/@style='bald'] 
+9
source

All Articles