473,232 Members | 1,682 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,232 software developers and data experts.

XPath syntax to check an attribute of all child nodes?

Bob
I have sitemap like XML, of which every element has an attribute "url", e.g.

<record menu_id="240" name="Countries and States" url="Countries.aspx">
<record2 page_id="54" url="CountriesEdit.aspx" />
<record3 page_id="27" url="Regions.aspx">
<record4 page_id="55" url="RegionsEdit.aspx" />
</record3>
</record>

I need to compare whether a string value contains the url or not. If so,
get the node. Here's my code:

XmlNode n = xml.SelectSingleNode("//*[contains('" + myString + "', @url)]");

This doesn't work. I know it works against the "record" node if the XPath
is //record[......]. How can I make it to search every element node
regardless of the name?

Thanks a lot
Bob
Nov 12 '05 #1
2 3749


Bob wrote:
I have sitemap like XML, of which every element has an attribute "url", e.g.

<record menu_id="240" name="Countries and States" url="Countries.aspx">
<record2 page_id="54" url="CountriesEdit.aspx" />
<record3 page_id="27" url="Regions.aspx">
<record4 page_id="55" url="RegionsEdit.aspx" />
</record3>
</record>

I need to compare whether a string value contains the url or not. If so,
get the node. Here's my code:

XmlNode n = xml.SelectSingleNode("//*[contains('" + myString + "', @url)]");

This doesn't work. I know it works against the "record" node if the XPath
is //record[......]. How can I make it to search every element node
regardless of the name?


The code and the XPath expression look fine to me, here is a complete
console example

public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test2005043001.xml");
XmlNode node = xmlDocument.SelectSingleNode("//*[contains('" +
args[0] + "', @url)]");
if (node != null) {
Console.WriteLine("Found node {0} with type {1}.", node,
node.NodeType);
}
else {
Console.WriteLine("No matching node found.");
}
}

if you pass on the argument Regions.aspx on the command line for
instance then the matching element node is found.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Bob
HI Martin:

I ran your code against the sample XML I gave and it works. However, my
real XML has many "record" elements, and it somehow just returns the entire
XML tree. The url is unique across all elements but it somhow just doesn't
return the single element.

Is there a way to look for the element name "record*" since the element
names are "record", "record1", "record2" etc.?

Thanks again
Bob
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...


Bob wrote:
I have sitemap like XML, of which every element has an attribute "url", e.g.
<record menu_id="240" name="Countries and States" url="Countries.aspx">
<record2 page_id="54" url="CountriesEdit.aspx" />
<record3 page_id="27" url="Regions.aspx">
<record4 page_id="55" url="RegionsEdit.aspx" />
</record3>
</record>

I need to compare whether a string value contains the url or not. If so, get the node. Here's my code:

XmlNode n = xml.SelectSingleNode("//*[contains('" + myString + "', @url)]");
This doesn't work. I know it works against the "record" node if the XPath is //record[......]. How can I make it to search every element node
regardless of the name?


The code and the XPath expression look fine to me, here is a complete
console example

public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test2005043001.xml");
XmlNode node = xmlDocument.SelectSingleNode("//*[contains('" +
args[0] + "', @url)]");
if (node != null) {
Console.WriteLine("Found node {0} with type {1}.", node,
node.NodeType);
}
else {
Console.WriteLine("No matching node found.");
}
}

if you pass on the argument Regions.aspx on the command line for
instance then the matching element node is found.

--

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

Nov 12 '05 #3

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

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
2
by: nkunapa | last post by:
Hi: Is there a way in XPATH to find the nearest node of the node in context with a certain attribute value. Here is my problem. I have the following XML and I am trying to add all the nodes with...
7
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange";...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
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...
4
by: SkyHook | last post by:
1. Under the topic "Select Nodes Using XPath Navigation" it says: "All XmlNodeList objects are synchronized with the underlying document, therefore if you ... modify the value of a node, that node...
3
by: SD | last post by:
Hi, How can i get all the nodes with attribute Name = "Publisher" or Name="Administrator" using XPath query and C# for this xml doc? <GetRoleCollectionFromUser...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.