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

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

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

  • unescaped escaped CMDI 1.2 attributes
  • warn when a @ref contains multiple resource proxy references, and use only the first one

M toolkit/src/main/resources/toolkit/downgrade/cmd-component-1_2-to-1_1.xsl

  • escape attributes that would clash with CMDI 1.1 reserved attributes

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

  • moved Schematron rules which are validate best practices

M toolkit/src/test/java/eu/clarin/cmd/toolkit/TestCMDToolkit.java

  • removed obsolete commented out code
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    <xsl:variable name="cr-profile-xsd" select="concat($cr-profiles,'/',$profile,$cr-extension-xsd)"/>
62    <xsl:variable name="cr-profile-xml" select="concat($cr-profiles,'/',$profile,$cr-extension-xml)"/>
63   
64    <!-- CMD version becomes 1.2 -->
65    <xsl:template match="/cmd0:CMD/@CMDVersion">
66        <xsl:attribute name="CMDVersion" select="'1.2'"/>
67    </xsl:template>
68   
69    <!-- Create our own xsi:schemaLocation -->
70    <xsl:template match="@xsi:schemaLocation"/>
71   
72    <xsl:template match="@xsi:noNamespaceSchemaLocation"/>
73   
74    <xsl:template match="cmd0:CMD">
75        <cmd:CMD>
76            <xsl:namespace name="cmd" select="'http://www.clarin.eu/cmd/1'"/>
77            <xsl:namespace name="cmdp" select="$cmd-profile-uri"/>
78            <xsl:apply-templates select="@* except (@xsi:schemaLocation|@xsi:noNamespaceSchemaLocation)"/>
79            <xsl:attribute name="xsi:schemaLocation">
80                <xsl:value-of select="$cmd-uri"/>
81                <xsl:text> </xsl:text>
82                <xsl:value-of select="$cmd-envelop-xsd"/>
83                <xsl:text> </xsl:text>
84                <xsl:value-of select="$cmd-profile-uri"/>
85                <xsl:text> </xsl:text>
86                <xsl:value-of select="$cr-profile-xsd"/>
87            </xsl:attribute>
88            <xsl:apply-templates select="cmd0:Header"/>
89            <xsl:apply-templates select="cmd0:Resources"/>
90            <xsl:apply-templates select="cmd0:Resources/cmd0:IsPartOfList"/>
91            <xsl:apply-templates select="cmd0:Components"/>
92        </cmd:CMD>
93    </xsl:template>
94   
95    <!-- Make sure cmd:Header contains cmd:MdProfile -->
96    <xsl:template match="cmd0:Header">
97        <cmd:Header>
98            <xsl:apply-templates select="cmd0:MdCreator"/>
99            <xsl:apply-templates select="cmd0:MdCreationDate"/>
100            <xsl:apply-templates select="cmd0:MdSelfLink"/>
101            <cmd:MdProfile>
102                <xsl:value-of select="$profile"/>
103            </cmd:MdProfile>
104            <xsl:apply-templates select="cmd0:MdCollectionDisplayName"/>
105        </cmd:Header>
106    </xsl:template>
107   
108    <!-- Skip cmd:Resources/cmd:IsPartOfList -->
109    <xsl:template match="cmd0:Resources">
110        <cmd:Resources>
111            <xsl:apply-templates select="cmd0:ResourceProxyList"/>
112            <xsl:apply-templates select="cmd0:JournalFileProxyList"/>
113            <xsl:apply-templates select="cmd0:ResourceRelationList"/>
114        </cmd:Resources>
115    </xsl:template>
116   
117    <!-- Reshape ResourceRelationList -->
118    <xsl:template match="cmd0:ResourceRelation/cmd0:RelationType">
119        <cmd:RelationType>
120            <!-- take the string value, ignore deeper structure -->
121            <xsl:value-of select="."/>
122        </cmd:RelationType>
123    </xsl:template>
124   
125    <xsl:template match="cmd0:ResourceRelation/cmd0:res1">
126        <cmd:Resource>
127            <xsl:apply-templates select="@*"/>
128        </cmd:Resource>
129    </xsl:template>
130   
131    <xsl:template match="cmd0:ResourceRelation/cmd0:res2">
132        <cmd:Resource>
133            <xsl:apply-templates select="@*"/>
134        </cmd:Resource>
135    </xsl:template>
136   
137    <!-- put envelop in the envelop namespace -->
138    <xsl:template match="/cmd0:CMD//*" priority="1">
139        <xsl:element name="cmd:{local-name()}">
140            <xsl:apply-templates select="@*|node()"/>
141        </xsl:element>
142    </xsl:template>
143   
144    <!-- put payload in the profile namespace -->
145    <xsl:template match="cmd0:Components//*" priority="2">
146        <xsl:element namespace="{$cmd-profile-uri}" name="cmdp:{local-name()}">
147            <xsl:apply-templates select="@*|node()"/>
148        </xsl:element>
149    </xsl:template>
150   
151    <!-- unescape downgraded CMDI 1.2 attributes -->
152    <xsl:template match="cmd0:Components//@*[name()=local-name()][starts-with(name(),$escape)]">
153        <xsl:attribute name="{substring-after(name(),$escape)}" select="."/>
154    </xsl:template>
155   
156    <!-- move CMD attributes to the CMD namespace -->
157    <xsl:template match="cmd0:Components//@ref" priority="2">
158        <xsl:choose>
159            <xsl:when test="exists(parent::*/text()[normalize-space()!=''])">
160                <!-- this is an element keep the @ref -->
161                <!--<xsl:message>INF: this is an element keep the ref</xsl:message>-->
162                <xsl:copy/>
163            </xsl:when>
164            <xsl:when test="exists(../node()) or exists(parent::*/@ComponentId)">
165                <!-- the parent is a component add the namespace to @ref -->
166                <!--<xsl:message>INF: this is an component add the namespace to ref</xsl:message>-->
167                <xsl:attribute name="cmd:ref">
168                    <xsl:variable name="refs" select="tokenize(.,'\s+')"/>
169                    <xsl:if test="count($refs) gt 1">
170                        <xsl:message>WRN: CMDI 1.2 doesn't support references to multiple ResourceProxies! Only the first reference is kept.</xsl:message>
171                    </xsl:if>
172                    <xsl:value-of select="$refs[1]"/>
173                </xsl:attribute>
174            </xsl:when>
175            <xsl:otherwise>
176                <!-- don't know if the parent is a component without children, or an element without value
177                     have a look at the profile -->
178                <xsl:variable name="prof" select="doc(resolve-uri($cr-profile-xml,base-uri()))"/>
179                <xsl:variable name="path" select="ancestor::*[. >> /cmd0:CMD/cmd0:Components]"/>
180                <xsl:variable name="attr" select="$prof//Attribute[Name='ref'][string-join(ancestor::*[local-name()=('CMD_Component','CMD_Element')]/@name,'/')=string-join($path/local-name(),'/')]"/>
181                <xsl:choose>
182                    <xsl:when test="exists($attr)">
183                        <!-- in CMDI 1.1 @ref can only be an user declared attribute for an element -->
184                        <!--<xsl:message>INF: according to the profile this @ref is an user declared attribute, so keep the ref</xsl:message>-->
185                        <xsl:copy/>
186                    </xsl:when>
187                    <xsl:otherwise>
188                        <!-- this is an undeclared @ref, so add the namespace -->
189                        <!--<xsl:message>INF: according to the profile this @ref is not user defined, so add the namespace</xsl:message>-->
190                        <xsl:attribute name="cmd:ref">
191                            <xsl:variable name="refs" select="tokenize(.,'\s+')"/>
192                            <xsl:if test="count($refs) gt 1">
193                                <xsl:message>WRN: CMDI 1.2 doesn't support references to multiple ResourceProxies! Only the first reference is kept.</xsl:message>
194                            </xsl:if>
195                            <xsl:value-of select="$refs[1]"/>
196                        </xsl:attribute>
197                    </xsl:otherwise>
198                </xsl:choose>
199            </xsl:otherwise>
200        </xsl:choose>
201    </xsl:template>
202   
203    <xsl:template match="cmd0:Components//@ComponentId" priority="2">
204        <xsl:attribute name="cmd:ComponentId" select="."/>
205    </xsl:template>
206
207</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.