Subject: Re: [MarkLogic Dev General] How to add attributes to
existingnodesor replace only the attribute of an element - reg.,
Kelly Stirman
Kelly.Stirman at marklogic.com
Fri May 15 15:12:41 PDT 2009
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 <marklogic at wylovan.com>
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
<general at developer.marklogic.com>
Message-ID: <4A0D659F.6080200 at wylovan.com>
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
More information about the General
mailing list