0

I want to count all the r elements that don't have the text "unspecified" within them.

<!-- test.xml -->
<e>
<r/>
<r/>
<r>hi</r>
<r>there</r>
<r>you</r>
<r>all</r>
<r>unspecified</r>
<r>unspecified</r>
</e>

I am employing the following XQuery:

let $r_nodes := count(doc('test.xml')//r[text() ne 'unspecified'])
return
  $r_nodes

The $r_nodes variable gives me 4 not 6 as I was expecting,

har07
  • 88,338
  • 12
  • 84
  • 137
richardtk_1
  • 751
  • 3
  • 11
  • 33

2 Answers2

2

You are trying to compare the effective string value of the text nodes (hence text()). Any empty element will not have a text node. So instead your query should look like:

let $r_nodes := count(doc('test.xml')//r[text() ne 'unspecified'])
return
  $r_nodes

In this way you compare the effective string value of the r element and not it's text nodes.

adamretter
  • 3,885
  • 2
  • 23
  • 43
1

You can use string() or even more compact, . instead of text() to correctly consider element that doesn't contain any text node :

let $r_nodes := count(doc('test.xml')//r[. ne 'unspecified'])
return
  $r_nodes
har07
  • 88,338
  • 12
  • 84
  • 137