a-a-h
a-a-h

Reputation: 47

Is it possible to find nodes containing a "simple list" using xpath 1.0?

is it possible to write an xpath 1.0 expression that finds for a given node all its direct child nodes that have one or more child nodes again, where all this child nodes have the same name and they do not have child nodes again?

Example:

<root>
  <`mynode`>
    <findthis>
      <entry>a</entry>
      <entry>b</entry>
      <entry>c</entry>
    </findthis>
    <ignorethis>
      <foo>a</foo>
      <bar>b</bar>
    </ignorethis>
    <ignorethat>
      <child>
        <childofchild></childofchild>
      </child>
    </ignorethat>
  <mynode>
</root>

For mynode, the xpath expression should find node findthis, but not ignorethis (because child nodes have different names), and not ignorethat (because its child node has a child again). I could of course do this programmatically in multiple steps (Perl with XML::LibXML), but I am curious if I can do this with a single xpath.

Searched via google but did not find i solution.

Upvotes: 0

Views: 45

Answers (1)

Michael Kay
Michael Kay

Reputation: 163595

Perhaps something like

root/mynode/*[
  *[not(*)] and 
  not(*[position()>1 and name(.) != name(preceding-sibling::*[1])])]

Upvotes: 2

Related Questions