|
Hi Mark,
There are several ways to achieve this goal. Please find the below sample code :
In this code, I am assuming all initial books(without chapter_count element) are in "MyCollection" collection inside MarkLogic database.
for $eachDocUri in cts:uris((),(),cts:collection-query("MyCollection"))
let $doc := fn:doc($eachDocUri)/book
let $newBook := element {fn:local-name($doc)} {
$doc/*,
element total_chapters {fn:count($doc/chapter)}
}
return $newBook (: xdmp:document-insert(fn:concat("/newbooks",$eachDocUri),$newBook,(),"BookCollection") :)
At the last line of code => Here you can import $newBook directly into corb OR can insert the final books(with chapter_count element) into "BookCollection" collection into MarkLogic database and then from this "BookCollection" collection you
can import into corb.
I hope it will work for you.
Regards,
Asit Nautiyal
From: [email protected] [[email protected]] on behalf of Mark Shanks [[email protected]]
Sent: Sunday, June 21, 2015 12:01 AM To: [email protected] Subject: [MarkLogic Dev General] How to insert into a document the number of a particular element Hi,
So I’m looking for a xquery function that I would have thought would be quite common, but searching has turned up a blank (partly because the search terms I can think of are too generic). Say if you have the following documents:
<book>
<title>Moby Dick</title>
<chapter>Chapter 1 title</chapter>
<chapter>Chapter 2 title</chapter>
</book>
<book>
<title>War of the Worlds</title>
<chapter>Chapter 1 title</chapter>
<chapter>Chapter 2 title</chapter>
<chapter>Chapter 3 title</chapter>
</book>
You can import all of the documents into corb using the following get-uris.xqy:
xquery version "1.0-ml";
let $uris := cts:uris('', 'document')
return (count($uris), $uris)
I would then like to add another element to each book document that gives the number of chapters in each of the books, i.e., the resulting documents should be like this:
<book>
<title>Moby Dick</title>
<chapter>Chapter 1 title</chapter>
<chapter>Chapter 2 title</chapter>
<total chapters>2</total chapters>
</book>
<book>
<title>War of the Worlds</title>
<chapter>Chapter 1 title</chapter>
<chapter>Chapter 2 title</chapter>
<chapter>Chapter 3 title</chapter>
<total chapters>3</total chapters>
</book>
What xquery would one use in the transform-docs.xqy to do this? Inserting another element is easy (xdmp:node-insert-child) but actually counting up the number of a particular element for each document is what I’m not sure of. Any insight appreciated.
Thanks,
Mark
The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. ---------------------------------------------------------------------------------------------------------------------------------------------------- |
_______________________________________________ General mailing list [email protected] Manage your subscription at: http://developer.marklogic.com/mailman/listinfo/general
