<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: feeds.xslt 205 2009-04-28 02:54:12Z dret $ -->
<!-- This work is licensed under a Creative Commons Attribution 3.0 United States License. -->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" xmlns:feeds="http://isd.ischool.berkeley.edu/stimulus/2009-029/feeds.xsd" xmlns:weekly="http://isd.ischool.berkeley.edu/stimulus/2009-029/weekly.xsd" xmlns:comm="http://isd.ischool.berkeley.edu/stimulus/2009-029/comm.xsd" xmlns:fbga="http://isd.ischool.berkeley.edu/stimulus/2009-029/fbga.xsd" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:saxon="http://saxon.sf.net/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="feeds weekly comm fbga atom georss kml saxon xsd">
	<xsl:output name="xhtml" method="xhtml" indent="yes" omit-xml-declaration="yes"/>
	<xsl:output name="atom" method="xml" indent="yes" omit-xml-declaration="yes"/>
	<xsl:output name="xml" method="xml" indent="yes" omit-xml-declaration="no"/>
	<xsl:output name="kml" method="xml" indent="yes" omit-xml-declaration="no" cdata-section-elements="kml:description"/>
	<xsl:output name="kml-escaped" method="xml" indent="yes" omit-xml-declaration="yes"/>
	<xsl:variable name="base" select="'http://isd.ischool.berkeley.edu/stimulus/2009-029/'"/>
	<xsl:template match="/">
		<xsl:result-document href="feeds.html" format="xhtml">
			<html>
				<head>
					<title>List of Fake Agencies</title>
				</head>
				<body>
					<h1>List of Fake Agencies</h1>
					<h3>Machine-readable list available at <code><a href="recovery.xml">recovery.xml</a></code></h3>
					<hr/>
					<ul>
						<xsl:for-each select="//feeds:agency">
							<xsl:sort select="@name"/>
							<li>
								<a href="{@id}/">
									<xsl:value-of select="@name"/>
								</a>
							</li>
						</xsl:for-each>
					</ul>
					<hr/>
					<p>Generated by <code><a href="feeds.xslt">feeds.xslt</a></code> from <code><a href="feeds.xml">feeds.xml</a></code></p>
				</body>
			</html>
		</xsl:result-document>
		<xsl:result-document href="recovery.xml" format="xml" xmlns="">
			<agencies>
				<xsl:for-each select="//feeds:agency">
					<agency name="{@name}" href="{$base}{@id}">
						<xsl:if test="exists(feeds:communications)">
							<feed type="comm" href="{$base}{@id}/comm.xml"/>
						</xsl:if>
						<xsl:if test="exists(feeds:allocations)">
							<feed type="fbga" href="{$base}{@id}/fbga.xml"/>
						</xsl:if>
						<xsl:if test="exists(feeds:weeklies)">
							<feed type="weekly" href="{$base}{@id}/weekly.xml"/>
						</xsl:if>
					</agency>
				</xsl:for-each>
			</agencies>
		</xsl:result-document>
		<xsl:for-each select="//feeds:agency">
			<xsl:result-document href="{@id}/index.html" format="xhtml">
				<xsl:comment> This work is licensed under a Creative Commons Attribution 3.0 United States License. </xsl:comment>
				<xsl:text>&#xa;</xsl:text>
				<html>
					<head>
						<title>
							<xsl:value-of select="@name"/>
							<xsl:text> Recovery Site</xsl:text>
						</title>
						<xsl:if test="exists(feeds:communications)">
							<link href="comm.xml" type="application/atom+xml" rel="alternate" title="Major Communications Feed"/>
						</xsl:if>
						<xsl:if test="exists(feeds:allocations)">
							<link href="fbga.xml" type="application/atom+xml" rel="alternate" title="Formula Block Grant Allocation Feed"/>
						</xsl:if>
						<xsl:if test="exists(feeds:weeklies)">
							<link href="weekly.xml" type="application/atom+xml" rel="alternate" title="Weekly Update Report Feed"/>
						</xsl:if>
					</head>
					<body>
						<h1>
							<xsl:value-of select="@name"/>
							<xsl:text> Recovery Site</xsl:text>
						</h1>
						<hr/>
						<xsl:if test="exists(child::*)">
							<p>Reports Published by this Agency:</p>
							<ul>
								<xsl:if test="exists(feeds:communications)">
									<li>
										<xsl:text>Major Communications: </xsl:text>
										<a href="comm.xml" title="Major Communications Feed">
											<img alt="Feed" src="../feed.png" style=" border : none "/>
										</a>
										<xsl:for-each select="feeds:communications/comm:major-comm">
											<xsl:sort select="comm:date"/>
											<xsl:variable name="date" select="xsd:date(substring(comm:date, 1, 10))"/>
											<xsl:variable name="path" select="concat(year-from-date($date), '/', month-from-date($date), '/', day-from-date($date))"/>
											<xsl:text>, </xsl:text>
											<a href="{$path}/comm.xml" title="Major Communications published {format-date($date, '[MNn] [D], [Y]', 'en', (), ())}">
												<xsl:value-of select="$date"/>
											</a>
										</xsl:for-each>
									</li>
								</xsl:if>
								<xsl:if test="exists(feeds:allocations)">
									<li>
										<xsl:text>Formula Block Grant Allocations: </xsl:text>
										<a href="fbga.xml" title="Formula Block Grant Allocations Feed">
											<img alt="Feed" src="../feed.png" style=" border : none "/>
										</a>
										<xsl:for-each select="feeds:allocations/fbga:allocation">
											<xsl:sort select="@date"/>
											<xsl:variable name="date" select="xsd:date(@date)"/>
											<xsl:variable name="path" select="concat(year-from-date($date), '/', month-from-date($date), '/', day-from-date($date))"/>
											<xsl:text>, </xsl:text>
											<a href="{$path}/fbga.xml" title="Formula Block Grant Allocation published {format-date($date, '[MNn] [D], [Y]', 'en', (), ())}">
												<xsl:value-of select="$date"/>
											</a>
										</xsl:for-each>
									</li>
								</xsl:if>
								<xsl:if test="exists(feeds:weeklies)">
									<li>
										<xsl:text>Weekly Update Reports: </xsl:text>
										<a href="weekly.xml" title="Weekly Update Report Feed">
											<img alt="Feed" src="../feed.png" style=" border : none "/>
										</a>
										<xsl:for-each select="feeds:weeklies/weekly:weekly">
											<xsl:sort select="weekly:weekstart"/>
											<xsl:variable name="date" select="xsd:date(weekly:weekstart)"/>
											<xsl:variable name="path" select="concat(year-from-date($date), '/', month-from-date($date), '/', day-from-date($date))"/>
											<xsl:text>, </xsl:text>
											<a href="{$path}/weekly.xml" title="Weekly Update Report published {format-date($date, '[MNn] [D], [Y]', 'en', (), ())}">
												<xsl:value-of select="$date"/>
											</a>
										</xsl:for-each>
									</li>
								</xsl:if>
							</ul>
						</xsl:if>
						<hr/>
						<p>Generated by <code><a href="../feeds.xslt">feeds.xslt</a></code> from <code><a href="../feeds.xml">feeds.xml</a></code></p>
					</body>
				</html>
			</xsl:result-document>
		</xsl:for-each>
		<xsl:for-each select="/feeds:feeds/feeds:agency/feeds:*" xmlns="http://www.w3.org/2005/Atom">
			<xsl:variable name="type" select=" if ( local-name() eq 'communications' ) then 'comm' else if ( local-name() eq 'allocations' ) then 'fbga' else 'weekly' "/>
			<xsl:result-document href="{../@id}/{$type}.xml" format="atom">
				<feed>
					<xsl:namespace name="georss" select="'http://www.georss.org/georss'"/>
					<xsl:text>&#xa;</xsl:text>
					<xsl:comment> This is fake data. Source: http://isd.ischool.berkeley.edu/stimulus/2009-029/ </xsl:comment>
					<xsl:text>&#xa;</xsl:text>
					<xsl:comment> This work is licensed under a Creative Commons Attribution 3.0 United States License. </xsl:comment>
					<xsl:text>&#xa;</xsl:text>
					<title>
						<xsl:choose>
							<xsl:when test="$type eq 'comm'">
								<xsl:text>Major Communications Feed</xsl:text>
							</xsl:when>
							<xsl:when test="$type eq 'fbga'">
								<xsl:text>Formula Block Grant Allocation Feed</xsl:text>
							</xsl:when>
							<xsl:when test="$type eq 'weekly'">
								<xsl:text>Weekly Update Report Feed</xsl:text>
							</xsl:when>
						</xsl:choose>
					</title>
					<subtitle>
						<xsl:text>Recovery Act information provided by </xsl:text>
						<xsl:value-of select="../@name"/>
					</subtitle>
					<id>
						<xsl:value-of select="concat($base, ../@id, '/', $type, '.xml')"/>
					</id>
					<link rel="self" href="{$base}{../@id}/{$type}.xml"/>
					<link rel="via" href="{$base}{feeds.xml}"/>
					<author>
						<name>
							<xsl:value-of select="../@name"/>
						</name>
						<uri>
							<xsl:value-of select="concat($base, ../@id, '/')"/>
						</uri>
					</author>
					<updated>
						<xsl:value-of select="current-dateTime()"/>
					</updated>
					<generator uri="{$base}{feeds.xslt}" version="$Id: feeds.xslt 205 2009-04-28 02:54:12Z dret $">Experimental Stimulus Feed Code</generator>
					<xsl:for-each select="*">
						<xsl:variable name="file" select=" if ( $type eq 'comm' ) then concat(../../@id, '/', year-from-dateTime(comm:date), '/', month-from-dateTime(comm:date), '/', day-from-dateTime(comm:date), '/', $type, '.xml') else if ( $type eq 'weekly' ) then concat(../../@id, '/', year-from-date(weekly:weekstart), '/', month-from-date(weekly:weekstart), '/', day-from-date(weekly:weekstart), '/', $type, '.xml') else concat(../../@id, '/', year-from-date(@date), '/', month-from-date(@date), '/', day-from-date(@date), '/', $type, '.xml') "/>
						<xsl:result-document href="{$file}" format="xml">
							<xsl:comment> This is fake data. Source: http://isd.ischool.berkeley.edu/stimulus/2009-029/ </xsl:comment>
							<xsl:text>&#xa;</xsl:text>
							<xsl:comment> This work is licensed under a Creative Commons Attribution 3.0 United States License. </xsl:comment>
							<xsl:text>&#xa;</xsl:text>
							<xsl:apply-templates select="." mode="copy"/>
						</xsl:result-document>
						<entry>
							<xsl:text>&#xa;</xsl:text>
							<xsl:comment> This is fake data. Source: http://isd.ischool.berkeley.edu/stimulus/2009-029/ </xsl:comment>
							<xsl:text>&#xa;</xsl:text>
							<xsl:comment> This work is licensed under a Creative Commons Attribution 3.0 United States License. </xsl:comment>
							<xsl:text>&#xa;</xsl:text>
							<!-- this is the only info identical across all entry types: the <link> to the XML, used as <id>. -->
							<link rel="alternate" type="application/xml+stimulus" href="{$base}{$file}"/>
							<id>
								<xsl:value-of select="concat($base, $file)"/>
							</id>
							<updated>
								<xsl:value-of select="current-dateTime()"/>
							</updated>
							<xsl:choose>
								<xsl:when test="$type eq 'comm'">
									<title>
										<xsl:value-of select="comm:title/text()"/>
									</title>
									<summary>
										<xsl:value-of select="comm:short/text()"/>
									</summary>
									<published>
										<xsl:value-of select="comm:date/text()"/>
									</published>
									<link rel="alternate" type="text/html" href="{comm:link/text()}"/>
									<category term="{comm:type/text()}" scheme="http://localhost/isd/stimulus/2009-029/comm-types"/>
									<content type="xhtml">
										<div xmlns="http://www.w3.org/1999/xhtml">
											<p>
												<xsl:value-of select="comm:short/text()"/>
											</p>
											<p>Link to <a href="{comm:link/text()}">published item</a> and its <a href="{$base}{$file}">XML version</a>.</p>
										</div>
									</content>
									<georss:point>
										<xsl:value-of select="@lat"/>
										<xsl:text> </xsl:text>
										<xsl:value-of select="@lon"/>
									</georss:point>
								</xsl:when>
								<xsl:when test="$type eq 'weekly'">
									<title>
										<xsl:text>Weekly report for </xsl:text>
										<xsl:value-of select="format-date(weekly:weekstart, '[MNn] [D], [Y]', 'en', (), ())"/>
									</title>
									<published>
										<xsl:value-of select="concat(weekly:weekstart, 'T00:00:00Z')"/>
									</published>
									<content type="xhtml">
										<xsl:call-template name="weekly2xhtml">
											<xsl:with-param name="file" select="$file"/>
										</xsl:call-template>
									</content>
									<georss:point>
										<xsl:value-of select="../../@lat"/>
										<xsl:text> </xsl:text>
										<xsl:value-of select="../../@lon"/>
									</georss:point>
								</xsl:when>
								<xsl:when test="$type eq 'fbga'">
									<title>
										<xsl:value-of select="fbga:recipient"/>
									</title>
									<published>
										<xsl:value-of select="concat(@date, 'T00:00:00Z')"/>
									</published>
									<georss:point>
										<xsl:value-of select="@lat"/>
										<xsl:text> </xsl:text>
										<xsl:value-of select="@lon"/>
									</georss:point>
									<content type="xhtml">
										<xsl:call-template name="fbga2xhtml">
											<xsl:with-param name="file" select="$file"/>
										</xsl:call-template>
									</content>
								</xsl:when>
							</xsl:choose>
						</entry>
					</xsl:for-each>
				</feed>
			</xsl:result-document>
		</xsl:for-each>
		<xsl:result-document href="feeds.kml" format="kml">
			<kml xmlns="http://www.opengis.net/kml/2.2">
				<Folder>
					<name>Fake Stimulus Feed Data</name>
					<xsl:for-each select="/feeds:feeds/feeds:agency">
						<Folder>
							<name>
								<xsl:value-of select="@name"/>
							</name>
							<Folder>
								<name>Formula Block Grant Allocation</name>
								<xsl:for-each select="feeds:allocations/fbga:allocation">
									<Placemark id="{$base}{../../@id}/{year-from-date(@date)}/{month-from-date(@date)}/{day-from-date(@date)}/fbga.xml">
										<xsl:variable name="xhtml">
											<xsl:call-template name="fbga2xhtml">
												<xsl:with-param name="file" select="concat(../../@id, '/', year-from-date(@date), '/', month-from-date(@date), '/', day-from-date(@date), '/fbga.xml')"/>
											</xsl:call-template>
										</xsl:variable>
										<xsl:variable name="description" select="saxon:serialize($xhtml, 'kml-escaped')"/>
										<description>
											<xsl:value-of select="$description"/>
										</description>
										<name>
											<xsl:value-of select="fbga:recipient"/>
										</name>
										<Point>
											<coordinates>
												<xsl:value-of select="@lon"/>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="@lat"/>
											</coordinates>
										</Point>
										<TimeStamp>
											<when>
												<xsl:value-of select="concat(@date, 'T00:00:00Z')"/>
											</when>
										</TimeStamp>
										<ExtendedData>
											<xsl:for-each select="fbga:*">
												<Data name="{name()}">
													<value>
														<xsl:value-of select="node()"/>
													</value>
												</Data>
											</xsl:for-each>
										</ExtendedData>
									</Placemark>
								</xsl:for-each>
							</Folder>
							<Folder>
								<name>Weekly Reports</name>
								<xsl:for-each select="feeds:weeklies/weekly:weekly">
									<Placemark id="{$base}{../../@id}/{year-from-date(weekly:weekstart)}/{month-from-date(weekly:weekstart)}/{day-from-date(weekly:weekstart)}/weekly.xml">
										<xsl:variable name="xhtml">
											<xsl:call-template name="weekly2xhtml">
												<xsl:with-param name="file" select="concat(../../@id, '/', year-from-date(weekly:weekstart), '/', month-from-date(weekly:weekstart), '/', day-from-date(weekly:weekstart), '/weekly.xml')"/>
											</xsl:call-template>
										</xsl:variable>
										<xsl:variable name="description" select="saxon:serialize($xhtml, 'kml-escaped')"/>
										<description>
											<xsl:value-of select="$description"/>
										</description>
										<name>
											<xsl:text>Weekly report for </xsl:text>
											<xsl:value-of select="format-date(weekly:weekstart, '[MNn] [D], [Y]', 'en', (), ())"/>
										</name>
										<Point>
											<coordinates>
												<xsl:value-of select="../../@lon"/>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="../../@lat"/>
											</coordinates>
										</Point>
										<TimeStamp>
											<when>
												<xsl:value-of select="concat(weekly:weekstart, 'T00:00:00Z')"/>
											</when>
										</TimeStamp>
										<ExtendedData>
											<Data name="weekly:weekstart">
												<displayName>Week Start</displayName>
												<value>
													<xsl:value-of select="weekly:weekstart/node()"/>
												</value>
											</Data>
										</ExtendedData>
									</Placemark>
								</xsl:for-each>
							</Folder>
							<Folder>
								<name>Major Communications</name>
								<xsl:for-each select="feeds:communications/comm:major-comm">
									<Placemark id="{$base}{ concat(../../@id, '/', year-from-dateTime(comm:date), '/', month-from-dateTime(comm:date), '/', day-from-dateTime(comm:date), '/comm.xml')}">
										<description>
											<xsl:value-of select="comm:short/text()"/>
										</description>
										<name>
											<xsl:value-of select="comm:title/text()"/>
										</name>
										<Point>
											<coordinates>
												<xsl:value-of select="@lon"/>
												<xsl:text>,</xsl:text>
												<xsl:value-of select="@lat"/>
											</coordinates>
										</Point>
										<TimeStamp>
											<when>
												<xsl:value-of select="comm:date/text()"/>
											</when>
										</TimeStamp>
										<ExtendedData>
											<xsl:for-each select="comm:*">
												<Data name="{name()}">
													<value>
														<xsl:value-of select="node()"/>
													</value>
												</Data>
											</xsl:for-each>
										</ExtendedData>
									</Placemark>
								</xsl:for-each>
							</Folder>
						</Folder>
					</xsl:for-each>
				</Folder>
			</kml>
		</xsl:result-document>
	</xsl:template>
	<xsl:template match=" @* | node() " mode="copy">
	   <xsl:copy copy-namespaces="no">
		  <xsl:apply-templates select=" @* | element() | text()[normalize-space() ne ''] " mode="copy"/>
	   </xsl:copy>
	</xsl:template>
	<xsl:template match=" @lat | @lon " mode="copy"/>
	<xsl:template match=" weekly:completed | weekly:planned ">
	   <ul xmlns="http://www.w3.org/1999/xhtml">
		  <xsl:for-each select="weekly:action">
			  <li>
				  <xsl:value-of select="text()"/>
			  </li>
		  </xsl:for-each>
	   </ul>
	</xsl:template>
	<xsl:template name="fbga2xhtml">
		<xsl:param name="file"/>
		<div xmlns="http://www.w3.org/1999/xhtml">
			<table>
				<tbody>
					<tr>
						<th align="right" valign="top">Recipient:</th>
						<td>
							<xsl:value-of select="fbga:recipient"/>
							<xsl:text> (DUNS </xsl:text>
							<xsl:value-of select="fbga:DUNS"/>
							<xsl:text>)</xsl:text>
						</td>
					</tr>
					<tr>
						<th align="right" valign="top">Funding:</th>
						<td>
							<xsl:value-of select="format-number(fbga:funding, '$#,##0.00')"/>
						</td>
					</tr>
					<tr>
						<th align="right" valign="top">CFDA:</th>
						<td>
							<xsl:value-of select="fbga:CFDA-title"/>
							<xsl:text> (</xsl:text>
							<xsl:value-of select="fbga:CFDA"/>
							<xsl:text>)</xsl:text>
						</td>
					</tr>
					<tr>
						<th align="right" valign="top">Address:</th>
						<td>
							<xsl:for-each select="fbga:address">
								<xsl:value-of select="."/>
								<xsl:if test="position() ne last()">
									<br/>
								</xsl:if>
							</xsl:for-each>
							<br/>
							<xsl:value-of select="fbga:city"/>
							<xsl:text>, </xsl:text>
							<xsl:value-of select="fbga:state/@code"/>
							<xsl:text> </xsl:text>
							<xsl:value-of select="fbga:zip"/>
						</td>
					</tr>
					<tr>
						<th align="right" valign="top">Program Source:</th>
						<td>
							<xsl:text>Agency </xsl:text>
							<xsl:value-of select="fbga:agency"/>
							<xsl:text>; Account </xsl:text>
							<xsl:value-of select="fbga:account"/>
							<xsl:text>; Sub-Account </xsl:text>
							<xsl:value-of select="fbga:sub-account"/>
						</td>
					</tr>
				</tbody>
			</table>
			<p>Link to <a href="{$base}{$file}">XML version</a>.</p>
		</div>
	</xsl:template>
	<xsl:template name="weekly2xhtml">
		<xsl:param name="file"/>
		<div xmlns="http://www.w3.org/1999/xhtml">
			<table>
				<thead>
					<tr>
						<th>Agency Code</th>
						<th>Account Code</th>
						<th>Sub-Account Code</th>
						<th>Appropriation</th>
						<th>Obligations</th>
						<th>Expenditures</th>
						<th>Completed Actions</th>
						<th>Planned Actions</th>
					</tr>
				</thead>
				<tbody>
					<xsl:for-each select="weekly:report">
						<tr>
							<td>
								<xsl:value-of select="weekly:agency"/>
							</td>
							<td>
								<xsl:value-of select="weekly:account"/>
							</td>
							<td>
								<xsl:value-of select="weekly:sub-account"/>
							</td>
							<td>
								<xsl:value-of select="format-number(weekly:appropriation, '$#,##0.00')"/>
							</td>
							<td>
								<xsl:value-of select="format-number(weekly:obligations, '$#,##0.00')"/>
							</td>
							<td>
								<xsl:value-of select="format-number(weekly:expenditures, '$#,##0.00')"/>
							</td>
							<td>
								<xsl:apply-templates select="weekly:completed"/>
							</td>
							<td>
								<xsl:apply-templates select="weekly:planned"/>
							</td>
						</tr>
					</xsl:for-each>
				</tbody>
			</table>
			<p>Link to <a href="{$base}{$file}">XML version</a>.</p>
		</div>
	</xsl:template>
</xsl:stylesheet>