Skip to content

Instantly share code, notes, and snippets.

@caschwartz
Last active October 5, 2015 17:38
Show Gist options
  • Select an option

  • Save caschwartz/2845542 to your computer and use it in GitHub Desktop.

Select an option

Save caschwartz/2845542 to your computer and use it in GitHub Desktop.
XQuery - Create controlaccess elements for EAD documents
xquery version "1.0-ml";
(: 12/11/10 Query to create controlaccess elements for EAD. Take the value of persname element and create a new node, the controlaccess element for each of the 227 EAD documents. Insert the new controlacess nodes after the first did element using the xdmp: node-insert-after function :)
(: NOTE: For unprefixed names (our EAD documents have unprefixed names) I needed to declare default element namespace, otherwise new elements will go into no namespace. This is what happpened when I first tested the controlaccess elements. :)
declare default element namespace "urn:isbn:1-931666-22-9";
let $doc := xdmp:directory("/EAD/")
let $newNodes := for $doc in xdmp:directory("/EAD/")
let $name := fn:string($doc/ead/archdesc/did/origination/persname)
order by $name ascending
return <controlaccess>
<persname encodinganalog="100" role="creator" source="lcnaf" altrender="{$name}">{$name}</persname>
</controlaccess>
let $topNodes := for $doc in xdmp:directory("/EAD/")
let $sibling := ($doc/ead/archdesc/did)[1]
order by fn:string($doc/ead/archdesc/did/origination/persname) ascending
return $sibling
return for $topNode at $x in $topNodes
return xdmp:node-insert-after($topNode, $newNodes[$x])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment