Skip to content

Instantly share code, notes, and snippets.

@caschwartz
Last active October 13, 2015 02:47
Show Gist options
  • Select an option

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

Select an option

Save caschwartz/4126944 to your computer and use it in GitHub Desktop.
XQuery - Query retrieves and replaces sort titles with punctuation at the beginning of the title
xquery version "1.0-ml";
(: 11/7/12 This query retrieves and replaces sort titles with punctuation at the beginning of the title :)
declare namespace ia = "http://my.namespace.uri";
let $docs := xdmp:directory("/ia-xml/o/", "infinity")
let $sortTitles := $docs/ia:doc/ia:metadata/ia:sortTitle
for $sortTitle in $sortTitles
let $newSortTitle := fn:replace($sortTitle, "^\p{P}+\s*", "")
let $normTitle := fn:normalize-unicode($newSortTitle, "NFKD")
let $letter := fn:upper-case(fn:substring($normTitle, 1, 1))
return if (fn:matches($sortTitle, "^\p{P}+\s*")) then
xdmp:node-replace($sortTitle,
<sortTitle first-letter="{ $letter }" xmlns="http://digital.library.ptsem.edu/ia">{ $newSortTitle }</sortTitle>)
else ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment