-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpreprocess.xsl
More file actions
134 lines (107 loc) · 5.17 KB
/
preprocess.xsl
File metadata and controls
134 lines (107 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="3.0"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:f="urn:stylesheet-functions"
xmlns:xd="http://www.pnp-software.com/XSLTdoc"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="f h xd">
<xsl:output indent="no" method="xml" encoding="utf-8"/>
<xsl:include href="modules/stripns.xsl"/>
<xsl:include href="modules/normalize-table.xsl"/>
<xd:doc type="stylesheet">
<xd:short>Stylesheet to preprocess TEI documents.</xd:short>
<xd:detail><h:p>This stylesheet preprocesses TEI documents, so the final conversion to HTML
can be handled more easily.</h:p>
<h:p>The following aspects are handled:</h:p>
<h:ul>
<h:li>1. Strip the TEI namespace if present (see stripns.xsl).</h:li>
<h:li>2. Normalize tables (see normalize-table.xsl).</h:li>
<h:li>3. Remove superfluous attributes.</h:li>
</h:ul>
</xd:detail>
</xd:doc>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Remove TEIform attributes as they are TEI-specific metadata not needed in output -->
<xsl:template match="@TEIform" mode="#all"/>
<xsl:template match="table[f:has-rend-value(@rend, 'transpose')]">
<xsl:variable name="transposed-table">
<xsl:apply-templates select="." mode="transpose"/>
</xsl:variable>
<xsl:apply-templates select="$transposed-table/table" mode="normalize-table"/>
</xsl:template>
<xsl:template match="table">
<xsl:apply-templates select="." mode="normalize-table"/>
</xsl:template>
<xsl:function name="f:is-margin-note" as="xs:boolean">
<xsl:param name="note" as="element(note)"/>
<xsl:sequence select="$note/@place = 'margin' or $note/@type = 'margin'"/>
</xsl:function>
<xsl:function name="f:has-preceding-margin-note" as="xs:boolean">
<xsl:param name="node" as="element()"/>
<xsl:sequence select="boolean($node/preceding-sibling::node()[not(self::text()[normalize-space()=''])][1]/self::note[f:is-margin-note(.)])"/>
</xsl:function>
<!-- Consecutive marginal notes should be combined into a single marginal note. -->
<xsl:template match="note[f:is-margin-note(.)]">
<xsl:if test="not(f:has-preceding-margin-note(.))">
<xsl:variable name="siblings" select="(., following-sibling::node())"/>
<xsl:variable name="notes">
<xsl:iterate select="$siblings">
<xsl:choose>
<xsl:when test="self::note[f:is-margin-note(.)]">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:when test="self::text() and normalize-space(.) = ''">
<!-- Skip whitespace -->
</xsl:when>
<xsl:otherwise>
<xsl:break/>
</xsl:otherwise>
</xsl:choose>
</xsl:iterate>
</xsl:variable>
<!-- Combine the notes, copy the attributes of the first in the sequence. -->
<note>
<xsl:for-each select="@*">
<xsl:attribute name="{name(.)}" select="."/>
</xsl:for-each>
<xsl:for-each select="$notes/note">
<xsl:if test="position() > 1">
<lb>
<xsl:if test="@id">
<xsl:attribute name="id" select="@id"/>
</xsl:if>
<!-- <xsl:message>INFO: merged consecutive marginal notes.</xsl:message> -->
</lb>
</xsl:if>
<xsl:apply-templates/>
</xsl:for-each>
</note>
</xsl:if>
</xsl:template>
<!-- Suppress individual notes that are merged -->
<xsl:template match="note[f:is-margin-note(.)][f:has-preceding-margin-note(.)]" priority="1"/>
<!-- Two consecutive "phantom"-elements can be merged -->
<xsl:template match="ab[@type='phantom']" mode="#all">
<xsl:variable name="following" select="following-sibling::node()[1]"/>
<xsl:variable name="preceding" select="preceding-sibling::node()[1]"/>
<xsl:choose>
<xsl:when test="$following/self::ab[@type='phantom']">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="#current"/>
<xsl:apply-templates select="$following/node()" mode="#current"/>
</xsl:copy>
</xsl:when>
<xsl:when test="$preceding/self::ab[@type='phantom']"/>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="#current"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>