Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath get nodes that do not have complete empty children

Tags:

xpath

Scenario (minified):

<a>
  <Sections>
    <Section>
      <Title></Title>
      <Subject></Subject>
      <Body></Body>
    </Section>
    <Section>
      <Title/>
      <Subject/>
      <Body/>
    </Section>
    <Section>
      <Title>Hello</Title>
      <Subject></Subject>
      <Body></Body>
    </Section>
    <Section>
      <Title></Title>
      <Subject>I have a problem</Subject>
      <Body></Body>
    </Section>
  </Sections>
</a>

Question:

What XPath should I use to return a list of <Section/> nodes that have at least one child node not empty such that this is returned:

    <Section>
      <Title>Hello</Title>
      <Subject></Subject>
      <Body></Body>
    </Section>
    <Section>
      <Title></Title>
      <Subject>I have a problem</Subject>
      <Body></Body>
    </Section>

In other words, <Section> nodes with completely empty child nodes should be filtered out.

like image 743
mrd3650 Avatar asked Dec 30 '25 12:12

mrd3650


1 Answers

Try:

.//Section[./*/node()]

i.e. look for Section elements that have children that have children (text nodes or element nodes). This may or may not work depending on your requirement for empty child nodes, and may therefore need refinement.

like image 51
paul trmbrth Avatar answered Jan 07 '26 05:01

paul trmbrth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!