This repository was archived by the owner on Oct 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup-opf-metadata.xsl
More file actions
154 lines (131 loc) · 8.3 KB
/
group-opf-metadata.xsl
File metadata and controls
154 lines (131 loc) · 8.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?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:dc="http://purl.org/dc/elements/1.1/"
xmlns:opf="http://www.idpf.org/2007/opf"
exclude-result-prefixes="#all"
version="2.0">
<xsl:output indent="no" omit-xml-declaration="yes"/>
<xsl:param name="include-namespaces" select="''" as="xs:string"/>
<xsl:param name="include-other-identifiers-placeholder" select="false()" as="xs:boolean"/>
<xsl:template match="@* | node()" mode="#all">
<xsl:copy exclude-result-prefixes="#all">
<xsl:apply-templates select="@* | node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/text()"/>
<xsl:template match="opf:metadata">
<xsl:variable name="metadata" select="." as="element()"/>
<xsl:variable name="creativeWorkProperties" select="('dc:title', 'dc:creator', 'dc:language', 'dc:contributor', 'responsibilityStatement', 'dc:subject', 'dc:type.genre', 'dc:type.fiction', 'dc:type.literaryForm',
'series.issn', 'series.position', 'periodical', 'periodicity', 'magazine', 'newspaper',
'audience', 'typicalAgeRange', 'exclude-from-recommendations',
.//*[starts-with(@name,'dc:title.') and not(ends-with(@name, '.part') or contains(@name, '.part.'))]/string(@name),
.//*[starts-with(@name,'dc:contributor.') and not(@name='dc:contributor.narrator')]/string(@name),
.//*[starts-with(@name,'dc:subject.')]/string(@name))"/>
<xsl:variable name="format" select="(dc:format[not(@refines)])[1]/text()" as="xs:string?"/>
<xsl:text> </xsl:text>
<xsl:copy exclude-result-prefixes="#all">
<!-- Set namespaces on the metadata element -->
<xsl:namespace name="dc" select="'http://purl.org/dc/elements/1.1/'"/>
<xsl:for-each select="tokenize($include-namespaces, '\s+')[position() mod 2 = 1]">
<xsl:variable name="prefix" select="replace(., ':', '')"/>
<xsl:variable name="uri" select="replace($include-namespaces, concat('(^|.*\s)', $prefix ,':\s*([^\s]+)(\s.*|$)'), '$2')"/>
<xsl:namespace name="{$prefix}" select="$uri"/>
</xsl:for-each>
<xsl:variable name="explicit-prefixes" select="$include-namespaces"/>
<xsl:for-each select="//opf:meta">
<xsl:if test="starts-with(@property, ':')">
<xsl:variable name="prefix" select="substring-before(@property, ':')" as="xs:string"/>
<xsl:if test="concat($prefix, ':') = tokenize($include-namespaces, '\s+')[position() mod 2 = 1]">
<xsl:variable name="namespace-uri" select="namespace-uri-for-prefix($prefix, .)"/>
<xsl:if test="$namespace-uri">
<xsl:namespace name="{$prefix}" select="$namespace-uri"/>
</xsl:if>
</xsl:if>
</xsl:if>
</xsl:for-each>
<xsl:copy-of select="@*" exclude-result-prefixes="#all"/>
<!-- edition identifier (dc:identifier) before everything else -->
<xsl:call-template name="comment-line">
<xsl:with-param name="text" select="concat('Boknummer for ', if ($format) then concat($format, '-') else '', 'utgaven')"/>
</xsl:call-template>
<xsl:for-each select="dc:identifier[not(@refines)]">
<xsl:call-template name="copy-element">
<xsl:with-param name="metadata" select="$metadata"/>
<xsl:with-param name="element" select="."/>
</xsl:call-template>
</xsl:for-each>
<xsl:call-template name="newline"/>
<!-- reference to other editions -->
<xsl:if test="$include-other-identifiers-placeholder">
<xsl:call-template name="comment-line">
<xsl:with-param name="text" select="'Boknummer for andre utgaver'"/>
</xsl:call-template>
<xsl:call-template name="comment-line">
<xsl:with-param name="text" select="'REPLACEME_OTHER_IDENTIFIERS'"/> <!-- to be replaced in postprocessing with python which will make an API call to get the other idendifiers -->
</xsl:call-template>
<xsl:call-template name="newline"/>
</xsl:if>
<!-- creative work metadata -->
<xsl:call-template name="comment-line">
<xsl:with-param name="text" select="'Metadata for åndsverket'"/>
</xsl:call-template>
<xsl:for-each select="$creativeWorkProperties">
<xsl:variable name="property" select="." as="xs:string"/>
<xsl:variable name="elements" select="$metadata/*[not(@refines) and $property = (name(), @property, tokenize(@property, ':')[last()])]" as="element()*"/>
<xsl:for-each select="$elements">
<xsl:call-template name="copy-element">
<xsl:with-param name="metadata" select="$metadata"/>
<xsl:with-param name="element" select="."/>
</xsl:call-template>
</xsl:for-each>
</xsl:for-each>
<xsl:call-template name="newline"/>
<!-- edition metadata -->
<xsl:call-template name="comment-line">
<xsl:with-param name="text" select="concat('Metadata for ', if ($format) then concat($format, '-') else '', 'utgaven')"/>
</xsl:call-template>
<xsl:for-each select="$metadata/*[not(@refines)]">
<xsl:variable name="property" select="(@property, name())[1]" as="xs:string"/>
<xsl:if test="not(($property, tokenize($property, ':')[last()]) = $creativeWorkProperties) and not($property = 'dc:identifier')">
<xsl:call-template name="copy-element">
<xsl:with-param name="metadata" select="$metadata"/>
<xsl:with-param name="element" select="."/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
<xsl:call-template name="newline"/>
<xsl:text>
</xsl:text>
<xsl:text>
</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template name="comment-line">
<xsl:param name="text" as="xs:string"/>
<xsl:call-template name="newline"/>
<xsl:comment select="concat(' ', $text, ' ')"/>
</xsl:template>
<xsl:template name="newline">
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template name="copy-element">
<xsl:param name="metadata" as="element()"/>
<xsl:param name="element" as="element()"/>
<xsl:call-template name="newline"/>
<xsl:copy-of select="$element" exclude-result-prefixes="#all"/>
<xsl:variable name="trailing-space" select="$element/(following-sibling::node()[1] intersect following-sibling::text()[1])" as="text()?"/>
<xsl:variable name="trailing-comment" select="($element | $trailing-space)/(following-sibling::node()[1] intersect following-sibling::comment()[1])[1]" as="comment()?"/>
<xsl:if test="count($trailing-comment) and $trailing-space/matches(., ' +')">
<xsl:copy-of select="$trailing-space" exclude-result-prefixes="#all"/>
<xsl:copy-of select="$trailing-comment" exclude-result-prefixes="#all"/>
</xsl:if>
<xsl:for-each select="$metadata/*[@refines != '' and @refines = $element/concat('#', @id)]">
<xsl:call-template name="copy-element">
<xsl:with-param name="metadata" select="$metadata"/>
<xsl:with-param name="element" select="."/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>