Strange XPath

lordofduct

Well-known member
Joined
Jun 24, 2010
Messages
71
Programming Experience
3-5
Does anyone know if there is a syntax for XPath that supports "possible attribute".

Basically I want a XPath that tests for two attributes of a node equal to some value. BUT if one of those attributes doesn't exist it ignores testing that attribute and considers itself successful.


Say I have a node with children as follows:

VB.NET:
<parnode>
	<node id="a" other="1" />
	<node id="b" other="1" />
	<node id="b" />
	<node id="b" other="2" />
</parnode>

and I said something like:

VB.NET:
parnode.SelectNodes("./node[@id="b" andMaybe @other="1"]")

the returned nodes would be:

VB.NET:
	<node id="b" other="1" />
	<node id="b" />





I know it's weird, but it's something I'm looking for... it'd also be nice if it was greedy towards the attribute existing. So say in this case if I said SelectSingleNode it'd return the one that has a 'other' attribute no matter the order because it prefers that the attrib does exist then if it doesn't.



In the mean time I just do two tests...

If for single node, I test with the 'other' attrib, if it doesn't exist, then test with out the 'other' attrib.

For a nodeList, I test with the 'other' attrib, then a test where it doesn't have the attrib, and combine the two.
 
nevermind, I answered my own question...

VB.NET:
parnode.SelectSingleNode("./node[(@id=""b"" and @other=""1"") or (@id=""b"" and not(@other))]")

Though I'm still hoping for something that is greedy for the 'other' attrib and only moves on to the otherside of the 'or' if the first side fails on all nodes.
 
Back
Top