-
Notifications
You must be signed in to change notification settings - Fork 23
/
tei__hocr.xsl
162 lines (150 loc) · 5.54 KB
/
tei__hocr.xsl
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:util="http://example/com/util/namespace"
version="2.0"
exclude-result-prefixes="xsl util"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html" encoding="UTF-8" indent="yes"
omit-xml-declaration="yes" />
<xsl:param name="docTitle" select="'document_name'"/>
<xsl:param name="langs" select="'de'"/>
<xsl:param name="npages" select="1"/>
<xsl:param name="scripts" select="'Latg'"/>
<xsl:param name="system" select="'unknown'"/>
<xsl:param name="left" select="-1"/>
<xsl:param name="top" select="-1"/>
<xsl:param name="width" select="-1"/>
<xsl:param name="height" select="-1"/>
<!-- converts comma-separated to space-separated coordinates -->
<xsl:function name="util:coords">
<xsl:param name="coords" />
<xsl:value-of select="replace($coords, ',', ' ')" />
</xsl:function>
<!-- calculates bounding box of all nodes with attribute 'function' -->
<xsl:function name="util:get-pagebox">
<xsl:param name="nodes" />
<xsl:variable name="bbox">
<xsl:choose>
<xsl:when test="$left = -1">
<xsl:for-each select="$nodes">
<xsl:sort select="tokenize(./@function, ',')[1]" data-type="number" order="ascending" />
<xsl:if test="position() = 1">
<xsl:value-of select="tokenize(./@function, ',')[1]" />
</xsl:if>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$left" />
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$top = -1">
<xsl:for-each select="$nodes">
<xsl:sort select="tokenize(./@function, ',')[2]" data-type="number" order="ascending" />
<xsl:if test="position() = 1">
<xsl:value-of select="tokenize(./@function, ',')[2]" />
</xsl:if>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$top" />
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$width = -1">
<xsl:for-each select="$nodes">
<xsl:sort select="tokenize(./@function, ',')[3]" data-type="number" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="tokenize(./@function, ',')[3]" />
</xsl:if>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$width" />
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$height = -1">
<xsl:for-each select="$nodes">
<xsl:sort select="tokenize(./@function, ',')[4]" data-type="number" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="tokenize(./@function, ',')[4]" />
</xsl:if>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$height" />
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="normalize-space($bbox)" />
</xsl:function>
<!-- Start of transformation -->
<xsl:template match="/">
<html>
<head>
<title>
<xsl:value-of select="$docTitle" />
</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="ocr-system" content="{$system}" />
<meta name="ocr-capabilities" content="ocr_page ocr_carea ocr_par ocr_line ocrx_word" />
<meta name="ocr-langs" content="{$langs}" />
<meta name="ocr-number-of-pages" content="{$npages}" />
<meta name="ocr-scripts" content="{$scripts}" />
</head>
<xsl:apply-templates select=".//text" />
</html>
</xsl:template>
<xsl:template match="text">
<body>
<xsl:apply-templates select=".//milestone" />
</body>
</xsl:template>
<!-- Page -->
<xsl:template match="milestone[@type='page']">
<xsl:variable name="pageno" select="@n" />
<xsl:variable name="pagenodes" select="//*[@function]" />
<xsl:variable name="pagebox" select="util:get-pagebox($pagenodes)" />
<div class="ocr_page" id="page_{$pageno}" title="image "{$docTitle}"; bbox {$pagebox}; ppageno {$pageno - 1}">
<div class="ocr_carea" id="block_{$pageno}" title="bbox {$pagebox}">
<xsl:apply-templates select="//p|//figure" />
</div>
</div>
</xsl:template>
<!-- Paragraph -->
<xsl:template match="p">
<xsl:variable name="pid" select="@id" />
<p class="ocr_par" id="{$pid}">
<xsl:apply-templates select="./w" />
</p>
</xsl:template>
<!-- Word -->
<xsl:template match="w">
<xsl:variable name="bbox" select="util:coords(@function)" />
<span class="ocrx_word" title="bbox {$bbox}">
<xsl:value-of select="text()" />
</span>
</xsl:template>
<!-- Figure -->
<xsl:template match="figure">
<xsl:variable name="bbox" select="util:coords(@function)" />
<div class="ocr_float" title="bbox {$bbox}" />
</xsl:template>
<!-- Unmatched Elements -->
<xsl:template match="*">
<xsl:message terminate="no">
WARNING: Unmatched element: <xsl:value-of select="name()"/>
</xsl:message>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>