Your function needs to return exactly one node, as specified by "as node()". If you specify "as node()?" you'll be allowed to return exactly one or zero nodes.

Cheers,

Eric

Tim Meagher wrote:
Hi folks,

This may be a simple xquery syntax issue, but I’ve cut my teeth with xquery in MarkLogic and thought I’d ask the question here.

There are times that I need to build a node in a function using conditional logic. For example, given the following function:

declare function aas:build-value-element($qname as xs:string,

  $value as xs:string) as node()

{

    if ($value ne "") then element value { attribute name {$qname}, $value}

    else ()

};

then if a blank value is passed in, e.g.

aas:build-value-element("surname ", "")

then an error is returned:

* *[1.0-ml] XDMP-AS: (err:XPTY0004) aas:build-value-element("author-surname", "") -- Invalid coercion: () as node()

So the question is, what is the best way to return an empty node? What I have been doing to get around this is to return a blank text node, e.g.:

declare function aas:build-value-element($qname as xs:string,

  $value as xs:string) as node()

{

    if ($value ne "") then element value { attribute name {$qname}, $value}

    else text {“”}

};

but the result is that the return node has the extra node in it, albeit a blank text node.

Are there any best practices for handling this? Is this really an issue – in all reality does the blank text node get stripped off when storing the results in an XML document?

Thanks for the help!

Tim Meagher – AAOM Consulting


------------------------------------------------------------------------

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

Reply via email to