teacup/transforms/newDocBook2BP.xsl
2021-12-15 11:29:23 +00:00

1139 lines
52 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns="http://docbook.org/ns/docbook" xmlns:db="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:ifp="http://www.ifactory.com/press" exclude-result-prefixes="#all">
<!--
Stylesheet to convert the new Bloomsbury DocBook format to the old Bloomsbury Professional DocBook
Brad Scott
20161121: Added support for variablelists and fixed remaining table issues
20160805: Added support for annotated legislation
20160711: Modifications for case reports
20160421: Revised and ready for commit to GitHub
20151201: Add PubFactory metadata, and improve support so it loads properly
20151124: Handle implied authors
20150701: First outline version of commentary XSLT
-->
<!-- PubFactory Metadata -->
<xsl:variable name="pfID"><xsl:text>add_required_book_title_here</xsl:text>
<!--<xsl:value-of
select="translate(db:book/db:info/db:title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ -/,.:;?', 'abcdefghijklmnopqrstuvwxyz___')"
/>-->
</xsl:variable>
<xsl:variable name="collection1">irish_criminal_law</xsl:variable>
<xsl:variable name="collection2">irish_law_tax</xsl:variable>
<xsl:variable name="jurisdiction">ie</xsl:variable>
<xsl:variable name="filename"><xsl:value-of select="$pfID"/>.xml</xsl:variable>
<!-- index terms -->
<xsl:variable name="indexterms" select="//db:indexterm[not(@class='endofrange')]"/>
<!-- Most things are fine as they are so we need a general copy -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Small differences in NS declarations and @role -->
<xsl:template match="db:book">
<book xmlns="http://docbook.org/ns/docbook" version="5.0-extension bp-0.1"
xmlns:xl="http://www.w3.org/1999/xlink"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:ifp="http://www.ifactory.com/press" xml:id="{@xml:id}">
<xsl:attribute name="role">
<xsl:value-of select="db:info/db:biblioid[@otherclass = 'legalContentType']"/>
</xsl:attribute>
<xsl:text>
</xsl:text>
<xsl:comment>
This data has been generated from the source DocBook file. It MUST NOT be edited; if corrections or changes are required, they MUST be made in the source file and then this regenerated from it.
</xsl:comment>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="node()"/>
</book>
</xsl:template>
<!--<xsl:template match="db:book/db:info">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<!-\- Create the space for the book metadata -\->
<biblioset role="productMetadata">
<biblioid class="pfID">
<xsl:value-of select="$pfID"/>
</biblioid>
<biblioid class="collection">
<xsl:value-of select="$collection1"/>
</biblioid>
<biblioid class="collection">
<xsl:value-of select="$collection2"/>
</biblioid>
<biblioid class="jurisdiction">
<xsl:value-of select="$jurisdiction"/>
</biblioid>
</biblioset>
</xsl:copy>
</xsl:template>-->
<xsl:template match="db:book/db:info/db:edition">
<xsl:copy>
<xsl:value-of select="substring-before(., ' edition')"/>
</xsl:copy>
</xsl:template>
<!-- Different @class required for isbn -->
<xsl:template match="db:biblioid[@class = 'isbn']">
<biblioid class="isbn13">
<xsl:if test="@role">
<xsl:attribute name="role">
<xsl:value-of select="@role"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node()"/>
</biblioid>
</xsl:template>
<xsl:template match="db:biblioset[@role = 'publisher']">
<xsl:choose>
<!-- Only the first publisher block will contain all the info we need, so we can throw the others away -->
<xsl:when test="preceding-sibling::db:biblioset[@role = 'publisher']"/>
<xsl:otherwise>
<biblioset role="publisher">
<xsl:if test="db:bibliomisc[@role = 'imprint']">
<bibliomisc role="imprint">
<xsl:value-of select="db:bibliomisc[@role = 'imprint'][1]"/>
</bibliomisc>
</xsl:if>
<publisher>
<publishername>
<xsl:value-of select="db:publisher[1]/db:publishername[1]"/>
</publishername>
</publisher>
</biblioset>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- We can ignore @outputformat -->
<xsl:template match="@outputformat"/>
<!-- Throw away the front and back part containers -->
<xsl:template match="db:part[@role = 'front'] | db:part[@role = 'back']">
<xsl:apply-templates select="node()" exclude-result-prefixes="#all"/>
</xsl:template>
<!-- BP DocBook partintro has a mandatory @role -->
<xsl:template match="db:partintro">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:if test="not(@role)">
<!-- not sure what "longTitle" does; "other" outnumbers it 91:4 -->
<xsl:attribute name="role" select="'other'"/>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- And throw away the prelims chunk -->
<xsl:template match="db:preface[@role = 'prelims']"> </xsl:template>
<xsl:template match="db:toc">
<xsl:choose>
<xsl:when test="@role">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise><!--<toc role="other"><xsl:apply-templates select="@*|node()"/></toc>--></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:preface">
<xsl:choose>
<xsl:when test="@role = 'legalContents'">
<xsl:choose>
<xsl:when
test="contains(translate(db:info/db:title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstyvwxyz'), 'legislation')">
<toc role="legislation">
<xsl:apply-templates select="@xml:id | node()"/>
</toc>
</xsl:when>
<xsl:when
test="contains(translate(db:info/db:title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstyvwxyz'), 'cases')">
<toc role="cases">
<xsl:apply-templates select="@xml:id | node()"/>
</toc>
</xsl:when>
<xsl:when
test="contains(translate(db:info/db:title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstyvwxyz'), 'statutes')">
<toc role="statutes">
<xsl:apply-templates select="@xml:id | node()"/>
</toc>
</xsl:when>
<xsl:when
test="contains(translate(db:info/db:title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstyvwxyz'), 'statutory')">
<toc role="legislation">
<xsl:apply-templates select="@xml:id | node()"/>
</toc>
</xsl:when>
<xsl:otherwise>
<toc role="legislation">
<xsl:apply-templates select="@xml:id | node()"/>
</toc>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="@role = 'contributors'">
<xsl:copy>
<xsl:apply-templates select="@xml:id | node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:chapter">
<xsl:choose>
<!-- specifically for case reports -->
<xsl:when
test="ancestor::db:book/db:info[db:biblioid[@otherclass = 'legalContentType'] = 'cases']">
<chapter role="case">
<xsl:apply-templates select="@* | node()"/>
</chapter>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template
match="db:chapter/db:info | db:part/db:info | db:appendix/db:info | db:section/db:info"
exclude-result-prefixes="#all">
<xsl:choose>
<!-- If there is a label, need that as an element -->
<xsl:when test="parent::node()/@label">
<info xml:id="{@xml:id}">
<xsl:choose>
<!-- But we don't need them as labels for nested sections -->
<xsl:when test="ancestor::db:section/parent::db:section"/>
<xsl:otherwise>
<label>
<xsl:choose>
<xsl:when test="parent::node[@role = 'labelOverRide']">
<xsl:value-of select="parent::node[@role = 'labelOverRide']"
/>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="parent::db:chapter">
<xsl:text>Chapter </xsl:text>
<xsl:value-of select="parent::node()/@label"/>
</xsl:when>
<xsl:when test="parent::db:part">
<xsl:text>Part </xsl:text>
<xsl:value-of select="parent::node()/@label"/>
</xsl:when>
<xsl:when test="parent::db:appendix">
<xsl:text>Appendix </xsl:text>
<xsl:value-of select="parent::node()/@label"/>
</xsl:when>
<!-- We only need the labels for level 1 sections -->
<xsl:when test="parent::db:section">
<xsl:value-of select="parent::node()/@label"/>
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</label>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="node()"/>
</info>
</xsl:when>
<!-- Case Reports -->
<xsl:when
test="parent::db:chapter and ancestor::db:book/db:info[db:biblioid[@otherclass = 'legalContentType'] = 'cases']">
<info xml:id="{@xml:id}">
<xsl:apply-templates select="db:title"/>
<xsl:if test="db:biblioid[@otherclass = 'caseCitation'] and not(db:subtitle)">
<subtitle>
<xsl:value-of select="db:biblioid[@otherclass = 'caseCitation']"/>
</subtitle>
</xsl:if>
<xsl:apply-templates
select="node()[not(self::db:biblioid[@otherclass = 'caseCitation']) and not(self::db:title)]"/>
<xsl:if test="following-sibling::db:para[@role = 'court']">
<hearing>
<court>
<xsl:value-of select="following-sibling::db:para[@role = 'court']"/>
</court>
<xsl:if test="following-sibling::db:para[@role = 'hearingDate']">
<date>
<xsl:attribute name="value">
<xsl:value-of
select="db:biblioid[@otherclass = 'decisionDate']"/>
</xsl:attribute>
<xsl:value-of
select="following-sibling::db:para[@role = 'hearingDate']"/>
</date>
</xsl:if>
<xsl:if test="following-sibling::db:para[@role = 'panel']">
<judge>
<xsl:value-of
select="following-sibling::db:para[@role = 'panel']"/>
</judge>
</xsl:if>
</hearing>
</xsl:if>
</info>
</xsl:when>
<!-- Throw away front and back containers -->
<xsl:when test="parent::node()/@role = 'front' or parent::node()/@role = 'back'"/>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:section">
<xsl:choose>
<xsl:when
test="ancestor::db:book/db:info[db:biblioid[@otherclass = 'legalContentType'] = 'cases']">
<section xml:id="{@xml:id}" chunking="none">
<xsl:apply-templates select="node()"/>
</section>
</xsl:when>
<xsl:when test="db:info[db:biblioid[@otherclass = 'legalDivType']]">
<xsl:variable name="myActID"
select="translate(ancestor::db:chapter/@xml:id, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:choose>
<xsl:when
test="db:info[db:biblioid[@otherclass = 'legalDivType'] = 'section']">
<ifp:legislation-marker>
<xsl:attribute name="id">
<xsl:value-of select="$myActID"/>
<xsl:text>/</xsl:text>
<xsl:value-of
select="db:info/db:biblioid[@otherclass = 'legalDivType']"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="@label"/>
</xsl:attribute>
</ifp:legislation-marker>
</xsl:when>
<xsl:otherwise>
<xsl:message>Unhandled legalDivType [<xsl:value-of select="@xml:id"
/>]</xsl:message>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:biblioid[@otherclass = 'legalDivType']"/>
<xsl:template match="db:note">
<xsl:choose>
<xsl:when test="@role = 'annotations'">
<formalpara xml:id="{@xml:id}" role="example">
<xsl:apply-templates select="node()"/>
</formalpara>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- hack to suppress block codes in looseleaf content, e.g. #FootnoteB, #FootnoteE:
these should be marked for suppression in the DocBook input to this transform -->
<xsl:template match="db:para[starts-with(normalize-space(.), '#')][ends-with(., 'B') or ends-with(., 'E')]"/>
<xsl:template match="db:formalpara/db:para[starts-with(normalize-space(.), '#')][ends-with(., 'B') or ends-with(., 'E')]"
priority="10">
<xsl:copy>
<xsl:apply-templates select="@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="db:para">
<xsl:choose>
<!-- various case report conditions -->
<xsl:when test="@role = 'court'"/>
<xsl:when test="@role = ('hearingDate', 'panel')">
<xsl:choose>
<xsl:when test="preceding-sibling::db:para[@role = 'court']"/>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="@role = 'catchwords'">
<para xml:id="{@xml:id}" role="keywords">
<xsl:apply-templates select="node()"/>
</para>
</xsl:when>
<xsl:when test="@role = 'judgmentBy'">
<para xml:id="{@xml:id}" role="judgmentBy">
<emphasis role="bold">
<xsl:apply-templates select="node()"/>
</emphasis>
</para>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:bridgehead">
<xsl:choose>
<xsl:when test="@renderas = 'sect1'">
<para xml:id="{@xml:id}">
<emphasis role="bold">
<xsl:apply-templates select="node()"/>
</emphasis>
</para>
</xsl:when>
<xsl:when test="@renderas = 'sect2'">
<para xml:id="{@xml:id}">
<emphasis role="italic">
<xsl:apply-templates select="node()"/>
</emphasis>
</para>
</xsl:when>
<xsl:otherwise>
<para xml:id="{@xml:id}">
<emphasis role="smallcaps">
<xsl:apply-templates select="node()"/>
</emphasis>
</para>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:biblioid[@class = 'doi']">
<xsl:choose>
<!-- The chunk DOIs in the source data break the display of the chunk in the BP site, so we must strip them out -->
<xsl:when test="ancestor::db:chapter or ancestor::db:part"/>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:variablelist">
<xsl:choose>
<xsl:when test="parent::db:preface[@role = 'legalContents'] or parent::db:toc or parent::db:index">
<tocdiv>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</tocdiv>
</xsl:when>
<xsl:when test="ancestor::db:preface[@role = 'legalContents'] or ancestor::db:toc or ancestor::db:index">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<itemizedlist role="simple">
<xsl:apply-templates select="@* | node()"/>
</itemizedlist>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:variablelist/db:info/db:title | db:formalpara/db:info/db:title">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<emphasis role="bold">
<xsl:apply-templates select="node()"/>
</emphasis>
</xsl:copy>
</xsl:template>
<xsl:template match="db:varlistentry">
<xsl:choose>
<xsl:when test="ancestor::db:preface[@role = 'legalContents']">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:when>
<xsl:when test="ancestor::db:toc">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:when>
<xsl:when test="ancestor::db:index">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="db:listitem" mode="varlist"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:listitem" mode="varlist">
<listitem>
<xsl:apply-templates select="@*"/>
<simpara><xsl:apply-templates select="preceding-sibling::db:term" mode="varlist"/></simpara>
<xsl:apply-templates select="node()"/>
</listitem>
</xsl:template>
<xsl:template match="db:term" mode="varlist">
<xsl:apply-templates select="@* | node()"/>
</xsl:template>
<!-- External links and emails don't additionally need their in-text URL marked up -->
<xsl:template match="db:uri[parent::db:link[@xlink:href]]">
<xsl:apply-templates select="@* | node()"/>
</xsl:template>
<xsl:template match="db:email[parent::db:link[@xlink:href]]">
<xsl:apply-templates select="@* | node()"/>
</xsl:template>
<!-- TABLES -->
<xsl:template match="db:table | db:informaltable">
<informaltable>
<xsl:apply-templates select="@xml:id | node()"/>
</informaltable>
</xsl:template>
<!-- Don't need the info wrapper -->
<xsl:template match="db:table/db:info">
<xsl:if test="parent::db:table/@label">
<label>
<emphasis role="bold">
<xsl:text>Table </xsl:text>
<xsl:value-of select="parent::db:table/@label"/>
<xsl:text>: </xsl:text>
</emphasis>
</label>
</xsl:if>
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="db:table/db:info/db:title">
<caption>
<emphasis role="bold">
<xsl:apply-templates select="node()"/>
</emphasis>
</caption>
</xsl:template>
<!-- Figures -->
<xsl:template match="db:figure">
<xsl:variable name="label" select="@label"/>
<figure>
<xsl:apply-templates select="@*[not(parent::node()/@label)]"/>
<info>
<title>
<xsl:if test="@label">
<emphasis role="bold">
<xsl:text>Figure </xsl:text>
<xsl:value-of select="@label"/>
<xsl:text>: </xsl:text>
</emphasis>
</xsl:if>
<emphasis role="bold">
<xsl:apply-templates select="db:info/db:title"/>
</emphasis>
</title>
</info>
<xsl:apply-templates select="db:mediaobject"/>
</figure>
</xsl:template>
<!-- New DocBook uses the CALS model, whereas the Bloomsbury Professional model uses the HTML one -->
<xsl:template match="db:tgroup">
<!-- <xsl:if test="db:colspec">
<xsl:for-each-group select="db:colspec" group-by=".">
<colgroup>
<xsl:apply-templates select="current-group()"/>
</colgroup>
</xsl:for-each-group>
</xsl:if>-->
<xsl:apply-templates select="node()[not(self::db:colspec)]"/>
</xsl:template>
<xsl:template match="db:colspec">
<!-- Add a colgroup wrapper around all of them -->
<col>
<xsl:apply-templates select="@* | node()"/>
</col>
</xsl:template>
<xsl:template match="db:row">
<tr>
<xsl:apply-templates select="@* | node()"/>
</tr>
</xsl:template>
<xsl:template match="db:entry">
<xsl:variable name="position">
<xsl:value-of select="count(preceding-sibling::db:entry) + 1"/>
</xsl:variable>
<xsl:variable name="align">
<xsl:value-of select="ancestor::db:tgroup/db:colspec[@colnum = $position]/@align"/>
</xsl:variable>
<xsl:variable name="namest">
<xsl:value-of select="@namest"/>
</xsl:variable>
<xsl:variable name="namest-colno">
<xsl:value-of select="substring-after($namest,'col')"/>
</xsl:variable>
<xsl:variable name="nameend">
<xsl:value-of select="@nameend"/>
</xsl:variable>
<xsl:variable name="nameend-colno">
<xsl:value-of select="substring-after($nameend,'col')"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="ancestor::db:thead">
<th>
<xsl:if test="@morerows">
<xsl:attribute name="rowspan">
<xsl:value-of select="number(@morerows) + 1"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@namest">
<xsl:attribute name="colspan">
<xsl:value-of
select="number($nameend-colno) - number($namest-colno) + 1"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@xml:id | node()"/>
</th>
</xsl:when>
<xsl:otherwise>
<!--<xsl:message>position=<xsl:value-of select="$position"/>; align=<xsl:value-of select="$align"/>; ID=<xsl:value-of select="@xml:id"/></xsl:message>-->
<td>
<xsl:if test="string-length($align) gt 0">
<xsl:attribute name="align">
<xsl:value-of select="$align"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@morerows">
<xsl:attribute name="rowspan">
<xsl:value-of select="number(@morerows) + 1"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@namest">
<xsl:attribute name="colspan">
<xsl:value-of
select="number($nameend-colno) - number($namest-colno) + 1"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="@xml:id | node()"/>
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- LISTS -->
<xsl:template match="db:itemizedlist">
<xsl:choose>
<xsl:when test="db:listitem[@w:type[not(. = '●')]] and not(@mark)">
<itemizedlist xml:id="{@xml:id}" role="simple">
<xsl:apply-templates select="node()"/>
</itemizedlist>
</xsl:when>
<xsl:when test="not(@mark) and db:listitem/db:para/text()[matches(.,'^\([\w]\)\s')]">
<itemizedlist xml:id="{@xml:id}" role="simple">
<xsl:apply-templates select="node()"/>
</itemizedlist>
</xsl:when>
<xsl:when test="not(@mark)">
<itemizedlist xml:id="{@xml:id}" role="bulleted">
<xsl:apply-templates select="node()"/>
</itemizedlist>
</xsl:when>
<xsl:when test="@mark = 'none'">
<itemizedlist xml:id="{@xml:id}" role="simple">
<xsl:apply-templates select="node()"/>
</itemizedlist>
</xsl:when>
<xsl:otherwise>
<itemizedlist xml:id="{@xml:id}" role="bulleted">
<xsl:apply-templates select="node()"/>
</itemizedlist>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:listitem[@w:type[not(. = '●')]]">
<listitem xml:id="{@xml:id}">
<xsl:apply-templates select="node()"/>
</listitem>
</xsl:template>
<xsl:template match="db:para[1][parent::db:listitem[@w:type[not(. = '●')]]]">
<para xml:id="{@xml:id}"><xsl:value-of select="parent::db:listitem/@w:type"/><xsl:text> </xsl:text><xsl:apply-templates select="node()"/>
</para>
</xsl:template>
<!-- INLINE ELEMENTS -->
<xsl:template match="db:phrase">
<xsl:choose>
<xsl:when test="@role">
<xsl:choose>
<xsl:when test="@role = 'caseName'">
<case>
<xsl:apply-templates select="node()"/>
</case>
</xsl:when>
<xsl:when test="@role = 'caseCitation'">
<xsl:apply-templates select="node()"/>
</xsl:when>
<xsl:when test="@role = 'incompleteXref'">
<xsl:apply-templates select="node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<phrase role="default">
<xsl:apply-templates select="node()"/>
</phrase>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:author">
<xsl:copy>
<!-- Don't need @condition to be carried through -->
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="db:bibliolist">
<xsl:choose>
<xsl:when test="@role = 'inline'">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:attribute name="role">block</xsl:attribute>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:orderedlist">
<xsl:if test="parent::db:orderedlist[@continuation = 'continues']">
<xsl:message>List <xsl:value-of select="parent::db:orderedlist/@xml:id"/> continues
previous numbering; NOT HANDLED</xsl:message>
</xsl:if>
<xsl:choose>
<xsl:when test="@numeration">
<itemizedlist role="simple">
<xsl:attribute name="xml:id">
<xsl:value-of select="@xml:id"/>
</xsl:attribute>
<xsl:apply-templates select="db:listitem" mode="BPlist"/>
</itemizedlist>
</xsl:when>
<xsl:otherwise>
<itemizedlist role="simple">
<xsl:attribute name="xml:id">
<xsl:value-of select="@xml:id"/>
</xsl:attribute>
<xsl:apply-templates select="db:listitem" mode="BPlist"/>
</itemizedlist>
<!-- <xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
-->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="db:listitem" mode="BPlist">
<xsl:variable name="position" select="number(count(preceding-sibling::db:listitem)) + 1"/>
<xsl:variable name="listitemNumber">
<xsl:choose>
<!-- handle continuation -->
<xsl:when test="parent::db:orderedlist[@continuation = 'continues']"/>
<xsl:when test="parent::db:orderedlist[@startingnumber]">
<xsl:value-of select="parent::db:orderedlist/@startingnumber + $position - 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$position"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="labelValue">
<xsl:choose>
<xsl:when test="parent::db:orderedlist[not(@numeration)]">
<xsl:value-of select="$listitemNumber"/>
</xsl:when>
<xsl:when test="parent::db:orderedlist[@numeration = 'arabic']">
<xsl:value-of select="$listitemNumber"/>
</xsl:when>
<xsl:when test="parent::db:orderedlist[@numeration = 'loweralpha']">
<xsl:choose>
<xsl:when test="$listitemNumber = 1">a</xsl:when>
<xsl:when test="$listitemNumber = 2">b</xsl:when>
<xsl:when test="$listitemNumber = 3">c</xsl:when>
<xsl:when test="$listitemNumber = 4">d</xsl:when>
<xsl:when test="$listitemNumber = 5">e</xsl:when>
<xsl:when test="$listitemNumber = 6">f</xsl:when>
<xsl:when test="$listitemNumber = 7">g</xsl:when>
<xsl:when test="$listitemNumber = 8">h</xsl:when>
<xsl:when test="$listitemNumber = 9">i</xsl:when>
<xsl:when test="$listitemNumber = 10">j</xsl:when>
<xsl:when test="$listitemNumber = 11">k</xsl:when>
<xsl:when test="$listitemNumber = 12">l</xsl:when>
<xsl:when test="$listitemNumber = 13">m</xsl:when>
<xsl:when test="$listitemNumber = 14">n</xsl:when>
<xsl:when test="$listitemNumber = 15">o</xsl:when>
<xsl:when test="$listitemNumber = 16">p</xsl:when>
<xsl:when test="$listitemNumber = 17">q</xsl:when>
<xsl:when test="$listitemNumber = 18">r</xsl:when>
<xsl:when test="$listitemNumber = 19">s</xsl:when>
<xsl:when test="$listitemNumber = 20">t</xsl:when>
<xsl:when test="$listitemNumber = 21">u</xsl:when>
<xsl:when test="$listitemNumber = 22">v</xsl:when>
<xsl:when test="$listitemNumber = 23">w</xsl:when>
<xsl:when test="$listitemNumber = 24">x</xsl:when>
<xsl:when test="$listitemNumber = 25">y</xsl:when>
<xsl:when test="$listitemNumber = 26">z</xsl:when>
<xsl:otherwise>
<xsl:message><xsl:value-of select="parent::db:orderedlist/@numeration"/>
listitem <xsl:value-of select="@xml:id"/> not handled; position =
<xsl:value-of select="$position"/></xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="parent::db:orderedlist[@numeration = 'upperalpha']">
<xsl:choose>
<xsl:when test="$listitemNumber = 1">A</xsl:when>
<xsl:when test="$listitemNumber = 2">B</xsl:when>
<xsl:when test="$listitemNumber = 3">C</xsl:when>
<xsl:when test="$listitemNumber = 4">D</xsl:when>
<xsl:when test="$listitemNumber = 5">E</xsl:when>
<xsl:when test="$listitemNumber = 6">F</xsl:when>
<xsl:when test="$listitemNumber = 7">G</xsl:when>
<xsl:when test="$listitemNumber = 8">H</xsl:when>
<xsl:when test="$listitemNumber = 9">I</xsl:when>
<xsl:when test="$listitemNumber = 10">J</xsl:when>
<xsl:when test="$listitemNumber = 11">K</xsl:when>
<xsl:when test="$listitemNumber = 12">L</xsl:when>
<xsl:when test="$listitemNumber = 13">M</xsl:when>
<xsl:when test="$listitemNumber = 14">N</xsl:when>
<xsl:when test="$listitemNumber = 15">O</xsl:when>
<xsl:when test="$listitemNumber = 16">P</xsl:when>
<xsl:when test="$listitemNumber = 17">Q</xsl:when>
<xsl:when test="$listitemNumber = 18">R</xsl:when>
<xsl:when test="$listitemNumber = 19">S</xsl:when>
<xsl:when test="$listitemNumber = 20">T</xsl:when>
<xsl:when test="$listitemNumber = 21">U</xsl:when>
<xsl:when test="$listitemNumber = 22">V</xsl:when>
<xsl:when test="$listitemNumber = 23">W</xsl:when>
<xsl:when test="$listitemNumber = 24">X</xsl:when>
<xsl:when test="$listitemNumber = 25">Y</xsl:when>
<xsl:when test="$listitemNumber = 26">Z</xsl:when>
<xsl:otherwise>
<xsl:message><xsl:value-of select="parent::db:orderedlist/@numeration"/>
listitem <xsl:value-of select="@xml:id"/> not handled; position =
<xsl:value-of select="$position"/></xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="parent::db:orderedlist[@numeration = 'lowerroman']">
<xsl:choose>
<xsl:when test="$listitemNumber = 1">i</xsl:when>
<xsl:when test="$listitemNumber = 2">ii</xsl:when>
<xsl:when test="$listitemNumber = 3">iii</xsl:when>
<xsl:when test="$listitemNumber = 4">iv</xsl:when>
<xsl:when test="$listitemNumber = 5">v</xsl:when>
<xsl:when test="$listitemNumber = 6">vi</xsl:when>
<xsl:when test="$listitemNumber = 7">vii</xsl:when>
<xsl:when test="$listitemNumber = 8">viii</xsl:when>
<xsl:when test="$listitemNumber = 9">ix</xsl:when>
<xsl:when test="$listitemNumber = 10">x</xsl:when>
<xsl:when test="$listitemNumber = 11">xi</xsl:when>
<xsl:when test="$listitemNumber = 12">xii</xsl:when>
<xsl:when test="$listitemNumber = 13">xiii</xsl:when>
<xsl:when test="$listitemNumber = 14">xiv</xsl:when>
<xsl:when test="$listitemNumber = 15">xv</xsl:when>
<xsl:when test="$listitemNumber = 16">xvi</xsl:when>
<xsl:when test="$listitemNumber = 17">xvii</xsl:when>
<xsl:when test="$listitemNumber = 18">xviii</xsl:when>
<xsl:when test="$listitemNumber = 19">xix</xsl:when>
<xsl:when test="$listitemNumber = 20">xx</xsl:when>
<xsl:when test="$listitemNumber = 21">xxi</xsl:when>
<xsl:when test="$listitemNumber = 22">xxii</xsl:when>
<xsl:when test="$listitemNumber = 23">xxiii</xsl:when>
<xsl:when test="$listitemNumber = 24">xxiv</xsl:when>
<xsl:when test="$listitemNumber = 25">xxv</xsl:when>
<xsl:when test="$listitemNumber = 26">xxvi</xsl:when>
<xsl:otherwise>
<xsl:message><xsl:value-of select="parent::db:orderedlist/@numeration"/>
listitem <xsl:value-of select="@xml:id"/> not handled; position =
<xsl:value-of select="$position"/></xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:message><xsl:value-of select="parent::db:orderedlist/@numeration"/>
listitem <xsl:value-of select="@xml:id"/> not handled; position =
<xsl:value-of select="$position"/></xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="startingnumber" select="@startingnumber"/>
<listitem>
<xsl:attribute name="xml:id">
<xsl:value-of select="@xml:id"/>
</xsl:attribute>
<simpara>(<xsl:value-of select="$labelValue"/>)</simpara>
<xsl:apply-templates select="node()"/>
</listitem>
</xsl:template>
<xsl:template match="db:index">
<xsl:copy>
<xsl:apply-templates select="@*, db:info/db:title, db:info/following-sibling::*"/>
<!-- TODO: construct index here, from:
<indexterm class="singular" xml:id="b-9781784515393-1100372"><primary><emphasis role="bold">Subsidiaries</emphasis></primary><secondary>consolidated financial statements</secondary><seealso><emphasis role="bold">Consolidated financial statements</emphasis></seealso></indexterm>
to:
<indexdiv xml:id="CN5-index-UID3155">
<title>A</title>
<indexentry>
<primaryie xml:id="CN5-index-1">
<phrase role="indexTerm">Abdominal aortic aneurysms</phrase>
</primaryie>
<secondaryie>
<phrase role="indexTerm">elective treatment,</phrase>
<phrase role="indexRefs">&#x0009;<link linkend="CN5-s26_47"
>26.47</link>&#x2013;<link linkend="CN5-s26_53">26.53</link></phrase>
</secondaryie>
<secondaryie>
<phrase role="indexTerm">ruptured,</phrase>
<phrase role="indexRefs">&#x0009;<link linkend="CN5-s26_54"
>26.54</link>&#x2013;<link linkend="CN5-s26_56">26.56</link></phrase>
</secondaryie>
</indexentry>-->
<!-- TODO: normalize sort key: initial letter might be punctuation -->
<!-- 1. distinct values of indexterm/primary
2. get distinct values of subordinate indexterms
3. sort, inserting para references:
a. get start and end range
b. get ID of first formalpara ancestor for both
i. if they match, tab <link id='ID'>formal para title</>
ii. otherwise, as above, but separate start and end with em dash.
-->
<!-- distinct first letters -->
<xsl:for-each select="distinct-values(
for $x in $indexterms/db:primary/replace(., '&#x2018;', '')
return substring($x, 1, 1)
)">
<xsl:sort select="."/>
<xsl:variable name="letter" select="."/>
<xsl:message>index div title=<xsl:value-of select="$letter"/></xsl:message>
<indexdiv>
<title><xsl:value-of select="$letter"/></title>
<!-- distinct primary index terms -->
<xsl:for-each select="distinct-values($indexterms/db:primary[matches(., concat('^&#x2018;?', $letter))])">
<xsl:sort select="lower-case(replace(., '^&#x2018;', ''))"/>
<xsl:message>primary=<xsl:value-of select="."/>; letter=<xsl:value-of select="$letter"/>; current=<xsl:value-of select="current()"/></xsl:message>
<indexentry><xsl:apply-templates select="($indexterms/db:primary[. = current()])[1]"/></indexentry>
</xsl:for-each>
</indexdiv>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="db:indexterm[not(@class='endofrange')]">
<indexentry>
<xsl:apply-templates/>
</indexentry>
</xsl:template>
<xsl:key name="secondary-by-primary" match="db:secondary" use="../db:primary"/>
<xsl:template match="db:indexterm/db:primary">
<xsl:variable name="primary" select="."/>
<primaryie xml:id='{../@xml:id}'>
<phrase role='indexTerm'><xsl:apply-templates/></phrase>
</primaryie>
<xsl:variable name="secondary" select="key('secondary-by-primary', $primary)"/>
<xsl:variable name="seeSeeAlso" select="following-sibling::*[1][self::db:see or self::db:seealso]"/>
<xsl:variable name="secondary">
<xsl:for-each select="distinct-values($secondary)">
<xsl:sort select="lower-case(replace(., '^&#x2018;', ''))"/>
<xsl:message>secondary=<xsl:value-of select="."/></xsl:message>
<xsl:apply-templates select="($secondary[. = current()])[1]"/>
</xsl:for-each>
</xsl:variable>
<xsl:apply-templates select="$secondary | $seeSeeAlso"/>
</xsl:template>
<xsl:key name="tertiary-by-secondary" match="db:tertiary" use="../db:secondary"/>
<xsl:template match="db:indexterm/db:secondary">
<xsl:variable name="primary" select="../db:primary"/>
<xsl:variable name="secondary" select="."/>
<secondaryie>
<phrase role='indexTerm'><xsl:apply-templates/></phrase>
<!-- TODO: this template is only applied to the FIRST term -->
<xsl:if test="not(../db:tertiary)">
<xsl:variable name="indexRefs">
<xsl:apply-templates select=".." mode="index-refs"/>
</xsl:variable>
<xsl:if test="$indexRefs">
<phrase role='indexRefs'>
<xsl:for-each select="$indexRefs">
<xsl:copy-of select="."/>
<xsl:if test="position() != last()">, </xsl:if>
</xsl:for-each>
</phrase>
</xsl:if>
</xsl:if>
</secondaryie>
<xsl:variable name="tertiary" select="key('tertiary-by-secondary', $secondary)[../db:primary = $primary]"/>
<xsl:variable name="seeSeeAlso" select="following-sibling::*[1][self::db:see or self::db:seealso]"/>
<xsl:variable name="tertiary">
<xsl:for-each select="distinct-values($tertiary)">
<xsl:sort select="lower-case(replace(., '^&#x2018;', ''))"/>
<xsl:message>tertiary=<xsl:value-of select="."/></xsl:message>
<xsl:sequence select="($tertiary[. = current()])[1]"/>
</xsl:for-each>
</xsl:variable>
<xsl:apply-templates select="$tertiary | $seeSeeAlso"/>
</xsl:template>
<xsl:template match="db:indexterm/db:tertiary">
<tertiaryie>
<phrase role='indexTerm'><xsl:apply-templates/></phrase>
<!-- TODO: this template is only applied to the FIRST term -->
<xsl:variable name="indexRefs">
<xsl:apply-templates select=".." mode="index-refs"/>
</xsl:variable>
<xsl:if test="$indexRefs">
<phrase role='indexRefs'>
<xsl:for-each select="$indexRefs">
<xsl:copy-of select="."/>
<xsl:if test="position() != last()">, </xsl:if>
</xsl:for-each>
</phrase>
</xsl:if>
</tertiaryie>
</xsl:template>
<xsl:template match="db:indexterm[@class = 'singular']" mode="index-refs">
<xsl:variable name="formalpara" select="ancestor::db:formalpara[1]"/>
<link linkend="{$formalpara/@xml:id}">
<xsl:value-of select="$formalpara/db:info/db:title"/>
</link>
</xsl:template>
<xsl:key name="end-of-range" match="db:indexterm[@class='endofrange']" use="@startref"/>
<xsl:template match="db:indexterm[@class = 'startofrange']" mode="index-refs">
<xsl:variable name="start" select="ancestor::db:formalpara[1]"/>
<xsl:variable name="end" select="key('end-of-range', @xml:id)/ancestor::db:formalpara[1]"/>
<!-- link to start of range -->
<link linkend="{$start/@xml:id}">
<xsl:value-of select="$start/db:info/db:title"/>
</link>
<!-- link to end of range, if it spans formalparas -->
<xsl:if test="not($start is $end)">
<xsl:text>&#x2014;</xsl:text>
<link linkend="{$end/@xml:id}">
<xsl:value-of select="$end/db:info/db:title"/>
</link>
</xsl:if>
</xsl:template>
<xsl:key name="primary-by-string" match="db:primary" use="."/>
<xsl:template match="db:indexterm/db:see | db:indexterm/db:seealso">
<xsl:element name="{local-name()}ie">
<!-- see and seealso don't contain phrase[@role='indexTerm'] -->
<emphasis role='italic'>
<xsl:text>see</xsl:text>
<xsl:if test="self::db:seealso"> also</xsl:if>
</emphasis>
<link linkend="{key('primary-by-string', .)/ancestor::db:formalpara[1]/@xml:id}">
<xsl:apply-templates/>
</link>
</xsl:element>
<xsl:apply-templates select="following-sibling::*[1][self::db:see or self::db:seeAlso]"/>
</xsl:template>
<xsl:template match="db:indexterm[@class='endofrange']"/>
<xsl:template match="element()[@condition = 'hidden']"/>
<xsl:template match="@w:*"/>
</xsl:stylesheet>