<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	exclude-result-prefixes="xhtml">

<!--
	Scottish Parliament HTML -> Public Whip XML
	Copyright © 2007 Paul Mitchell. All rights reserved. ip@paul-mitchell.me.uk
-->

<xsl:output
	method="xml"
	encoding="iso-8859-1"
	doctype-system="publicwhip.dtd"
	indent="yes" />

<!-- disable automatic text output -->
<xsl:template match="@*|text()"/>

<!-- the main content is in a table, the immediate following sibling of
			anchor "mainContent" -->
<xsl:template
	match="/">
	<xsl:for-each
		select="//xhtml:table[preceding-sibling::*[1][self::xhtml:a[@id='mainContent']]]">
		<publicwhip>
			<xsl:apply-templates select="xhtml:tr/xhtml:td/*"/>
		</publicwhip>
	</xsl:for-each>
</xsl:template>

<!-- speeches are contained in classified divisions, immediately
			preceded by a paragraph containing the column number and anchor -->
<xsl:template
	match="xhtml:div[@class='orindent']">
	<xsl:apply-templates select="*">
		<xsl:with-param
			name="colnum"
			select="preceding-sibling::*[1][self::xhtml:p]/xhtml:a/text()"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template
	match="xhtml:div[@class='orindent']/xhtml:p[* or text()]">
	<xsl:param name="colnum"/>

	<p colnum="{$colnum}">

	<!-- identify and transform time, speech, divisions and continuation paragraphs -->
	<xsl:choose>
		<!-- the time -->
		<xsl:when test="count(text())=1 and string-length(text())=5 and substring(text(),3,1)=':'">
			<xsl:attribute name="class">time</xsl:attribute>
			<xsl:copy-of select="text()"/>
		</xsl:when>

		<!-- a note for the record, such as
						<strong>Member</strong><em>rose</em>
						<strong>Member</strong> <em>indicated disagreement</em>	 -->
		<xsl:when test="not(normalize-space(text())) and count(xhtml:em)=1">
			<xsl:attribute name="class">note</xsl:attribute>
			<xsl:if test="xhtml:strong">
				<!-- deal with  -->
				<xsl:attribute name="speaker">
					<xsl:copy-of select="normalize-space(xhtml:strong/text())"/>
				</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates select="xhtml:em/node()" mode="scrape"/>
		</xsl:when>

		<!-- a member or members begin speaking -->
		<xsl:when test="count(xhtml:strong)=1 and count(text())">
			<xsl:attribute name="speaker">
				<xsl:copy-of select="substring-before(xhtml:strong/text(),':')"/>
			</xsl:attribute>
			<xsl:apply-templates select="node()" mode="scrape"/>
		</xsl:when>

		<!-- a question is asked -->
		<!-- <p> <strong>3.</strong> <strong>Member: </strong> Question.... (question id)</p> -->
		<xsl:when test="count(xhtml:strong)=2 and count(text())">
			<xsl:attribute name="class">question</xsl:attribute>
			<xsl:attribute name="qnum">
				<xsl:copy-of select="number(xhtml:strong[1]/text())"/>
			</xsl:attribute>
			<xsl:attribute name="speaker">
				<xsl:copy-of select="substring-before(xhtml:strong[2]/text(),':')"/>
			</xsl:attribute>
			<xsl:apply-templates select="node()" mode="scrape"/>
		</xsl:when>

		<!-- fancy formatted FOR/AGAINST/ABSTENTIONS during divisions -->
		<xsl:when test="xhtml:strong and xhtml:font[xhtml:strong]">
			<xsl:attribute name="class">votes</xsl:attribute>
			<xsl:copy-of select=".//text()"/>
		</xsl:when>

		<!-- a debate heading -->
		<xsl:when test="xhtml:strong and not(text())">
			<xsl:attribute name="class">heading</xsl:attribute>
			<xsl:apply-templates select="xhtml:strong/node()" mode="scrape"/>
		</xsl:when>

		<!-- a quote -->
		<xsl:when test="starts-with(text(),'&quot;')">
			<xsl:attribute name="class">indent</xsl:attribute>
			<xsl:apply-templates mode="scrape"/>
		</xsl:when>

		<xsl:otherwise>
			<xsl:apply-templates mode="scrape"/>
		</xsl:otherwise>

	</xsl:choose>

	</p>
</xsl:template>

<!-- default copy everything in scrape mode -->
<xsl:template
	mode="scrape"
	match="@*|node()">
	<xsl:copy><xsl:apply-templates select="@*|node()" mode="scrape"/></xsl:copy>
</xsl:template>

<!-- convert em to i -->
<xsl:template
	mode="scrape"
	match="xhtml:em">
	<i><xsl:apply-templates mode="scrape"/></i>
</xsl:template>

<!-- eliminate anchors -->
<!-- eliminate strongs, because they have already been dealt with -->
<!-- eliminate breaks -->
<xsl:template
	mode="scrape"
	match="xhtml:a|xhtml:strong|xhtml:br"/>

<!-- pass-thru content of some tags -->
<xsl:template
	mode="scrape"
	match="xhtml:sup|xhtml:sub">
	<xsl:apply-templates mode="scrape"/>
</xsl:template>

</xsl:stylesheet>