473,406 Members | 2,273 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,406 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
6 2008
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


Zhiv Kurilka wrote:

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
Here is a C# example:

foreach (XmlNode b in xmlDocument.SelectNodes(@"a/b[c = '10']")) {
XmlNodeList names = b.SelectNodes("name");
for (int i = 0; i < names.Count; i++) {
if (i == names.Count - 1) {
Console.WriteLine(names[i].InnerText);
}
else {
Console.Write("{0}/", names[i].InnerText);
}
}
}

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 2 '06 #3
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
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 #5
Thank you very much
"Martin Honnen" <ma*******@yahoo.deschrieb im Newsbeitrag
news:eh**************@TK2MSFTNGP03.phx.gbl...
>

Zhiv Kurilka wrote:

>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

Here is a C# example:

foreach (XmlNode b in xmlDocument.SelectNodes(@"a/b[c = '10']")) {
XmlNodeList names = b.SelectNodes("name");
for (int i = 0; i < names.Count; i++) {
if (i == names.Count - 1) {
Console.WriteLine(names[i].InnerText);
}
else {
Console.Write("{0}/", names[i].InnerText);
}
}
}

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 2 '06 #6
Here is an example written in vb.net

.... load xmldoc.
Dim TagDataNodes As XmlNodeList
Dim TagDataNode As XmlNode

TagDataNodes = XMLDoc.SelectNodes("//C[.='10']")

For Each TagDataNode In TagDataNodes
GetPath(TagDataNode)
Next
End Sub

Private Function GetPath(ByRef node As XmlNode) As String

Dim FullPath As String = node.Name.ToString

Dim ParentNode As XmlNode

ParentNode = node.SelectSingleNode("..")

While Not ParentNode Is Nothing

If ParentNode.SelectSingleNode("..") Is Nothing Then
FullPath = "\" + FullPath
Else
FullPath = ParentNode.Name.ToString + "\" + FullPath
End If

ParentNode = ParentNode.SelectSingleNode("..")

End While

Console.Write(FullPath & vbCrLf)

Return FullPath

End Function

Zhiv Kurilka wrote:
Thank you very much
"Martin Honnen" <ma*******@yahoo.deschrieb im Newsbeitrag
news:eh**************@TK2MSFTNGP03.phx.gbl...


Zhiv Kurilka wrote:

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
Here is a C# example:

foreach (XmlNode b in xmlDocument.SelectNodes(@"a/b[c = '10']")) {
XmlNodeList names = b.SelectNodes("name");
for (int i = 0; i < names.Count; i++) {
if (i == names.Count - 1) {
Console.WriteLine(names[i].InnerText);
}
else {
Console.Write("{0}/", names[i].InnerText);
}
}
}

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 3 '06 #7

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...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.