Skip to content

Instantly share code, notes, and snippets.

@tuurma
Created September 3, 2018 12:07
Show Gist options
  • Save tuurma/1c57890db2d5634edcf695be257db28d to your computer and use it in GitHub Desktop.
Save tuurma/1c57890db2d5634edcf695be257db28d to your computer and use it in GitHub Desktop.
basic xquery typeswitch example, assumes play is encoded in TEI like http://firstfolio.bodleian.ox.ac.uk/download/xml/F-tem.xml
xquery version "3.1";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "html5";
declare option output:media-type "text/html";
declare function local:transform($item) {
for $node in $item
return
typeswitch ( $node )
case element(tei:head) return
let $level := count($node/ancestor::tei:div)
return element {"h" || $level} {
for $child in $node/node()
return local:transform($child)
}
case element(tei:sp) return
<div>
{
for $child in $node/node()
return local:transform($child)
}
</div>
case element(tei:speaker) return
<h4>
{
for $child in $node/node()
return local:transform($child)
}
</h4>
case element(tei:stage) return
<h5>
{
for $child in $node/node()
return local:transform($child)
}
</h5>
case text()
return $node
default return
for $child in $node/node()
return local:transform($child)
};
let $doc := doc("/db/F-tem.xml")/tei:TEI
return local:transform($doc//tei:text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment