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

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

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

  • fixed ref lookup:

-- if a ref is declared, no namespace
-- if a ref is not declared add namespace

M toolkit/src/main/resources/toolkit/xslt/comp2schema.xsl

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