Skip to content

XQuery Samples (Advanced)

Daniel Aitken edited this page Jul 21, 2016 · 4 revisions

FOR/AT

(: Add a 'position' attribute to MODS 'topic' nodes denoting their position :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  for $text at $count in $input/mods:mods/mods:subject/mods:topic
  return insert node attribute position {$count} into $text
)
return $input

LET

(: Concatenate MODS 'typeOfResource' with MODS 'topic' using a variable :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  let $type := $input/mods:mods/mods:typeOfResource
  for $text in $input/mods:mods/mods:subject/mods:topic
  return replace value of node $text with concat($type, ' ', $text)
)
return $input

REORDER NODES

(: Reorder MODS 'topic' nodes alphabetically, ascending :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  for $text in $input/mods:mods/mods:subject/mods:topic
  order by $text ascending
  return (
    delete node $text,
    insert node $text as first into $text/..
  )
)
return $input

COUNT NODES

(: Add a 'topics' attribute to MODS 'subject' containing the MODS 
   'topic' node count :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  let $nodes := $input/mods:mods/mods:subject/mods:topic
  let $numNodes := count($nodes)
  return insert node attribute topics {$numNodes} into $input/mods:mods/mods:subject
)
return $input

TESTING CONTENT

(: Replace the content of MODS 'hierarchicalGeographic' if it isn't empty :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  for $node in $input/mods:mods/mods:subject/mods:hierarchicalGeographic
  where not(empty($node))
  return replace value of node $node with "Replaced Content"
)
return $input

TESTING NODES USING IS

(: Replace the content of the first MODS 'topic' node :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  let $nodes := $input/mods:mods/mods:subject/mods:topic
  for $node in $nodes
  where $node is $nodes[1]
  return replace value of node $node with "This was the first one"
)
return $input

IF/THEN/ELSE (STRICT COMPARISON VIA COUNT)

(: Add a comma to the end of every MODS 'topic' element except the last,
   to which, add a period :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  let $nodes := $input/mods:mods/mods:subject/mods:topic
  let $numNodes := count($nodes)
  for $node at $count in $nodes
  (: Proceed if the 'at' position is equal to the total number of nodes :)
  return if ($count = $numNodes)
    then replace value of node $node with concat($node, '.')
    else replace value of node $node with concat($node, ',')
)
return $input

IF/THEN/ELSE (SIMPLE COMPARISON VIA IS)

(: Add a comma to the end of every MODS 'topic' element except the last,
   to which, add a period :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  let $nodes := $input/mods:mods/mods:subject/mods:topic
  for $node in $nodes
  (: Proceed if $node is the last 'topic' element :)
  return if ($node is $nodes[last()])
    then replace value of node $node with concat($node, '.')
    else replace value of node $node with concat($node, ',')
)
return $input

COMPLETE FLWOR SAMPLE

(: Reorder MODS 'topic' nodes alphabetically, descending, if there is more than
   one of them :)
xquery version "3.0" encoding "UTF-8";
declare namespace mods="http://www.loc.gov/mods/v3";
copy $input := .
modify (
  let $nodes := $input/mods:mods/mods:subject/mods:topic
  let $count := count($nodes)
  for $node in $nodes
  where $count > 1
  order by $text descending
  return (
    delete node $node,
    insert node $node as first into $nodes
  )
)
return $input