1 | <?xml version="1.0" encoding="UTF-8"?> |
---|
2 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
---|
3 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
---|
4 | xmlns:xs="http://www.w3.org/2001/XMLSchema" |
---|
5 | xmlns:cmd0="http://www.clarin.eu/cmd/" |
---|
6 | xmlns:cmd="http://www.clarin.eu/cmd/1" |
---|
7 | exclude-result-prefixes="xs cmd0" |
---|
8 | version="2.0"> |
---|
9 | |
---|
10 | <xsl:param name="cmd-toolkit" select="'../../../../../main/resources/toolkit'"/> |
---|
11 | <xsl:param name="cmd-envelop-xsd" select="concat($cmd-toolkit,'/xsd/cmd-envelop.xsd')"/> |
---|
12 | <xsl:param name="cmd-uri" select="'http://www.clarin.eu/cmd/1'"/> |
---|
13 | <xsl:param name="cmd-profile" select="()"/> |
---|
14 | <xsl:param name="cr-uri" select="'..'"/> |
---|
15 | <xsl:param name="cr-extension-xsd" select="'-1_2.xsd'"/> |
---|
16 | <xsl:param name="cr-roundtrip-extension-xsd" select="'-1-1-1_2.xsd'"/> |
---|
17 | <xsl:param name="cr-extension-xml" select="'-1.2.xml'"/> |
---|
18 | |
---|
19 | <xsl:param name="escape" select="'ccmmddii_'"/> |
---|
20 | |
---|
21 | <xsl:variable name="cmd-components" select="concat($cmd-uri,'/components')"/> |
---|
22 | <xsl:variable name="cmd-profiles" select="concat($cmd-uri,'/profiles')"/> |
---|
23 | <xsl:variable name="cr-profiles" select="concat($cr-uri,'/profiles')"/> |
---|
24 | |
---|
25 | <!-- identity copy --> |
---|
26 | <xsl:template match="@*|node()"> |
---|
27 | <xsl:copy> |
---|
28 | <xsl:apply-templates select="@*|node()"/> |
---|
29 | </xsl:copy> |
---|
30 | </xsl:template> |
---|
31 | |
---|
32 | <!-- try to determine the profile --> |
---|
33 | <xsl:variable name="profile"> |
---|
34 | <xsl:variable name="header" select="/cmd0:CMD/cmd0:Header/cmd0:MdProfile/replace(.,'.*(clarin.eu:cr1:p_[0-9]+).*','$1')"/> |
---|
35 | <xsl:variable name="schema" select="/cmd0:CMD/(@xsi:schemaLocation|@xsi:noNamespaceSchemaLocation)/replace(.,'.*(clarin.eu:cr1:p_[0-9]+).*','$1')"/> |
---|
36 | <xsl:if test="count($header) gt 1"> |
---|
37 | <xsl:message>WRN: found more then one profile ID (<xsl:value-of select="string-join($header,',')"/>) in a cmd:MdProfile, will use the first one! </xsl:message> |
---|
38 | </xsl:if> |
---|
39 | <xsl:if test="count($schema) gt 1"> |
---|
40 | <xsl:message>WRN: found more then one profile ID (<xsl:value-of select="string-join($schema,',')"/>) in a xsi:schemaLocation, will use the first one! </xsl:message> |
---|
41 | </xsl:if> |
---|
42 | <xsl:choose> |
---|
43 | <xsl:when test="exists($header) and exists($schema)"> |
---|
44 | <xsl:if test="($header)[1] ne ($schema)[1]"> |
---|
45 | <xsl:message>WRN: the profile IDs found in cmd:MdProfile (<xsl:value-of select="($header)[1]"/>) and xsi:schemaLocation (<xsl:value-of select="($schema)[1]"/>), don't agree, will use the xsi:schemaLocation!</xsl:message> |
---|
46 | </xsl:if> |
---|
47 | <xsl:value-of select="($schema)[1]"/> |
---|
48 | </xsl:when> |
---|
49 | <xsl:when test="exists($header) and empty($schema)"> |
---|
50 | <xsl:value-of select="($header)[1]"/> |
---|
51 | </xsl:when> |
---|
52 | <xsl:when test="empty($header) and exists($schema)"> |
---|
53 | <xsl:value-of select="($schema)[1]"/> |
---|
54 | </xsl:when> |
---|
55 | <xsl:otherwise> |
---|
56 | <xsl:message terminate="yes">ERR: the profile ID can't be determined!</xsl:message> |
---|
57 | </xsl:otherwise> |
---|
58 | </xsl:choose> |
---|
59 | </xsl:variable> |
---|
60 | |
---|
61 | <!-- the profile specific uris --> |
---|
62 | <xsl:variable name="cmd-profile-uri" select="concat($cmd-profiles,'/',$profile)"/> |
---|
63 | <xsl:variable name="cr-profile-xml" select="concat($cr-profiles,'/',$profile,$cr-extension-xml)"/> |
---|
64 | <xsl:variable name="cr-profile-xsd"> |
---|
65 | <xsl:variable name="prof" select="if (exists($cmd-profile)) then ($cmd-profile) else (doc(resolve-uri($cr-profile-xml,base-uri())))"/> |
---|
66 | <xsl:choose> |
---|
67 | <!-- '' means there was no @CMDOriginalVersion, so the original version is 1.2 (the default) --> |
---|
68 | <xsl:when test="$prof/ComponentSpec/normalize-space(@CMDOriginalVersion)=('','1.2')"> |
---|
69 | <xsl:value-of select="concat($cr-profiles,'/',$profile,$cr-roundtrip-extension-xsd)"/> |
---|
70 | </xsl:when> |
---|
71 | <xsl:otherwise> |
---|
72 | <xsl:value-of select="concat($cr-profiles,'/',$profile,$cr-extension-xsd)"/> |
---|
73 | </xsl:otherwise> |
---|
74 | </xsl:choose> |
---|
75 | </xsl:variable> |
---|
76 | |
---|
77 | <!-- CMD version becomes 1.2 --> |
---|
78 | <xsl:template match="/cmd0:CMD/@CMDVersion"> |
---|
79 | <xsl:attribute name="CMDVersion" select="'1.2'"/> |
---|
80 | </xsl:template> |
---|
81 | |
---|
82 | <!-- Create our own xsi:schemaLocation --> |
---|
83 | <xsl:template match="@xsi:schemaLocation"/> |
---|
84 | |
---|
85 | <xsl:template match="@xsi:noNamespaceSchemaLocation"/> |
---|
86 | |
---|
87 | <xsl:template match="cmd0:CMD"> |
---|
88 | <cmd:CMD> |
---|
89 | <xsl:namespace name="cmd" select="'http://www.clarin.eu/cmd/1'"/> |
---|
90 | <xsl:namespace name="cmdp" select="$cmd-profile-uri"/> |
---|
91 | <xsl:apply-templates select="@* except (@xsi:schemaLocation|@xsi:noNamespaceSchemaLocation)"/> |
---|
92 | <xsl:attribute name="xsi:schemaLocation"> |
---|
93 | <xsl:value-of select="$cmd-uri"/> |
---|
94 | <xsl:text> </xsl:text> |
---|
95 | <xsl:value-of select="$cmd-envelop-xsd"/> |
---|
96 | <xsl:text> </xsl:text> |
---|
97 | <xsl:value-of select="$cmd-profile-uri"/> |
---|
98 | <xsl:text> </xsl:text> |
---|
99 | <xsl:value-of select="$cr-profile-xsd"/> |
---|
100 | </xsl:attribute> |
---|
101 | <xsl:apply-templates select="cmd0:Header"/> |
---|
102 | <xsl:apply-templates select="cmd0:Resources"/> |
---|
103 | <xsl:apply-templates select="cmd0:Resources/cmd0:IsPartOfList"/> |
---|
104 | <xsl:apply-templates select="cmd0:Components"/> |
---|
105 | </cmd:CMD> |
---|
106 | </xsl:template> |
---|
107 | |
---|
108 | <!-- Make sure cmd:Header contains cmd:MdProfile --> |
---|
109 | <xsl:template match="cmd0:Header"> |
---|
110 | <cmd:Header> |
---|
111 | <xsl:apply-templates select="cmd0:MdCreator"/> |
---|
112 | <xsl:apply-templates select="cmd0:MdCreationDate"/> |
---|
113 | <xsl:apply-templates select="cmd0:MdSelfLink"/> |
---|
114 | <cmd:MdProfile> |
---|
115 | <xsl:value-of select="$profile"/> |
---|
116 | </cmd:MdProfile> |
---|
117 | <xsl:apply-templates select="cmd0:MdCollectionDisplayName"/> |
---|
118 | </cmd:Header> |
---|
119 | </xsl:template> |
---|
120 | |
---|
121 | <!-- Skip cmd:Resources/cmd:IsPartOfList --> |
---|
122 | <xsl:template match="cmd0:Resources"> |
---|
123 | <cmd:Resources> |
---|
124 | <xsl:apply-templates select="cmd0:ResourceProxyList"/> |
---|
125 | <xsl:apply-templates select="cmd0:JournalFileProxyList"/> |
---|
126 | <xsl:apply-templates select="cmd0:ResourceRelationList"/> |
---|
127 | </cmd:Resources> |
---|
128 | </xsl:template> |
---|
129 | |
---|
130 | <!-- Reshape ResourceRelationList --> |
---|
131 | <xsl:template match="cmd0:ResourceRelation/cmd0:RelationType"> |
---|
132 | <cmd:RelationType> |
---|
133 | <!-- take the string value, ignore deeper structure --> |
---|
134 | <xsl:value-of select="."/> |
---|
135 | </cmd:RelationType> |
---|
136 | </xsl:template> |
---|
137 | |
---|
138 | <xsl:template match="cmd0:ResourceRelation/cmd0:res1"> |
---|
139 | <cmd:Resource> |
---|
140 | <xsl:apply-templates select="@*"/> |
---|
141 | </cmd:Resource> |
---|
142 | </xsl:template> |
---|
143 | |
---|
144 | <xsl:template match="cmd0:ResourceRelation/cmd0:res2"> |
---|
145 | <cmd:Resource> |
---|
146 | <xsl:apply-templates select="@*"/> |
---|
147 | </cmd:Resource> |
---|
148 | </xsl:template> |
---|
149 | |
---|
150 | <!-- put envelop in the envelop namespace --> |
---|
151 | <xsl:template match="/cmd0:CMD//*" priority="1"> |
---|
152 | <xsl:element name="cmd:{local-name()}"> |
---|
153 | <xsl:apply-templates select="@*|node()"/> |
---|
154 | </xsl:element> |
---|
155 | </xsl:template> |
---|
156 | |
---|
157 | <!-- put payload in the profile namespace --> |
---|
158 | <xsl:template match="cmd0:Components//*" priority="2"> |
---|
159 | <xsl:element namespace="{$cmd-profile-uri}" name="cmdp:{local-name()}"> |
---|
160 | <xsl:apply-templates select="@*|node()"/> |
---|
161 | </xsl:element> |
---|
162 | </xsl:template> |
---|
163 | |
---|
164 | <!-- unescape downgraded CMDI 1.2 attributes --> |
---|
165 | <xsl:template match="cmd0:Components//@*[name()=local-name()][starts-with(name(),$escape)]"> |
---|
166 | <xsl:attribute name="{substring-after(name(),$escape)}" select="."/> |
---|
167 | </xsl:template> |
---|
168 | |
---|
169 | <!-- move CMD attributes to the CMD namespace --> |
---|
170 | <xsl:template match="cmd0:Components//@ref" priority="2"> |
---|
171 | <xsl:choose> |
---|
172 | <xsl:when test="exists(parent::*/text()[normalize-space()!=''])"> |
---|
173 | <!-- this is an element keep the @ref --> |
---|
174 | <!--<xsl:message>INF: this is an element keep the ref</xsl:message>--> |
---|
175 | <xsl:copy/> |
---|
176 | </xsl:when> |
---|
177 | <xsl:when test="exists(../node()) or exists(parent::*/@ComponentId)"> |
---|
178 | <!-- the parent is a component add the namespace to @ref --> |
---|
179 | <!--<xsl:message>INF: this is an component add the namespace to ref</xsl:message>--> |
---|
180 | <xsl:attribute name="cmd:ref"> |
---|
181 | <xsl:variable name="refs" select="tokenize(.,'\s+')"/> |
---|
182 | <xsl:if test="count($refs) gt 1"> |
---|
183 | <xsl:message>WRN: CMDI 1.2 doesn't support references to multiple ResourceProxies! Only the first reference is kept.</xsl:message> |
---|
184 | </xsl:if> |
---|
185 | <xsl:value-of select="$refs[1]"/> |
---|
186 | </xsl:attribute> |
---|
187 | </xsl:when> |
---|
188 | <xsl:otherwise> |
---|
189 | <!-- don't know if the parent is a component without children, or an element without value |
---|
190 | have a look at the profile --> |
---|
191 | <xsl:variable name="prof" select="if (exists($cmd-profile)) then ($cmd-profile) else (doc(resolve-uri($cr-profile-xml,base-uri())))"/> |
---|
192 | <xsl:variable name="path" select="ancestor::*[. >> /cmd0:CMD/cmd0:Components]"/> |
---|
193 | <xsl:variable name="attr" select="$prof//Attribute[@name='ref' or Name='ref'][string-join(ancestor::*[local-name()=('Component','CMD_Component','Element','CMD_Element')]/@name,'/')=string-join($path/local-name(),'/')]"/> |
---|
194 | <xsl:choose> |
---|
195 | <xsl:when test="exists($attr)"> |
---|
196 | <!-- in CMDI 1.1 @ref can only be an user declared attribute for an element --> |
---|
197 | <!--<xsl:message>INF: according to the profile this @ref is an user declared attribute, so keep the ref</xsl:message>--> |
---|
198 | <xsl:copy/> |
---|
199 | </xsl:when> |
---|
200 | <xsl:otherwise> |
---|
201 | <!-- this is an undeclared @ref, so add the namespace --> |
---|
202 | <!--<xsl:message>INF: according to the profile this @ref is not user defined, so add the namespace</xsl:message>--> |
---|
203 | <xsl:attribute name="cmd:ref"> |
---|
204 | <xsl:variable name="refs" select="tokenize(.,'\s+')"/> |
---|
205 | <xsl:if test="count($refs) gt 1"> |
---|
206 | <xsl:message>WRN: CMDI 1.2 doesn't support references to multiple ResourceProxies! Only the first reference is kept.</xsl:message> |
---|
207 | </xsl:if> |
---|
208 | <xsl:value-of select="$refs[1]"/> |
---|
209 | </xsl:attribute> |
---|
210 | </xsl:otherwise> |
---|
211 | </xsl:choose> |
---|
212 | </xsl:otherwise> |
---|
213 | </xsl:choose> |
---|
214 | </xsl:template> |
---|
215 | |
---|
216 | <xsl:template match="cmd0:Components//@ComponentId" priority="2"> |
---|
217 | <xsl:attribute name="cmd:ComponentId" select="."/> |
---|
218 | </xsl:template> |
---|
219 | |
---|
220 | </xsl:stylesheet> |
---|