?
Solved

Need help with XPath

Posted on 2012-03-12
4
Medium Priority
?
341 Views
Last Modified: 2012-03-12
I have an xml file with the following content
<Root>   
<Node>     
<SubEl1>abc</SubEl1>     
<SubEl2>def</SubEl2>     
<SubEl3>123</SubEl3>     
<SubEl4>456</SubEl4>         
</Node> 
<Node>     
<SubEl1>abc</SubEl1>     
<SubEl2>fgi</SubEl2>     
<SubEl3>124</SubEl3>     
<SubEl4>579</SubEl4>         
</Node> 
<Node>     
<SubEl1>ghi</SubEl1>     
<SubEl2>klm</SubEl2>     
<SubEl3>789</SubEl3>     
<SubEl4>012</SubEl4>         
</Node> 
</Root> 

Open in new window


I need to retrieve all the Node elements where SubEl1 has a value of "abc". Actually what I really need is a list of SubEl2 values for those Node elements.

I am having trouble writing XPath expression. Here is what I got so far:

XmlNodeList myList = xdoc.SelectNodes("Root/Node/SubEl1[text()='abc']");

Open in new window


but the above only give me a list of SubEl1, i.e. "abc", "abc",. while what I need is SubEl2, i.e. "def", "fgi"
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 total points
ID: 37709762
XmlNodeList myList = xdoc.SelectNodes("Root/Node[SubEl1='abc']/SubEl2");
0
 
LVL 4

Expert Comment

by:MichaelStaszewski
ID: 37709763
You almost have it. Your expression should be...

Root/Node/SubEl1[text()='abc']/../SubEl2

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37709788
Michaels solution will work but is highly inefficient.
Why go down and back up if you don't need to?

Also (I should have made that comment earlier) it is never a good idea to test explicitely for a text() node. XPath can split a text content in multiple text() nodes, and then this would not work
I you want to drill down to the text level, you should do
SubEl1[.='abc']
or even better
SubEl1[normalize-space(.) ='abc']
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37709798
You can't delete a question that provided you with a good solution
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question