how to search for documents which do not have particular element or having null value in the element?

111 Views Asked by At

I want to search documents which do not have particular element or element having no child node(even text node) and the element should have a particular parent node.

for example:

sample xmls:

<root>
    <id>123</id>
    <root2>
        <number>1234</number>
    </root2>
</root>

<root>
    <id>123</id>
    <root2>
        <issue/>
        <number>1234</number>
    </root2>
</root>

<root>
    <id>123</id>
    <root2>
        <volume/>
        <number>1234</number>
    </root2>
</root>

<root-second>
    <id>123</id>
    <root2>
        <volume/>
        <issue/>
        <number>1234</number>
    </root2>
</root-second>

<root-second>
    <id>123</id>
    <root2>
        <volume/>
        <issue/>
        <number>1234</number>
    </root2>
</root-second>

New Docs:

<root>
    <id>123</id>
    <root2>
        <volume>233</volume>
        <number>1234</number>
    </root2>
</root>
<root>
    <id>123</id>
    <root2>
        <volume>233</volume>
        <issue>233</issue>
        <number>1234</number>
    </root2>
</root>
<root>
     <id>123</id>
     <root2>
        <issue>233</issue>
        <number>1234</number>
      </root2>
</root>

Now I want all xmls having <id> in <root> is 123 and <root2> can have empty <issue>, <volume> element or element(s) not present at all.

As in the above case, it should not return xmls with <root-second> as root.

It should return all xmls with <root> as root

1

There are 1 best solutions below

3
On

Assuming each XML is in its own document, you should be able to do this with:

cts:search(
  fn:doc(),
  cts:element-query(
    xs:QName("root"),
    cts:element-value-query(xs:QName("id"), "123")
  )
)

I'm not entirely clear on your question, but I think you might also be wanting to require that root2 be present under root, regardless of what it might contain. If that's the case, then:

cts:search(
  fn:doc(),
  cts:element-query(
    xs:QName("root"),
    cts:and-query((
      cts:element-value-query(xs:QName("id"), "123"),
      cts:element-query(xs:QName("root2"), cts:true-query())
    ))
  )
)