1 | <?xml version="1.0" encoding="UTF-8"?> |
---|
2 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" |
---|
3 | xmlns:dcif="http://www.isocat.org/ns/dcif" xmlns:f="http://www.isocat.org/" |
---|
4 | xmlns:xs="http://www.w3.org/2001/XMLSchema" |
---|
5 | xmlns:functx="http://www.functx.com"> |
---|
6 | |
---|
7 | <xsl:output method="text" encoding="utf-8"/> |
---|
8 | |
---|
9 | <xsl:variable name="NL" select="system-property('line.separator')"/> |
---|
10 | |
---|
11 | <xsl:variable name="lines" select="/csvFile"/> |
---|
12 | |
---|
13 | <xsl:template match="text()"/> |
---|
14 | |
---|
15 | <!-- functx --> |
---|
16 | <xsl:function name="functx:capitalize-first" as="xs:string?"> |
---|
17 | <xsl:param name="arg" as="xs:string?"/> |
---|
18 | <xsl:sequence select=" |
---|
19 | concat(upper-case(substring($arg,1,1)), |
---|
20 | substring($arg,2))"/> |
---|
21 | </xsl:function> |
---|
22 | |
---|
23 | <xsl:function name="functx:words-to-camel-case" as="xs:string"> |
---|
24 | <xsl:param name="arg" as="xs:string?"/> |
---|
25 | <xsl:sequence select=" |
---|
26 | string-join((tokenize($arg,'\s+')[1], |
---|
27 | for $word in tokenize($arg,'\s+')[position() > 1] |
---|
28 | return functx:capitalize-first($word)) |
---|
29 | ,'')"/> |
---|
30 | </xsl:function> |
---|
31 | |
---|
32 | <!-- make a commented copy of a node --> |
---|
33 | |
---|
34 | <xsl:template match="*" mode="comment" priority="1"> |
---|
35 | <xsl:text><</xsl:text> |
---|
36 | <xsl:value-of select="name()"/> |
---|
37 | <xsl:apply-templates select="@*" mode="comment"/> |
---|
38 | <xsl:text>></xsl:text> |
---|
39 | <xsl:apply-templates select="node()" mode="comment"/> |
---|
40 | <xsl:text></</xsl:text> |
---|
41 | <xsl:value-of select="name()"/> |
---|
42 | <xsl:text>></xsl:text> |
---|
43 | </xsl:template> |
---|
44 | |
---|
45 | <xsl:template match="@*" mode="comment" priority="1"> |
---|
46 | <xsl:text> </xsl:text> |
---|
47 | <xsl:value-of select="name()"/> |
---|
48 | <xsl:text>="</xsl:text> |
---|
49 | <xsl:value-of select="."/> |
---|
50 | <xsl:text>"</xsl:text> |
---|
51 | </xsl:template> |
---|
52 | |
---|
53 | <!-- convert a string to an integer --> |
---|
54 | <xsl:function name="f:int" as="xs:integer"> |
---|
55 | <xsl:param name="str" as="xs:string"/> |
---|
56 | <xsl:sequence select="normalize-space($str) cast as xs:integer"/> |
---|
57 | </xsl:function> |
---|
58 | |
---|
59 | <!-- lookup matching cells --> |
---|
60 | <xsl:function name="dcif:cellLookups" as="element()*"> |
---|
61 | <xsl:param name="ss" as="element()"/> |
---|
62 | <xsl:param name="names" as="xs:string+"/> |
---|
63 | <xsl:sequence select="$ss//value[normalize-space(lower-case(.))=$names]"/> |
---|
64 | </xsl:function> |
---|
65 | |
---|
66 | <!-- lookup a horizontal values --> |
---|
67 | <xsl:function name="dcif:vertLookups" as="xs:string+"> |
---|
68 | <xsl:param name="ss" as="element()"/> |
---|
69 | <xsl:param name="names" as="xs:string+"/> |
---|
70 | <xsl:variable name="cells" select="dcif:cellLookups($ss,$names)"/> |
---|
71 | <xsl:choose> |
---|
72 | <xsl:when test="exists($cells)"> |
---|
73 | <xsl:sequence select="for $cell in $cells return $cell/following-sibling::value[1]" |
---|
74 | /> |
---|
75 | </xsl:when> |
---|
76 | <xsl:otherwise> |
---|
77 | <xsl:sequence select="'UNK'"/> |
---|
78 | <xsl:message>WRN: unknown [<xsl:value-of select="$names[1]"/>]!</xsl:message> |
---|
79 | </xsl:otherwise> |
---|
80 | </xsl:choose> |
---|
81 | </xsl:function> |
---|
82 | |
---|
83 | <!-- lookup a horizontal value --> |
---|
84 | <xsl:function name="dcif:vertLookup" as="xs:string"> |
---|
85 | <xsl:param name="ss" as="element()"/> |
---|
86 | <xsl:param name="names" as="xs:string+"/> |
---|
87 | <xsl:variable name="vals" select="dcif:vertLookups($ss,$names)"/> |
---|
88 | <xsl:if test="count($vals) gt 1"> |
---|
89 | <xsl:message>WRN: multiple values for [<xsl:value-of select="$names[1]"/>], will use only the first one!</xsl:message> |
---|
90 | </xsl:if> |
---|
91 | <xsl:sequence select="$vals[1]"/> |
---|
92 | </xsl:function> |
---|
93 | |
---|
94 | <!-- lookup columns --> |
---|
95 | <xsl:function name="dcif:colLookups" as="xs:integer*"> |
---|
96 | <xsl:param name="ss" as="element()"/> |
---|
97 | <xsl:param name="names" as="xs:string+"/> |
---|
98 | <xsl:variable name="cells" select="dcif:cellLookups($ss,$names)"/> |
---|
99 | <xsl:choose> |
---|
100 | <xsl:when test="exists($cells)"> |
---|
101 | <xsl:sequence |
---|
102 | select="for $cell in $cells return (count($cell/preceding-sibling::value) + 1)" |
---|
103 | /> |
---|
104 | </xsl:when> |
---|
105 | <xsl:otherwise> |
---|
106 | <xsl:sequence select="0"/> |
---|
107 | <xsl:message>WRN: unknown [<xsl:value-of select="$names[1]"/>]!</xsl:message> |
---|
108 | </xsl:otherwise> |
---|
109 | </xsl:choose> |
---|
110 | </xsl:function> |
---|
111 | |
---|
112 | <!-- lookup a column --> |
---|
113 | <xsl:function name="dcif:colLookup" as="xs:integer?"> |
---|
114 | <xsl:param name="ss" as="element()"/> |
---|
115 | <xsl:param name="names" as="xs:string+"/> |
---|
116 | <xsl:variable name="cols" select="dcif:colLookups($ss,$names)"/> |
---|
117 | <xsl:if test="count($cols) gt 1"> |
---|
118 | <xsl:message>WRN multiple colums for [<xsl:value-of select="$names[1]"/>], will use only the first one!</xsl:message> |
---|
119 | </xsl:if> |
---|
120 | <xsl:sequence select="$cols[1]"/> |
---|
121 | </xsl:function> |
---|
122 | |
---|
123 | <!-- construct the map --> |
---|
124 | <xsl:function name="dcif:map" as="node()"> |
---|
125 | <xsl:param name="ss"/> |
---|
126 | <map> |
---|
127 | <project> |
---|
128 | <xsl:value-of select="dcif:vertLookup($ss,'project')"/> |
---|
129 | </project> |
---|
130 | <contact> |
---|
131 | <xsl:value-of select="dcif:vertLookup($ss,'contact')"/> |
---|
132 | </contact> |
---|
133 | <rel row="{count(dcif:cellLookups($ss,('pid'))[1]/(preceding::line|ancestor::line)) + 1}"> |
---|
134 | <xsl:variable name="PIDs" select="dcif:colLookups($ss,'pid')"/> |
---|
135 | <xsl:choose> |
---|
136 | <xsl:when test="count($PIDs) = 2"> |
---|
137 | <from col="{$PIDs[1]}"/> |
---|
138 | <to col="{$PIDs[2]}"/> |
---|
139 | </xsl:when> |
---|
140 | <xsl:when test="count($PIDs) > 2"> |
---|
141 | <xsl:message>WRN: more then two PID columns detected, the conversion will use the first two ones!</xsl:message> |
---|
142 | <from col="{$PIDs[1]}"/> |
---|
143 | <to col="{$PIDs[2]}"/> |
---|
144 | </xsl:when> |
---|
145 | <xsl:otherwise> |
---|
146 | <xsl:message terminate="yes">ERR: couldn't find any column with a PID!</xsl:message> |
---|
147 | </xsl:otherwise> |
---|
148 | </xsl:choose> |
---|
149 | <relation col="{dcif:colLookup($ss,('relation type','relation'))}"/> |
---|
150 | </rel> |
---|
151 | </map> |
---|
152 | </xsl:function> |
---|
153 | |
---|
154 | <xsl:template match="csvFile"> |
---|
155 | <xsl:variable name="map" select="dcif:map(.)"/> |
---|
156 | <xsl:result-document href="map.xml" method="xml"> |
---|
157 | <xsl:copy-of select="$map"/> |
---|
158 | </xsl:result-document> |
---|
159 | <xsl:choose> |
---|
160 | <xsl:when test="f:int($map//rel/@row) gt 0"> |
---|
161 | <xsl:text>@prefix relcat : <http://www.isocat.org/relcat/set/> .</xsl:text> |
---|
162 | <xsl:value-of select="$NL"/> |
---|
163 | <xsl:text>@prefix rel : <http://www.isocat.org/relcat/relations#> .</xsl:text> |
---|
164 | <xsl:value-of select="$NL"/> |
---|
165 | <xsl:value-of select="$NL"/> |
---|
166 | <xsl:text>relcat:</xsl:text> |
---|
167 | <xsl:value-of select="functx:words-to-camel-case($map/project)"/> |
---|
168 | <xsl:text> {</xsl:text> |
---|
169 | <xsl:value-of select="$NL"/> |
---|
170 | <xsl:apply-templates select="subsequence(line,$map/rel/@row)"> |
---|
171 | <xsl:with-param name="map" select="$map" tunnel="yes"/> |
---|
172 | <xsl:with-param name="rel" select="$map/rel" tunnel="yes"/> |
---|
173 | </xsl:apply-templates> |
---|
174 | <xsl:text>}</xsl:text> |
---|
175 | <xsl:value-of select="$NL"/> |
---|
176 | </xsl:when> |
---|
177 | <xsl:otherwise> |
---|
178 | <xsl:message terminate="yes">ERR: can't determine where the rows with relation information start!</xsl:message> |
---|
179 | </xsl:otherwise> |
---|
180 | </xsl:choose> |
---|
181 | </xsl:template> |
---|
182 | |
---|
183 | <xsl:template match="line[normalize-space(.)!='']"> |
---|
184 | <xsl:param name="map" as="element()" tunnel="yes"/> |
---|
185 | <xsl:param name="rel" as="element()" tunnel="yes"/> |
---|
186 | <xsl:variable name="line" select="current()"/> |
---|
187 | |
---|
188 | <xsl:variable name="from" select="value[f:int($rel/from/@col)]"/> |
---|
189 | <xsl:variable name="relation" select="value[f:int($rel/relation/@col)]"/> |
---|
190 | <xsl:variable name="to" select="value[f:int($rel/to/@col)]"/> |
---|
191 | |
---|
192 | <xsl:text> </xsl:text> |
---|
193 | |
---|
194 | <xsl:text><</xsl:text> |
---|
195 | <xsl:choose> |
---|
196 | <xsl:when test="matches($from,'\d+')"> |
---|
197 | <xsl:text>http://www.isocat.org/datcat/DC-</xsl:text> |
---|
198 | <xsl:value-of select="$from"/> |
---|
199 | </xsl:when> |
---|
200 | <xsl:when test="matches($from,'DC-\d+')"> |
---|
201 | <xsl:text>http://www.isocat.org/datcat/</xsl:text> |
---|
202 | <xsl:value-of select="$from"/> |
---|
203 | </xsl:when> |
---|
204 | <xsl:otherwise> |
---|
205 | <xsl:value-of select="$from"/> |
---|
206 | </xsl:otherwise> |
---|
207 | </xsl:choose> |
---|
208 | <xsl:text>></xsl:text> |
---|
209 | |
---|
210 | <xsl:text> </xsl:text> |
---|
211 | |
---|
212 | <xsl:choose> |
---|
213 | <xsl:when test="$relation = ('related','sameAs','almostSameAs','broaderThan','superClassOf','hasPart','hasDirectPart','narrowerThan','subClassOf','isPartOf','isDirectPartOf')"> |
---|
214 | <xsl:text>rel:</xsl:text> |
---|
215 | <xsl:value-of select="$relation"/> |
---|
216 | </xsl:when> |
---|
217 | <xsl:otherwise> |
---|
218 | <xsl:value-of select="$relation"/> |
---|
219 | </xsl:otherwise> |
---|
220 | </xsl:choose> |
---|
221 | |
---|
222 | <xsl:text> </xsl:text> |
---|
223 | |
---|
224 | <xsl:text><</xsl:text> |
---|
225 | <xsl:choose> |
---|
226 | <xsl:when test="matches($to,'\d+')"> |
---|
227 | <xsl:text>http://www.isocat.org/datcat/DC-</xsl:text> |
---|
228 | <xsl:value-of select="$to"/> |
---|
229 | </xsl:when> |
---|
230 | <xsl:when test="matches($to,'DC-\d+')"> |
---|
231 | <xsl:text>http://www.isocat.org/datcat/</xsl:text> |
---|
232 | <xsl:value-of select="$to"/> |
---|
233 | </xsl:when> |
---|
234 | <xsl:otherwise> |
---|
235 | <xsl:value-of select="$to"/> |
---|
236 | </xsl:otherwise> |
---|
237 | </xsl:choose> |
---|
238 | <xsl:text>></xsl:text> |
---|
239 | |
---|
240 | <xsl:text> .</xsl:text> |
---|
241 | <xsl:value-of select="$NL"/> |
---|
242 | </xsl:template> |
---|
243 | |
---|
244 | </xsl:stylesheet> |
---|