473,403 Members | 2,183 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,403 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 3759


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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.