Hi Tim,
The "best" way is clearly subjective, but I've included some examples of how I
would handle your use-cases. None of these solutions should vary in any way
between ML 6 and ML 7, as they are all vanilla, standard XQuery.
let $list :=
<list>
<item/>
</list>
(: check if the list has any element values :)
return fn:exists($list/*)
(: count the number of items with values :)
You have only one <item/>, and it has no text nodes or attributes. I'm not sure
what you mean by "values" … See below.
let $list :=
<list>
<item/>
<item i="2"/>
<item>
<key/>
<value/>
</item>
<item>
<key>autumn</key>
<value>Autumn</value>
</item>
</list>
(: count the number of items with any attributes :)
return fn:count($list/item[@*])
(: count the number of items with any child elements :)
return fn:count($list/item[*])
(: count the number of items with non-empty child elements :)
return fn:count($list/item[*[. ne ""]])
(: iterate through the list of items with element values in any sub-node :)
Not sure exactly what this means, here's some variations:
(: iterate through the list of items with any child elements :)
for $item in $list/item
where fn:exists($item[*])
return $item
(: iterate through the list of items with any non-empty child elements :)
for $item in $list/item
where fn:exists($item[*[. ne ""]])
return $item
(: iterate through the list of items with only non-empty child elements :)
for $item in $list/item
where fn:exists($item[*]) and
(every $x in $item/* satisfies fn:exists($x[. ne ""]))
return $item
Thanks.
-jb
From: Tim <[email protected]<mailto:[email protected]>>
Reply-To: MarkLogic Developer Discussion
<[email protected]<mailto:[email protected]>>
Date: Monday, December 1, 2014 at 10:51 AM
To: 'MarkLogic Developer Discussion'
<[email protected]<mailto:[email protected]>>
Subject: [MarkLogic Dev General] Evaluation of nodes with element values
Hi Folks,
Given the following XML, what is the “best” way to test for the existence of
values in a node?
let $list :=
<list>
<item/>
</list>
How would I best check to see if the list has any element values?
If ($list/string()) then … ?
How would I count the number of items with values?
count($list/item)
What about for the following list?
let $list :=
<list>
<item/>
<item i=”2”/>
<item>
<key/>
<value/>
</item>
<item>
<key>autumn</key>
<value>Autumn</value>
</item>
</list>
How would I iterate through the list of items with element values in any
sub-node?
for $item in $list/item
where $item
return …
Were there changes between ML6 * ML7 that affected any of these evaluations?
What are some good resources for explaining this? Thanks for any help!
Tim M.
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general