-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopf-to-html.xsl
130 lines (116 loc) · 7.36 KB
/
opf-to-html.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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nlb="http://www.nlb.no/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:opf="http://www.idpf.org/2007/opf"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all"
version="2.0">
<xsl:import href="marcrel.xsl"/>
<xsl:output indent="no" omit-xml-declaration="yes"/>
<xsl:template match="/text()"/>
<xsl:template match="/*">
<xsl:for-each select="//opf:metadata">
<xsl:text><![CDATA[ ]]></xsl:text>
<head>
<!-- Copy namespaces from @prefix -->
<xsl:if test="ancestor-or-self::*/@prefix">
<xsl:analyze-string select="ancestor-or-self::*/@prefix" regex="([^\s]+:\s+[^\s]+)">
<xsl:matching-substring>
<xsl:variable name="prefix" select="substring-before(.,':')"/>
<xsl:variable name="namespace" select="replace(.,'^[^:]*:\s*','')"/>
<xsl:namespace name="{$prefix}" select="$namespace"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:if>
<!-- Copy namespaces from namespaces -->
<xsl:for-each select="namespace::*">
<xsl:choose>
<xsl:when test="name() = ('', 'opf', 'xml')"/>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!-- Declare predefined EPUB metadata namespaces -->
<xsl:for-each select="distinct-values(*/substring-before((@property, name())[1],':'))">
<xsl:variable name="prefix" select="."/>
<xsl:choose>
<xsl:when test="$prefix = 'dc'"><xsl:namespace name="{$prefix}" select="'http://purl.org/dc/elements/1.1/'"/></xsl:when>
<xsl:when test="$prefix = 'a11y'"><xsl:namespace name="{$prefix}" select="'http://www.idpf.org/epub/vocab/package/a11y/#'"/></xsl:when>
<xsl:when test="$prefix = 'dcterms'"><xsl:namespace name="{$prefix}" select="'http://purl.org/dc/terms/'"/></xsl:when>
<xsl:when test="$prefix = 'epubsc'"><xsl:namespace name="{$prefix}" select="'http://idpf.org/epub/vocab/sc/#'"/></xsl:when>
<xsl:when test="$prefix = 'marc'"><xsl:namespace name="{$prefix}" select="'http://id.loc.gov/vocabulary/'"/></xsl:when>
<xsl:when test="$prefix = 'media'"><xsl:namespace name="{$prefix}" select="'http://www.idpf.org/epub/vocab/overlays/#'"/></xsl:when>
<xsl:when test="$prefix = 'onix'"><xsl:namespace name="{$prefix}" select="'http://www.editeur.org/ONIX/book/codelists/current.html#'"/></xsl:when>
<xsl:when test="$prefix = 'rendition'"><xsl:namespace name="{$prefix}" select="'http://www.idpf.org/vocab/rendition/#'"/></xsl:when>
<xsl:when test="$prefix = 'schema'"><xsl:namespace name="{$prefix}" select="'http://schema.org/'"/></xsl:when>
<xsl:when test="$prefix = 'xsd'"><xsl:namespace name="{$prefix}" select="'http://www.w3.org/2001/XMLSchema#'"/></xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:text><![CDATA[
]]></xsl:text>
<meta charset="utf-8"/>
<xsl:if test="dc:title[not(@refines)][1]">
<xsl:call-template name="copy-meta">
<xsl:with-param name="meta" select="dc:title[not(@refines)][1]"/>
<xsl:with-param name="rename" select="'title'"/>
</xsl:call-template>
</xsl:if>
<xsl:text><![CDATA[
]]></xsl:text>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<xsl:if test="opf:meta[not(@refines)][@property='dc:description.abstract']">
<xsl:call-template name="copy-meta">
<xsl:with-param name="meta" select="opf:meta[not(@refines)][@property='dc:description.abstract'][1]"/>
<xsl:with-param name="rename" select="'description'"/>
</xsl:call-template>
</xsl:if>
<xsl:for-each select="(*[not(@refines)] | comment()) except (dc:title[not(@refines)][1], opf:meta[not(@refines)][@property='dc:description.abstract'][1])">
<xsl:choose>
<xsl:when test="self::comment() and (not(preceding-sibling::*[not(@refines)]) or (preceding-sibling::text() intersect preceding-sibling::*[1]/following-sibling::text())[matches(.,'.*\n.*')])">
<xsl:text><![CDATA[
]]></xsl:text>
<xsl:copy-of select="."/>
</xsl:when>
<xsl:when test="self::*">
<xsl:variable name="name" select="(@property, name())[1]"/>
<xsl:variable name="name" select="if ($name = 'dc:contributor' and @opf:role) then concat('dc:contributor.',nlb:marcrel-to-role(@opf:role)) else $name"/>
<xsl:call-template name="copy-meta">
<xsl:with-param name="meta" select="."/>
<xsl:with-param name="rename" select="$name"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:text><![CDATA[
]]></xsl:text>
</head>
</xsl:for-each>
</xsl:template>
<xsl:template name="copy-meta">
<xsl:param name="meta" as="element()"/>
<xsl:param name="rename" as="xs:string?"/>
<xsl:variable name="name" select="if ($rename) then $rename else ($meta/@property, $meta/name())[1]"/>
<xsl:text><![CDATA[
]]></xsl:text>
<xsl:choose>
<xsl:when test="$name = 'title'">
<title>
<xsl:value-of select="$meta/normalize-space(text())"/>
</title>
</xsl:when>
<xsl:otherwise>
<meta name="{$name}" content="{$meta/normalize-space(text())}" />
</xsl:otherwise>
</xsl:choose>
<xsl:variable name="comment" select="(if ($meta/following-sibling::*) then $meta/following-sibling::comment() intersect $meta/following-sibling::*[1]/preceding-sibling::comment() else $meta/following-sibling::comment())[1]" as="comment()?"/>
<xsl:variable name="space" select="string-join($meta/following-sibling::text() intersect $comment/preceding-sibling::text(), '')" as="xs:string"/>
<xsl:if test="$comment and matches($space, '^ *$')">
<xsl:text> </xsl:text>
<xsl:copy-of select="$comment"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>