Wyatt is right - attributes are nodes too. :-)
So, his second example can be simplified to use xdmp:node-insert-child instead
of xdmp:node-replace():
let $book := doc("book.xml")/bib/book[1]
return
xdmp:node-insert-child($book,
attribute year {2009}
)
I would combine both operations in an if/then statement:
let $t := doc("book.xml")/bib/book
return
if($t/@year)
then xdmp:node-replace($t/@year, attribute year {"1999"})
else xdmp:node-insert-child($t, attribute year {"1999"})
Wyatt managed to mention all the other node update built ins, so I'm not sure
how he overlooked this one. :-)
Perhaps it is because it isn't obvious that attributes are children of
elements, at least not to me.
(Forgive typos - currently away from my computer)
Kelly
Message: 2
Date: Fri, 15 May 2009 08:52:47 -0400
From: Wyatt VanderStucken <[email protected]>
Subject: Re: [MarkLogic Dev General] How to add attributes to existing
nodesor replace only the attribute of an element - reg.,
To: General Mark Logic Developer Discussion
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
An attribute() is also a node(), so xdmp:node-replace() is the right
function...
Update is really easy...
let $oldYear as attribute() := doc("book.xml")/bib/book[1]/@year,
$newYear as attribute() := attribute year {2005}
return xdmp:node-replace($oldYear, $newYear)
Adding it initially (since the book element has no other attributes) is
slightly less straight forward...
let $book := doc("book.xml")/bib/book[1]
return
xdmp:node-replace($book,
element book {
attribute year {2009},
$book/*
}
)
If the book element had other attributes you could use
xdmp:node-insert-after() or xdmp:node-insert-before().
Regards,
Wyatt
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general