joewiz
1/12/2017 - 10:01 PM

Find travels of the Secretary of State that took place within 8 days of inauguration

Find travels of the Secretary of State that took place within 8 days of inauguration

<results>
    <result>
        <president n="41">
            <id>bush-george-h-w</id>
            <name>George H. W. Bush</name>
            <birth-year>1924</birth-year>
            <death-year still-alive="true"/>
            <took-office-date>1989-01-20</took-office-date>
            <left-office-date>1993-01-20</left-office-date>
            <created-by>admin</created-by>
            <created-date>2011-08-18</created-date>
            <last-modified-by>admin</last-modified-by>
            <last-modified-date>2011-08-18</last-modified-date>
        </president>
        <trip who="shultz-george-pratt" role="secretary">
            <id>2188</id>
            <role>secretary</role>
            <name>George Pratt Shultz</name>
            <start-date>1989-01-16</start-date>
            <end-date>1989-01-18</end-date>
            <country id="austria">Austria</country>
            <locale>Vienna</locale>
            <remarks>Attended the closing session of the CSCE Follow-up Meeting. </remarks>
            <created-by>joewiz</created-by>
            <created-datetime>2010-02-27T12:35:48.889-06:00</created-datetime>
            <last-modified-by>joewiz</last-modified-by>
            <last-modified-datetime>2010-02-27T12:35:48.889-06:00</last-modified-datetime>
        </trip>
    </result>
    <result>
        <president n="43">
            <id>bush-george-w</id>
            <name>George W. Bush</name>
            <birth-year>1946</birth-year>
            <death-year still-alive="true"/>
            <took-office-date>2001-01-20</took-office-date>
            <left-office-date>2009-01-20</left-office-date>
            <created-by>admin</created-by>
            <created-date>2011-08-18</created-date>
            <last-modified-by>admin</last-modified-by>
            <last-modified-date>2011-08-18</last-modified-date>
        </president>
        <trip who="albright-madeleine-korbel" role="secretary">
            <id>269</id>
            <role>secretary</role>
            <name>Madeleine Korbel Albright</name>
            <start-date>2001-01-11</start-date>
            <end-date>2001-01-13</end-date>
            <country id="france">France</country>
            <locale>Paris</locale>
            <remarks>Met with Foreign Minister Vedrine and other European Foreign Ministers. </remarks>
            <created-by>joewiz</created-by>
            <created-datetime>2010-02-27T12:35:48.889-06:00</created-datetime>
            <last-modified-by>joewiz</last-modified-by>
            <last-modified-datetime>2010-02-27T12:35:48.889-06:00</last-modified-datetime>
        </trip>
    </result>
</results>
xquery version "3.1";

let $all-secretary-travels := collection('/db/apps/travels/secretary-travels')//trip
let $days-before-inauguration := 8
let $duration := xs:dayTimeDuration("P" || $days-before-inauguration || "D")
let $presidents := doc("/db/apps/travels/presidents/presidents.xml")//president
for $president in $presidents
let $inauguration-date := xs:date($president/took-office-date)
let $travels-within-period-before-inauguration := 
    for $trip in $all-secretary-travels
    let $start-date := xs:date($trip/start-date)
    let $end-date := xs:date($trip/end-date)
    return
        if 
            (
                (
                    $end-date le $inauguration-date 
                    and
                    $inauguration-date - $end-date le $duration
                )
                or
                (
                    $start-date le $inauguration-date 
                    and
                    $inauguration-date - $start-date le $duration
                )
            )
        then
            $trip
        else
            ()
return
    if ($travels-within-period-before-inauguration) then
        element result { $president, $travels-within-period-before-inauguration }
    else 
        ()