teacup/transforms/MergeBloomsburyContents.xslt

155 lines
7.6 KiB
HTML
Raw Normal View History

2021-12-15 11:29:23 +00:00
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:db="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xl="http://www.w3.org/1999/xlink" xmlns:xi='http://www.w3.org/2001/XInclude' xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:css="http://www.w3.org/1996/css" exclude-result-prefixes="xs db css">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="boxShaded" as="xs:string *">
<xsl:sequence select="('ArtworkBegin','ArtworkEnd','ArtworkB','ArtworkE','BoxShadedB','BoxShadedE','BoxSingleB','BoxSingleE','ElectronicOnly','ElectronicOnlyB','ElectronicOnlyE','ExampleB','ExampleE','IndexB','IndexE','TableB','TableE','OfficialDocB','OfficialDocE','FootnoteB','FootnoteE','StatutoryQuoteBegin','StatutoryQuoteEnd','StatutesTableB','StatutesTableE','CasesTableB','CasesTableE','SITableB','SITableE','PrecedentB','PrecedentE','HardcopyOnlyB','HardcopyOnlyE','CaseReportsB','CaseReportsE','ECLegTableB','ECLegTableE','AppendixChapE','ContributorsB','ContributorsE','PrefaceB','PrefaceE','AppendixChapB','ContChapB','ContChapE','HowToUseB','HowToUseE','AcknowledgementsB','AcknowledgementsE','ForewordB','ForewordE','LandscapeB','LandscapeE','AppendixPubB','AppendixPubE','AnnotationB','AnnotationE','PracticeDirectionB','PracticeDirectionE','ContDivB','ContDivE','HardcopytextonlyB','HardcopytextonlyE','ChronoB','ChronoE','TableBegin','TableEnd','IssuePageB','IssuePageE','StatutoryB','StatutoryE','EndorsementB','EndorsementE')"/>
</xsl:variable>
<xsl:template match="/">
<xsl:variable name="step1-Output">
<xsl:apply-templates mode="step-1"/>
</xsl:variable>
<xsl:variable name="step2-Output">
<xsl:apply-templates select="$step1-Output/node()" mode="step-2"/>
</xsl:variable>
<xsl:variable name="step3-Output">
<xsl:apply-templates select="$step2-Output/node()" mode="step-3"/>
</xsl:variable>
<xsl:variable name="searchList">
<terms>
<xsl:for-each select="$step3-Output//*:formalpara[normalize-space(*:info/*:title) != '']">
<term>
<search>
<xsl:value-of select="*:info/*:title"/>
</search>
<regex>
<xsl:text>\b</xsl:text>
<xsl:value-of select="replace(*:info/*:title, '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')"/>
<xsl:text>\b</xsl:text>
</regex>
<linkId>
<xsl:value-of select="@xml:id"/>
</linkId>
</term>
</xsl:for-each>
</terms>
</xsl:variable>
<xsl:variable name="sortedList">
<terms>
<xsl:for-each select="$searchList/terms/term[string-length(search) > 2]">
<xsl:sort select="string-length(search)" data-type="number" order="descending"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</terms>
</xsl:variable>
<xsl:variable name="step4-Output">
<xsl:apply-templates select="$step3-Output/node()" mode="step-4">
<xsl:with-param name="sortedlist" tunnel="yes">
<xsl:copy-of select="$sortedList/node()"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:variable>
<xsl:copy-of select="$step4-Output"/>
</xsl:template>
<!--Step 1 - Start - Merging Contents-->
<xsl:template match="@*|node()" mode="step-1">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="step-1"/>
</xsl:copy>
</xsl:template>
<!--<xsl:template match="*[@w:style=$boxShaded]" mode="step-1"/>-->
<xsl:template match="*:entry[@w:style = 'morerows_continued']" mode="step-1"/>
<xsl:template match="@css:*" mode="step-1"/>
<!--Step 1 - End-->
<!--Step 2 - Start - Sorting the contents-->
<xsl:template match="@*|node()" mode="step-2">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="step-2"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*:book" mode="step-2">
<book xmlns="http://docbook.org/ns/docbook">
<xsl:apply-templates select="@*" mode="step-2"/>
<xsl:apply-templates mode="step-2"/>
</book>
</xsl:template>
<xsl:template match="w:document[parent::*:book]" mode="step-2">
<xsl:copy-of select="*[not(self::*:title)]"/>
</xsl:template>
<xsl:template match="*:part[not(*:part)][parent::*:book or parent::*:part]" mode="step-2">
<xsl:copy>
<xsl:copy-of select="@xml:id"/>
<xsl:choose>
<xsl:when test="w:document/*:part">
<xsl:for-each select="w:document/*:part">
<xsl:copy-of select="node()"/>
<xsl:for-each select="parent::w:document/following-sibling::w:document">
<xsl:copy-of select="*[not(self::*:title)]"/>
</xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="w:document">
<xsl:copy-of select="*[not(self::*:title)]"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
<!--Step 2 - End-->
<!--Step 3 - Start - Adding Id attribute to all xml elements-->
<xsl:template match="@*|node()" mode="step-3">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="step-3"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*:chapter[contains(@xml:id,'-pref')][preceding-sibling::*:chapter[contains(@xml:id,'-pref')] or following-sibling::*:chapter[contains(@xml:id,'-pref')]]" mode="step-3">
<xsl:copy>
<xsl:variable name="prefCnt">
<xsl:value-of select="count(preceding-sibling::*:chapter[contains(@xml:id,'-pref')]) + 1"/>
</xsl:variable>
<xsl:attribute name="xml:id"><xsl:value-of select="concat(@xml:id,$prefCnt)"/></xsl:attribute>
<xsl:copy-of select="@* except (@xml:id)"/>
<xsl:apply-templates select="node()" mode="step-3"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[self::*:info | self::*:title | self::*:biblioset | self::*:address | self::*:legalnotice | self::*:abstract | self::*:para | self::*:section | self::*:formalpara | self::*:informaltable | self::*:blockquote | self::*:mediaobject | self::*:imageobject | self::*:entry | self::*:itemizedlist | self::*:listitem | self::*:simpara | self::*:variablelist | self::*:varlistentry | self::*:term | self::*:indexterm][not(@xml:id)]" mode="step-3">
<xsl:copy>
<xsl:attribute name="xml:id"><xsl:value-of select="concat(ancestor::*:book/@xml:id,'-',generate-id())"/></xsl:attribute>
<xsl:apply-templates select="@*|node()" mode="step-3"/>
</xsl:copy>
</xsl:template>
<!--Step 3 - End-->
<!--Step 4 - Start - Adding links-->
<xsl:template match="@*|*" mode="step-4">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="step-4"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()" mode="step-4">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()[not(ancestor::*:title) and not(ancestor::*:link)]" mode="step-4">
<xsl:param name="sortedlist" tunnel="yes"/>
<xsl:choose>
<xsl:when test="$sortedlist/*:terms/*:term">
<xsl:variable name="search" select="concat('(',string-join($sortedlist/*:terms/*:term/*:regex,'|'),')')"/>
<xsl:analyze-string select="." regex="{$search}" flags=";j">
<xsl:matching-substring>
<link linkend="{$sortedlist/*:terms/*:term[*:search=current()][1]/*:linkId}" role="xref" xmlns="http://docbook.org/ns/docbook">
<xsl:value-of select="."/>
</link>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--Step 4 - End-->
</xsl:stylesheet>