source: metadata/trunk/toolkit/src/main/resources/toolkit/upgrade/cmd-record-1_1-to-1_2.xsl @ 6908

Last change on this file since 6908 was 6908, checked in by Menzo Windhouwer, 9 years ago

M toolkit/src/main/resources/toolkit/xsd/cmd-component.xsd
M toolkit/src/main/resources/toolkit/sch/cmd-component-best-practices.sch

  • moved a_nest Schematron rule to best practices

M toolkit/src/main/resources/toolkit/upgrade/cmd-record-1_1-to-1_2.xsl

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