joewiz
1/6/2017 - 9:08 PM

Principal Officers and Chiefs of Mission from New Jersey; using XQuery

Principal Officers and Chiefs of Mission from New Jersey; using XQuery

xquery version "3.0";
 
(: Displays a list of Principals & Chiefs hailing from New Jersey. 
 : Updated from https://gist.github.com/joewiz/11154995 to use new hsg3 site structure and modules
:)

declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method "html5";
declare option output:media-type "text/html";

import module namespace app = "http://history.state.gov/ns/site/hsg/templates" at "/db/apps/hsg-shell/modules/app.xqm";
import module namespace pocom = "http://history.state.gov/ns/site/hsg/pocom-html" at "/db/apps/hsg-shell/modules/pocom-html.xqm";

declare function local:fix-links($nodes) {
    for $node in $nodes
    return
        typeswitch ($node)
            case text() return 
                $node 
            case element(a) return 
                element a {
                    attribute href { replace($node/@href, '^\$app', 'https://history.state.gov') },
                    local:fix-links($node/node())
                }
            case element() return 
                element { $node/name() } { $node/@*, local:fix-links($node/node()) }
            default return 
                ()
};

let $all-people := collection('/db/apps/pocom/people')/person
let $state-id := 'nj'
let $state-label := doc('/db/apps/pocom/code-tables/us-state-codes.xml')//item[value = $state-id]/label/string()
let $from-state := $all-people[residence/state-id = $state-id]
let $index := pocom:format-index(<blah/>, map{}, $from-state, ())
let $fixed-links := local:fix-links($index/*)
return 
    <div>
        <h1>Principal Officers and Chiefs of Mission from {$state-label}</h1>
        <h2>{format-date(current-date(), '[MNn] [D], [Y]')}</h2>
        <p>Principal Officers and Chiefs of Mission from {$state-label}. There are {count($from-state)} entries. For a description of the categories and markings used here, please see the introduction to <a href="https://history.state.gov/departmenthistory/people/principals-chiefs">Principal Officers and Chiefs of Mission</a>.</p>
        <ol>{ $fixed-links }</ol>
    </div>