Tim,

I'm pleased that you didn't simply drop the 'as node()' typing - many people do, but I think it's better to use strong typing wherever possible. Anyway, I think you want 'as node()?' instead of 'as node()', and you might consider 'as element(value)?' to use even stronger typing.

http://www.w3.org/TR/xquery/#FunctionDeclns

http://www.w3.org/TR/xquery/#prod-xquery-OccurrenceIndicator

-- Mike

On 2009-06-01 10:26, 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

Reply via email to