Hi Eric,
The xdmp:node-* functions are intended to be used on nodes that are
stored in the database, that's why you're getting the error about
constructed nodes.
Your work around is fine but I've actually shared a general purpose
library for doing exactly what you're looking to do. You can find it
on the developer site in the commons area at:
http://xqzone.marklogic.com/svn/commons/trunk/memupdate/
The functions are almost identical to the xdmp:node-* functions except
that they return the modified xml back to you which seems like it's
exactly what you're looking for. They also allow you to pass in
multiple nodes to delete or insert instead of just one. I've included
some examples on how to use the library but if you have any questions
let me know.
Bonus answer: If the element isn't in a namespace name(<hi/>) will
work, otherwise you can use local-name(<x:hi/>) and it will return "hi".
--Ryan
On Sep 15, 2008, at 10:26 AM, Eric Palmitesta wrote:
Hi all,
I'd like a function to add an attribute to an input element and
return it. I realize that xquery is functional, and the returned
element will be a new element rather than a modified version of the
input element, which is fine.
Consider the following function:
---
define function f($x as element(test))
as element(test)
{
xdmp:node-insert-child($x, attribute new { "new" } )
}
let $x := <test blah="blah">some text</test>
return f($x)
=> Error: Cannot update constructed nodes
---
This errors out and tells me "Cannot update constructed nodes", as
indicated in the API docs ("On-the-fly constructed nodes cannot be
updated.", http://developer.marklogic.com/pubs/3.2/apidocs/UpdateBuiltins.html#node-insert-child)
Here's a workaround function:
---
define function g($x as element(test))
as element(test)
{
<test>{
$x/text(),
$x/child::*,
$x/attribute::*,
attribute new{ "new" }
}</test>
}
let $x := <test blah="blah">some text</test>
return g($x)
=> <test blah="blah" new="new">some text</test>
---
It works. Is there a better way though? Also, what's the point of
xdmp:node-insert-child if it can't operate on an argument to a
function? Why is the arg considered 'on-the-fly'? Shouldn't the
function not care where $x came from or how it was constructed?
Bonus question: If I have an $x as element(), how can I pull out the
element name? IE if $x := <hi />, I want "hi".
Cheers,
Eric
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general