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

Reply via email to