Jesper wrote:
Say I have the following xml, below, and would like to get the second <bar>
element with the following xpath expression "/foo/bar[i='1' and j='2']". This
only works if the 'i' attribute value differs from the first and second bar
element - why?
I can't see any ambiguity issues.
<foo>
<bar i='1'/>
<bar i='1' j='2'/>
</foo>
Attribute nodes are selected with @attribute-name e.g.
/foo/bar[@i = '1' and @j = '2']
Using that I don't have any problems to find the second bar element, C#
sample program code is
string xmlMarkup = @"<foo>
<bar i='1'/>
<bar i='1' j='2'/>
</foo>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlMarkup);
XmlNode node = xmlDocument.SelectSingleNode(
@"/foo/bar[@i = '1' and @j = '2']");
if (node != null) {
Console.WriteLine("Found " + node.OuterXml);
}
else {
Console.WriteLine("No node found.");
}
which outputs
Found <bar i="1" j="2" />
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/