Something like this:

declare variable $URI as xs:string external;

let $book := fn:doc($URI)/book
return xdmp:node-insert-child($book, 
<total-chapters>{fn:count($book/chapter)}</total-chapters>)


________________________________________
From: [email protected] 
[[email protected]] on behalf of Mark Shanks 
[[email protected]]
Sent: 20 June 2015 19:31
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
_______________________________________________
General mailing list
[email protected]
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to