joewiz
9/11/2013 - 3:30 PM

Chiefs of Mission appointed during the presidency of Richard Nixon, using XQuery

Chiefs of Mission appointed during the presidency of Richard Nixon, using XQuery

<div>
    <p>268 Chiefs of Mission who were appointed between January 20, 1969 and August 9, 1974.</p>
    <ol>
        <li>
            <a href="http://history.state.gov/departmenthistory/people/adair-charles-wallace">Adair, Charles Wallace, Jr.</a>
            (Ambassador Extraordinary and Plenipotentiary  to Uruguay, September 15, 1969–September 28, 1972)</li>
        <li>
            <a href="http://history.state.gov/departmenthistory/people/adair-edwin-ross">Adair, Edwin Ross</a>
            (Ambassador Extraordinary and Plenipotentiary  to Ethiopia, May 11, 1971–February 12, 1974)</li>
        <li>
            <a href="http://history.state.gov/departmenthistory/people/aggrey-orison-rudolph">Aggrey, Orison Rudolph</a>
            (Ambassador Extraordinary and Plenipotentiary  to Gambia, The, November 23, 1973–July 10, 1977)</li>
        <li>
            <a href="http://history.state.gov/departmenthistory/people/aggrey-orison-rudolph">Aggrey, Orison Rudolph</a>
            (Ambassador Extraordinary and Plenipotentiary  to Gambia, The, November 23, 1973–July 10, 1977)</li>
        <!-- the rest -->
    </ol>
</div>
xquery version "3.0";

let $roles := collection('/db/cms/apps/principals-chiefs/code-tables/roles/data')/role
let $countries := collection('/db/cms/apps/countries/data')/country
let $start-date := '1969-01-20'
let $end-date := '1974-08-09'
let $appointments := collection('/db/cms/apps/principals-chiefs/data')//event[@type='appointed' and @when gt $start-date and @when lt $end-date]/parent::role[@class='chief' and @type ne 'charge-daffaires-ad-interim']
return
    <div>
        <p>{count($appointments)} Chiefs of Mission who were appointed between {format-date(xs:date($start-date), "[MNn] [D], [Y]")} and {format-date(xs:date($end-date), "[MNn] [D], [Y]")}.</p>
        <ol>{
            for $appointment in $appointments
            let $date := $appointment/event[@type='appointed']/@when
            let $person := $appointment/parent::person
            let $name := string-join(($person/persName/surname, $person/persName/forename, $person/persName/genName), ', ')
            let $rolename := $roles[id = $appointment/@type]/names/singular/string()
            let $dates := 
                string-join(
                    for $date in $appointment/event[@type=('appointed', 'appointmentterminated', 'missionterminated')]/@when
                    return
                        if ($date castable as xs:date) then format-date(xs:date($date), "[MNn] [D], [Y]") else concat($date, ' (?)')
                    , 
                    '–'
                )
            let $location := if ($appointment/@location) then concat(' to ', $countries[iso2 = $appointment/@location]/label) else ()
            let $id := $person/@id
            (:where contains($rolename, 'Ambassador'):)
            order by lower-case($name)
            return <li><a href="http://history.state.gov/departmenthistory/people/{$id}">{$name}</a> ({$rolename, $location}, {$dates})</li>
        }</ol>
</div>