source: CMDI-Interoperability/CMD2RDF/trunk/xsl/CMDRecord2RDF.xsl @ 3817

Last change on this file since 3817 was 3817, checked in by mwindhouwer, 11 years ago

M xsl/CMDRecord2RDF.xsl

  • handles now

-- header field
-- resource (proxies)
-- resource (proxy) references (@ref)

  • makes the oa:Annotation

M data/general.ttl

  • added Resource and properties

M data/sample_organisation.ttl

  • fixed typo

M data/cmd-rdf/*

  • new output
File size: 11.2 KB
Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE rdf:RDF [
3    <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
4    <!ENTITY rdfs 'http://www.w3.org/TR/WD-rdf-schema#'>
5    <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#'>
6    <!ENTITY cmdm 'http://www.clarin.eu/cmd/general.rdf#'>
7    <!ENTITY oa 'http://www.w3.org/ns/oa#'>
8]>
9<xsl:stylesheet
10    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
11    xmlns:dcr="http://www.isocat.org/ns/dcr.rdf#"
12    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
13    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
14    xmlns:cmd="http://www.clarin.eu/cmd/"
15    xmlns:cmdm="http://www.clarin.eu/cmd/general.rdf"
16    xmlns:ore="http://www.openarchives.org/ore/terms/"
17    xmlns:oa="http://www.w3.org/ns/oa#"
18    xmlns:dcterms="http://purl.org/dc/terms/"
19>
20   
21    <xsl:output method="xml" encoding="UTF-8"/>
22   
23    <xsl:param name="base" select="base-uri()"/>
24   
25    <xsl:include href="CMD2RDF.xsl"/>
26   
27    <xsl:template match="text()"/>
28   
29    <!-- let's create some RDF -->
30    <xsl:template match="/cmd:CMD">
31        <rdf:RDF xml:base="{replace($base,'([./])(xml|cmdi)$','$1rdf')}">
32            <oa:Annotation rdf:about="#{generate-id()}">
33                <xsl:apply-templates select="cmd:Header"/>
34                <xsl:apply-templates select="cmd:Resources"/>
35                <oa:hasBody>
36                    <xsl:apply-templates select="cmd:Components">
37                        <xsl:with-param name="context" tunnel="yes" select="''"/>
38                    </xsl:apply-templates>
39                </oa:hasBody>
40                <oa:motivatedBy rdf:resource="&oa;describing"/>
41            </oa:Annotation>
42        </rdf:RDF>
43    </xsl:template>
44   
45    <xsl:template match="cmd:MdCreator">
46        <dcterms:creator>
47            <xsl:value-of select="."/>
48        </dcterms:creator>
49    </xsl:template>
50
51    <xsl:template match="cmd:MdCreationDate">
52        <dcterms:created>
53            <xsl:value-of select="."/>
54        </dcterms:created>
55    </xsl:template>
56   
57    <xsl:template match="cmd:MdSelfLink">
58        <dcterms:identifier>
59            <xsl:value-of select="."/>
60        </dcterms:identifier>
61    </xsl:template>
62   
63    <xsl:template match="cmd:MdProfile">
64        <!-- TODO? -->
65    </xsl:template>
66   
67    <xsl:template match="cmd:MdCollectionDisplayName">
68        <!-- TODO -->
69    </xsl:template>
70   
71    <xsl:template match="cmd:ResourceProxy">
72        <oa:hasTarget rdf:resource="{ResourceRef}"/>
73        <cmdm:Resource rdf:about="{ResourceRef}">
74            <cmdm:hasResourceType rdf:resource="&cmdm;{ResourceType}"/>
75            <xsl:if test="normalize-space(ResourceType/@mimetype)!=''">
76                <cmdm:hasMimeType>
77                    <xsl:value-of select="ResourceType/@mimetype"/>
78                </cmdm:hasMimeType>
79            </xsl:if>
80        </cmdm:Resource>
81    </xsl:template>
82   
83    <!-- the CMD body -->
84    <xsl:template match="/cmd:CMD/cmd:Components">
85        <!-- get the profile id -->
86        <xsl:variable name="id" select="/cmd:CMD/cmd:Header/cmd:MdProfile"/>
87        <!-- load the profile -->
88        <xsl:variable name="profile" select="cmd:profile($id)"/>
89        <!-- the base URL (the namespace in RDF/XML) of the RDF resources will change during traversal to the URL of the active profile/component -->
90        <xsl:variable name="ns" select="concat(cmd:ppath($id,'rdf'),'#')"/>
91        <!-- we traverse the profile/component scheme and find the matching instances -->
92        <xsl:apply-templates select="$profile/CMD_ComponentSpec/CMD_Component">
93            <xsl:with-param name="context" tunnel="yes" select="''"/>
94            <xsl:with-param name="ns" tunnel="yes" select="$ns"/>
95            <xsl:with-param name="instance" tunnel="yes" select="."/>
96        </xsl:apply-templates>
97    </xsl:template>
98   
99    <!-- a CMD component -->
100    <xsl:template match="CMD_Component">
101        <xsl:param name="context" tunnel="yes"/>
102        <xsl:param name="ns" tunnel="yes"/>
103        <xsl:param name="instance" tunnel="yes"/>
104        <!-- if the component has its own id its something that can be shared by multiple profiles/components and has its own base RDF (namespace) -->
105        <xsl:variable name="local-ns">
106            <xsl:choose>
107                <xsl:when test="exists(@ComponentId)">
108                    <xsl:sequence select="concat(cmd:cpath(@ComponentId,'rdf'),'#')"/>
109                </xsl:when>
110                <xsl:otherwise>
111                    <xsl:sequence select="$ns"/>
112                </xsl:otherwise>
113            </xsl:choose>
114        </xsl:variable>
115        <!-- if the component has its own id its something that can be shared by multiple profiles/components a new context is started -->
116        <xsl:variable name="local-context">
117            <xsl:choose>
118                <xsl:when test="exists(@ComponentId)">
119                    <xsl:sequence select="''"/>
120                </xsl:when>
121                <xsl:otherwise>
122                    <xsl:sequence select="$context"/>
123                </xsl:otherwise>
124            </xsl:choose>
125        </xsl:variable>
126        <xsl:variable name="profile" select="."/>
127        <xsl:variable name="name" select="@name"/>
128        <!-- extend the context with this component -->
129        <xsl:variable name="id" select="cmd:path($local-context,$name)"/>
130        <!-- find the matching instances -->
131        <xsl:for-each select="$instance/*[local-name()=$name]">
132            <!-- create a class -->
133            <xsl:variable name="class">
134                <xsl:element name="{$id}" namespace="{$local-ns}">
135                    <xsl:attribute name="rdf:about" select="concat('#',generate-id(.))"/>
136                    <xsl:for-each select="tokenize(@ref,'\s+')">
137                        <xsl:variable name="res"/>
138                        <cmdm:describesResource rdf:resource="/cmd:CMD/cmd:Resources/cmd:ResourceProxyList/cmd:ResourceProxy[@id=$ref]/ResourceRef"/>
139                    </xsl:for-each>
140                    <!-- switch back from the instance to the profile -->
141                    <xsl:apply-templates select="$profile/*">
142                        <xsl:with-param name="context" tunnel="yes" select="$id"/>
143                        <xsl:with-param name="ns" tunnel="yes" select="$local-ns"/>
144                        <xsl:with-param name="instance" tunnel="yes" select="."/>
145                    </xsl:apply-templates>
146                </xsl:element>
147            </xsl:variable>
148            <!-- is this the root or not -->
149            <xsl:choose>
150                <xsl:when test="exists(parent::cmd:Components)">
151                    <xsl:copy-of select="$class"/>
152                </xsl:when>
153                <xsl:otherwise>
154                    <!-- use the generic RELcat related relationship for the nesting of components -->
155                    <cmdm:contains>
156                        <xsl:copy-of select="$class"/>
157                    </cmdm:contains>
158                </xsl:otherwise>
159            </xsl:choose>
160        </xsl:for-each>
161    </xsl:template>
162
163    <!-- a CMD element --> 
164    <xsl:template match="CMD_Element">
165        <xsl:param name="context" tunnel="yes"/>
166        <xsl:param name="ns" tunnel="yes"/>
167        <xsl:param name="instance" tunnel="yes"/>
168        <xsl:variable name="profile" select="."/>
169        <xsl:variable name="name" select="@name"/>
170        <!-- extend the context with this element -->
171        <xsl:variable name="id" select="cmd:path($context,$name)"/>
172        <xsl:variable name="has" select="cmd:path($context,concat('has',@name))"/>
173        <!-- find the matching instances -->
174        <xsl:for-each select="$instance/*[local-name()=$name]">
175            <!-- use the generic RELcat related relationship for the nesting of the element in its component -->
176            <cmdm:contains>
177                <!-- a CMD element is a RDF class -->
178                <xsl:element name="{$id}" namespace="{$ns}">
179                    <xsl:attribute name="rdf:about" select="concat('#',generate-id(.))"/>
180                    <!-- the value is assigned to a propery of the CMD element RDF class -->
181                    <xsl:element name="{$has}ElementValue" namespace="{$ns}">
182                        <!-- map the CMD XSD datatype to a datatype supported by RDF -->
183                        <xsl:attribute name="rdf:datatype" select="concat('&xsd;',cmd:datatype($profile/@ValueScheme))"/>
184                        <!-- copy @xml:lang -->
185                        <xsl:copy-of select="@xml:lang"/>
186                        <!-- copy the literal -->
187                        <xsl:value-of select="."/>
188                    </xsl:element>
189                    <!-- if there is enum we also have an entity property -->
190                    <xsl:if test="exists($profile/ValueScheme/enumeration)">
191                        <xsl:element name="{$id}ElementEntity" namespace="{$ns}">
192                            <xsl:attribute name="rdf:resource" select="concat($ns,$id,'ValueScheme',$STEP,replace(.,'\s',''))"/>
193                        </xsl:element>
194                    </xsl:if>
195                    <!-- switch back from the instance to the profile to handle the attributes -->
196                    <xsl:apply-templates select="$profile/AttributeList/Attribute">
197                        <xsl:with-param name="context" tunnel="yes" select="$id"/>
198                        <xsl:with-param name="ns" tunnel="yes" select="$ns"/>
199                        <xsl:with-param name="instance" tunnel="yes" select="."/>
200                    </xsl:apply-templates>
201                </xsl:element>
202            </cmdm:contains>
203        </xsl:for-each>
204    </xsl:template>
205   
206    <!-- a CMD attribute -->
207    <xsl:template match="Attribute">
208        <xsl:param name="context" tunnel="yes"/>
209        <xsl:param name="ns" tunnel="yes"/>
210        <xsl:param name="instance" tunnel="yes"/>
211        <xsl:variable name="profile" select="."/>
212        <xsl:variable name="name" select="Name"/>
213        <!-- extend the context with this attribute -->
214        <xsl:variable name="id" select="concat(cmd:path($context,$name),'Attribute')"/>
215        <xsl:variable name="has" select="concat(cmd:path($context,concat('has',$name)),'Attribute')"/>
216        <!-- find the matching instances -->
217        <xsl:for-each select="$instance/@*[local-name()=$name]">
218            <cmdm:containsAttribute>
219                <xsl:element name="{$id}" namespace="{$ns}">
220                    <xsl:attribute name="rdf:about" select="concat('#',generate-id(.))"/>
221                    <xsl:element name="{$has}Value" namespace="{$ns}">
222                        <!-- map the CMD XSD datatype to a datatype supported by RDF -->
223                        <xsl:attribute name="rdf:datatype" select="concat('&xsd;',cmd:datatype($profile/Type))"/>
224                        <!-- copy the literal -->
225                        <xsl:value-of select="."/>
226                    </xsl:element>
227                    <xsl:if test="exists($profile/ValueScheme/enumeration)">
228                        <xsl:element name="{$has}Entity" namespace="{$ns}">
229                            <xsl:attribute name="rdf:resource" select="concat($ns,$id,'ValueScheme',$STEP,replace(.,'\s',''))"/>
230                        </xsl:element>
231                    </xsl:if>
232                </xsl:element>
233            </cmdm:containsAttribute>
234        </xsl:for-each>
235    </xsl:template>
236
237</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.