78

XML: <A><B></B></A> or <A></A>

I want to get all A nodes that do not have any B children.

I've tried

/A[not(B)]  
/A[not(exists(B))]

without success

I prefer a solution with the syntax /*[local-name()="A" and .... ], if possible. Any ideas that works?

Clarification. The xml looks like:

<WhatEver>
  <A>
    <B></B>
  </A>
</WhatEver> 

or

<WhatEver>
  <A></A>
</WhatEver>
6
  • 3
    Without seeing your XML I would say that "/A[not(B)]" is the way to go. What's wrong with that?
    – Tomalak
    Commented May 14, 2009 at 8:43
  • Also, there should be only one root element, so you're either getting all your XML back or none. Maybe //A[not(B)] or /*/A[not(B)]?
    – alamar
    Commented May 14, 2009 at 8:46
  • With the "abstract" syntax /A/B, first line after "XML:", I meant <A><B>,just as Tomalak and alamar thought. Commented May 14, 2009 at 12:52
  • or <A><B><B><A> or <A><A> to be exact. Commented May 14, 2009 at 12:53
  • Sorry, of course with endings <A><B></B></A> or <A></A>. Commented May 14, 2009 at 13:00

5 Answers 5

60

Maybe *[local-name() = 'A' and not(descendant::*[local-name() = 'B'])]?

Also, there should be only one root element, so for /A[...] you're either getting all your XML back or none. Maybe //A[not(B)] or /*/A[not(B)]?

I don't really understand why /A[not(B)] doesn't work for you.

~/xml% xmllint ab.xml
<?xml version="1.0"?>
<root>
    <A id="1">
            <B/>
    </A>
    <A id="2">
    </A>
    <A id="3">
            <B/>
            <B/>
    </A>
    <A id="4"/>
</root>
~/xml% xpath ab.xml '/root/A[not(B)]'
Found 2 nodes:
-- NODE --
<A id="2">
    </A>
-- NODE --
<A id="4" />
3
  • I can confirm, /A[not(B)] doesn't always work as advertised. I have an example using Xalan in XMLspear where <xsl:for-each select="exsl:node-set($theFields)/*[name()='field'][not(child::*[name()='disabled'])]"> [DO STUFF HERE] </xsl:for-each> produces different output from <xsl:for-each select="exsl:node-set($FLD)/field[not(disabled)]"> [DO STUFF HERE] </xsl:for-each> In fact, in the same situation, using select="exsl:node-set($theFields)/*[name()='field']" produces output, but select="exsl:node-set($theFields)/field" doesn't at all.
    – John Smith
    Commented Mar 29, 2019 at 3:37
  • @MichaelKupietz why don't you create a new question about that?
    – alamar
    Commented Mar 29, 2019 at 9:03
  • At some point, when I have time to whittle a huge XSLT down to a minimum working demonstration, I may. At this point, though, I was just responding to the comment implying that /A(not[B]) ought to do what the OP says it doesn't, simply because I observed the same thing OP did. Evidently, it doesn't, at least, not always. That confirmation might spare some future reader of this page some confusion.
    – John Smith
    Commented Mar 30, 2019 at 3:25
25

Try this "/A[not(.//B)]" or this "/A[not(./B)]".

1
  • 5
    "Try this" answers are low-value on Stackoverflow because they do very little to education/empower the OP and thousands of future researchers. Commented Apr 4, 2020 at 3:34
12

The first / causes XPath to start at the root of the document, I doubt that is what you intended.

Perhaps you meant //A[not(B)] which would find all A nodes in the document at any level that do not have a direct B child.

Or perhaps you are already at a node that contains A nodes in which case you just want A[not(B)] as the XPath.

3

If you are trying to get A anywhere in the hierarchy from the root, this works (for xslt 1.0 as well as 2.0 in case its used in xslt)

//descendant-or-self::node()[local-name(.) = 'a' and not(count(b))]

OR you can also do

//descendant-or-self::node()[local-name(.) = 'a' and not(b)]

OR also

//descendant-or-self::node()[local-name(.) = 'a' and not(child::b)]

There are n no of ways in xslt to achieve the same thing.

Note: XPaths are case-sensitive, so if your node names are different (which I am sure, no one is gonna use A, B), then please make sure the case matches.

3
  • FYI, the question is about XPath, not XSLT. They are distinct (though related) technologies. Otherwise, your answer is technically correct. As an aside though, "/root/A[not(B)]" works just fine as @Alamar mentioned. ;-)
    – Cerebrus
    Commented May 14, 2009 at 9:16
  • Thanks for the correction. I meant xpaths in my last line. Have edited the post. As for "/root/A[not(B)]" I already ran it successfully. But yet posted alternate xpaths as it wasnt working for Martin. Which is also y i mentioned case-sensitivity. Commented May 14, 2009 at 9:32
  • How would one select all A with immediate children other than B? (A and all child nodes, as long as they contain one or more B elements only)
    – CodeManX
    Commented Jan 15, 2014 at 16:47
3

Use this:

/*[local-name()='A' and not(descendant::*[local-name()='B'])]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.