26

I want to select nodes for which a specific attribute does not exist. I've tried the Not() function, but it doesn't work. Is there a way for this?

Example: The following Xpath query:

group/msg[not(@owner)]

Should retrieve the first node but not the 2nd one. However, both SketchPath (tool to test Xpath queries) and my C# code consider that the 2 nodes are ok.

<group>
    <msg id="EVENTDATA_CCFLOADED_XMLCONTEXT"  numericId="14026"  translate="False"  topicId="302"  status="translated" >
        <text>Context</text>
        <comment></comment>
    </msg>
    <msg id="EVENTDATA_CCFLOADED_XMLCONTEXT_HELP"  numericId="14027"  translate="False"  topicId="302"  status="translated"  owner="EVENTDATA_CCFLOADED_XMLCONTEXT" >
        <text>Provides the new data displayed in the Object.</text>
        <comment></comment>
    </msg>
</group>

In fact the Not() function works correctly, it's just that I had other conditions and parentheses weren't set correctly. errare humanum est.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Antoine
  • 5,055
  • 11
  • 54
  • 82
  • 1
    If so, please CLOSE the question -- it is misleading and time consuming for other people. – Dimitre Novatchev Dec 12 '08 at 16:40
  • I closed the question as "no longer relevant", if you don't mind. – PhiLho Dec 12 '08 at 20:20
  • 3
    doesn't `group/msg[not(@owner) or @otherAttribute="true"]` worked for you? – CARLOS LOTH Mar 18 '12 at 12:20
  • ***For complete and correct answer*** without the distraction of the false alert raised by this question, see [**XPath for elements with attribute not equal or does not exist**](https://stackoverflow.com/questions/34632699/xpath-for-elements-with-attribute-not-equal-or-does-not-exist) – kjhughes Sep 26 '17 at 13:32

2 Answers2

17

In some old and not very standard XPath engine I had to use string-length(@attr)=0 for the same reason.

bluish
  • 26,356
  • 27
  • 122
  • 180
Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
1

Works for me when testing with XPath Explorer (based on Jaxen library).
So I guess it is indeed depending on the XPath implementation.

PhiLho
  • 40,535
  • 6
  • 96
  • 134