Hi Chris,

I'm working with a stack that always provides empty element nodes when the 
corresponding json does not return a value, so I don't have the luxury of 
omitting empty nodes.

Best!

Tim

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Christopher Hamlin
Sent: Monday, December 01, 2014 12:07 PM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] Evaluation of nodes with element values

Hi Tim,


>
> Given the following XML, what is the  best  way to test for the 
> existence of values in a node?
>
> let $list :=
>
> <list>
>
>                 <item/>
>
> </list>
>
>

I think usually the best is to not have the item if there is no value.
Then it is easy to find the items with values.

But, generally, if given the list, to find the list items with child elements:

$list/item[*]

To find the items with attributes:

$list/item[@*]

To find the items with immediate child text nodes:

$list/item[text()]

>
> How would I best check to see if the list has any element values?
>
> If ($list/string()) then   ?
>

string() will always return a string, so then you may have to check the length 
to see if it is empty.

text() will check for child text nodes, which can be especially useful if you 
know it will only ever contain text.  But it can also return more than one node 
if there is mixed content, and it only looks at immediate children so won't 
find text in descendants of the item.

>
> How would I count the number of items with values?
>
> count($list/item)

That will count the number of child item elements that the list has.
You need some predicate to weed out 'empty' items, as above.

>
> 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  
>
>

Yes.  Just change the where line to include your test.  So for items with a key 
child:

$item[key]

and for items that have a child key element or any attribute:

$list/item[key or @*]

and to find items with child key elements that have non-zero-length string 
values:

$list/item[string-length (string(key)) > 0]

But notice that once you start adding empty values, you could have a 
potentially large and completely 'empty' structure with no value anywhere.  Why 
not leave it out?

>
>
>
> Were there changes between ML6 * ML7 that affected any of these evaluations?

That seems unlikely to me.

>
> What are some good resources for explaining this?  Thanks for any help!
>

This sort of stuff, where you are operating on the structure of a particular 
document/node in memory (not MarkLogic search) is covered by xpath, so target 
that.  Michael Kay's book on xpath/xslt is great, though it can be tough to 
gain a foothold at first.

Hope it helps.


- Chris
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general


_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to