Andy Thornton
Andy Thornton

Reputation: 638

How to apply the :empty selector to an XML document

I have a XML document that I am trying to select every node that has no child. I thought I could accomplish this with something like:

$(XML).find(':empty')

However, this just returns and empty set. Here is a quick glimpse at my XML document.

<BOM>
  <BOMHeader>
    <ID>ID Text</ID>
    <Classification>ClassificationText</Classification>
    <Version>VersionText</Version>
    <MakeEffectiveDate>MakeEffectiveDateText</MakeEffectiveDate>
    <ExpirationDate>ExpirationDate Text</ExpirationDate>    
    <MaterialID>Material ID Text</MaterialID>
    <MaterialDescription>Material Description</MaterialDescription>
    <Quantity>Quantity Text</Quantity>
    <UOM>UOM Text</UOM>
    <CustomProperties>
      <Example>Example Text</Example>
    </CustomProperties>
  </BOMHeader>
</BOM>

So want I need is to be able to read in this XML and just return the set of nodes equal to ID, Classification, Version, MakeEffectiveDate, ExpirationDate, MaterialID, Material Description, Quantity, UOM, and Example.

Any thoughts on how to accomplish this? Many thanks in advance.

Upvotes: 1

Views: 42

Answers (2)

Esailija
Esailija

Reputation: 140228

It's because none of those elements are :empty. :empty means element that has no child nodes, such as <elem></elem>. Try :not(:has(*)):

$(XML).find(":not(:has(*))")

Upvotes: 0

Ram
Ram

Reputation: 144699

You can use the filter() method:

var $bachelors = $(XML).find('*').filter(function() {
                   return $(this).children().length === 0
                 })

Upvotes: 1

Related Questions