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



Add the attribute

Santhosh Raj wrote:

Hi all,

1) If i have a document stored in marklogic server without attributes for its element, suppose now if i want to add any attributes to the element, then how do i do that without using xdmp:node-replace() function

eg: My file is book.xml
<bib>
    <book >
        <title>TCP/IP Illustrated</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
<book >
        <title>Advanced Programming in the Unix environment</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
</bib>

If i want to add an attribute "year" to book element . How to do it without replacing the whole node.

let $b := doc("book.xml")//book
return
xdmp:node-replace($b[1], <book year="2009" >
        <title>TCP/IP Illustrated</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>)

2) If i want to change the attribute value (form 2009 to 2005) then how to do without replacing the whole node, How do i change the attribute value only using xquery.

Thanks and Regards,
Santhosh Rajasekaran
Tata Consultancy Services
Mailto: [email protected]
Website: http://www.tcs.com <http://www.tcs.com/>
____________________________________________
Experience certainty.        IT Services
                       Business Solutions
                       Outsourcing
____________________________________________
=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain confidential or privileged information. If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the information contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachments. Thank you


------------------------------------------------------------------------

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to