Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help with XPath

Posted on 2012-03-12
4
Medium Priority
?
342 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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