471,594 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 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 3674


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bdinmstig | last post: by
2 posts views Thread by nkunapa | last post: by
10 posts views Thread by Michael C# | last post: by
9 posts views Thread by David Thielen | last post: by
3 posts views Thread by SD | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.