473,378 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Simple xpath question

Dear all,
supposed I have xml file
<a>
<b>
<name>ggg</name>
<c>10</c>
<name>ddd</name>
</b>

<b>
<name>zzz</name>
<c>10</c>
<name>yyyy</name>
</b>
<b>
<name>zzz</name>
<c>12</c>
<name>xxxxx</name>
</b>

</a>
Lets say I need to select all nodes where c=10 and print a path of names
dim my_list as xmlnodelist =myRootNode.SelectNodes("//a/b[c='10']")
Now I need to print a path of nodes name. The result should be:
---------------------
ggg/ddd
zzz/yyy
--------------------
Please help me. How can I do that? I have no idea how to get this info from
my_list
Aug 2 '06 #1
3 854
Sorry, error in xml file. Should be:

<root>
<a>
<name>ddd</name>
<b>
<name>ggg</name>
<c>10</c>
</b>
</a>

<a>
<name>yyyy</name>
<b>
<name>zzz</name>
<c>10</c>
</b>
</a>
<a>
<name>zzz</name>
<b>

<c>12</c>
<name>xxxxx</name>
</b>
</a>

</root>
Aug 2 '06 #2
Something like (C# - and I've been lazy using string concatenation):

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlElement el in doc.SelectNodes("/root/a/b[c='10']"))
{
string name = el.SelectSingleNode("name").InnerText, path = "";
foreach (XmlElement pathEl in
el.SelectNodes("ancestor-or-self::*/name"))
{
path += "/" + pathEl.InnerText;
}
Debug.WriteLine(name + ": " + path);
}

Marc
Aug 2 '06 #3
Thanks just what I needed
"Marc Gravell" <ma**********@gmail.comschrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP04.phx.gbl...
Something like (C# - and I've been lazy using string concatenation):

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach (XmlElement el in doc.SelectNodes("/root/a/b[c='10']"))
{
string name = el.SelectSingleNode("name").InnerText, path = "";
foreach (XmlElement pathEl in
el.SelectNodes("ancestor-or-self::*/name"))
{
path += "/" + pathEl.InnerText;
}
Debug.WriteLine(name + ": " + path);
}

Marc

Aug 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joshua Beall | last post by:
Hi All, I have a task that should be very simple but I'm running into trouble. All I want to do is query a document using XPath, and save the resulting XML in a string. Here's that I am trying...
0
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
9
by: Jon Thackray | last post by:
I'm trying to use XPath to enable me to number items within sections in a paper. I have so far tried about ten things, all of which it seemed to me should work, and none of which do. Essentially...
3
by: Kathy Burke | last post by:
Hi again, I'm using the following xpath (works in visualizer) with a SelectSingleNode("xpath") statement. //Station/(WI])]/@order Problem is I get an error "expression passed to this method...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
6
by: Tommy | last post by:
For a XML fragment like below <ns0:BusCar_Request xmlns:ns0="http://BTSDG_SQL"> <ns0:sync> <ns0:after> <ns0:BusinessCards NameOnCard="NameOnCard_1" TitleOnCard="TitleOnCard_1" Quantity="10" />...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
6
by: Armel Asselin | last post by:
Hello, I'm searching for a simple command line tool to manipulate XML files. The idea would be commands such as that: xmanip-tool set /document/xpath/@name="value" remove //wrong-nodes add...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.