index.xsl 62.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (C) 2001-2016 Food and Agriculture Organization of the
  ~ United Nations (FAO-UN), United Nations World Food Programme (WFP)
  ~ and United Nations Environment Programme (UNEP)
  ~
  ~ This program is free software; you can redistribute it and/or modify
  ~ it under the terms of the GNU General Public License as published by
  ~ the Free Software Foundation; either version 2 of the License, or (at
  ~ your option) any later version.
  ~
  ~ This program is distributed in the hope that it will be useful, but
  ~ WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  ~ General Public License for more details.
  ~
  ~ You should have received a copy of the GNU General Public License
  ~ along with this program; if not, write to the Free Software
  ~ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  ~
  ~ Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
  ~ Rome - Italy. email: geonetwork@osgeo.org
  -->
24
25
  <!-- Modified 2018-2019 by Kim Mortimer @ MERIDIAN - k.mortimer@dal.ca -->
 <!-- Modifications essentially to support ISO 19139-2 + Darwin Core -->
26
27
28
29
30
31
32
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:gmd="http://www.isotc211.org/2005/gmd"
                xmlns:gmx="http://www.isotc211.org/2005/gmx"
                xmlns:gmi="http://standards.iso.org/iso/19115/-2/gmi/1.0"
                xmlns:gco="http://www.isotc211.org/2005/gco"
                xmlns:srv="http://www.isotc211.org/2005/srv"
33
34
35
36
                xmlns:dcterms="http://purl.org/dc/terms/" 
                xmlns:dwc="http://rs.tdwg.org/dwc/terms/" 
                xmlns:dwr="http://rs.tdwg.org/dwc/dwcrecord/"                                                                                       
                xmlns:geonet="http://www.fao.org/geonetwork"
Kim Mortimer's avatar
Kim Mortimer committed
37
                xmlns:gml="http://www.opengis.net/gml/3.2" 
38
                xmlns:MER="https://meridian.cs.dal.ca/"
39
40
41
42
43
44
45
46
47
                xmlns:xlink="http://www.w3.org/1999/xlink"
                xmlns:daobs="http://daobs.org"
                xmlns:saxon="http://saxon.sf.net/"
                extension-element-prefixes="saxon"
                exclude-result-prefixes="#all"
                version="2.0">

  <xsl:import href="fn.xsl"/>
  <xsl:import href="inspire-constant.xsl"/>
Kim Mortimer's avatar
Kim Mortimer committed
48
49
  <xsl:function name="MER:encloseWithBracket">
    <xsl:param name="items"/>
Kim Mortimer's avatar
Kim Mortimer committed
50
    <xsl:value-of select="concat('[',string-join($items,','),'],')"/>
Kim Mortimer's avatar
Kim Mortimer committed
51
  </xsl:function>
Kim Mortimer's avatar
Kim Mortimer committed
52
53
54
55
56
  <xsl:function name="MER:ByTupleEncloseWithBracket">
    <xsl:param name="items"/>
    <xsl:sequence 
      select="if (empty($items))
        then ()
Kim Mortimer's avatar
Kim Mortimer committed
57
58
        else (MER:encloseWithBracket(($items[1],$items[2])),
        MER:ByTupleEncloseWithBracket($items[position()>2]) )"
Kim Mortimer's avatar
Kim Mortimer committed
59
60
    />
  </xsl:function>
Kim Mortimer's avatar
Kim Mortimer committed
61
  
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  <xsl:output method="xml" indent="yes"/>

  <xsl:output name="default-serialize-mode"
              indent="no"
              omit-xml-declaration="yes"
              encoding="utf-8"
              escape-uri-attributes="yes"/>

  <!-- Define if operatesOn type should be defined
  by analysis of protocol in all transfers options.
  -->
  <xsl:variable name="operatesOnSetByProtocol" select="false()"/>

  <!-- Define if search for regulation title should be strict or light. -->
  <xsl:variable name="inspireRegulationLaxCheck" select="false()"/>

  <!-- List of keywords to search for to flag a record as opendata.
   Do not put accents or upper case letters here as comparison will not
   take them in account. -->
  <xsl:variable name="openDataKeywords"
                select="'opendata|open data|donnees ouvertes'"/>

  <xsl:variable name="dateFormat" as="xs:string"
                select="'[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]'"/>

  <xsl:variable name="separator" as="xs:string"
                select="'|'"/>

  <!-- To avoid Document contains at least one immense term
  in field="resourceAbstract" (whose UTF8 encoding is longer
  than the max length 32766. -->
  <xsl:variable name="maxFieldLength" select="32000" as="xs:integer"/>

  <xsl:template match="/">
    <xsl:apply-templates mode="index"/>
  </xsl:template>

  <xsl:template match="gmi:MI_Metadata|gmd:MD_Metadata"
                mode="extract-uuid">
    <xsl:value-of select="gmd:fileIdentifier/gco:CharacterString"/>
  </xsl:template>


  <xsl:template mode="index-extra-fields" match="*"/>

  <xsl:template mode="index-extra-documents" match="*"/>

  <xsl:template match="gmi:MI_Metadata|gmd:MD_Metadata"
                mode="index">
    <!-- Main variables for the document -->
    <xsl:variable name="identifier" as="xs:string"
                  select="gmd:fileIdentifier/gco:CharacterString[. != '']"/>


    <xsl:variable name="mainLanguageCode" as="xs:string?"
                  select="gmd:language[1]/gmd:LanguageCode/
                        @codeListValue[normalize-space(.) != '']"/>
    <xsl:variable name="mainLanguage" as="xs:string?"
                  select="if ($mainLanguageCode) then $mainLanguageCode else
                    gmd:language[1]/gco:CharacterString[normalize-space(.) != '']"/>

    <xsl:variable name="otherLanguages" as="attribute()*"
                  select="gmd:locale/gmd:PT_Locale/
                        gmd:languageCode/gmd:LanguageCode/
                          @codeListValue[normalize-space(.) != '']"/>

    <!-- Record is dataset if no hierarchyLevel -->
    <xsl:variable name="isDataset" as="xs:boolean"
                  select="
                      count(gmd:hierarchyLevel[gmd:MD_ScopeCode/@codeListValue='dataset']) > 0 or
                      count(gmd:hierarchyLevel) = 0"/>
    <xsl:variable name="isService" as="xs:boolean"
                  select="
                      count(gmd:hierarchyLevel[gmd:MD_ScopeCode/@codeListValue='service']) > 0"/>

    <xsl:message><xsl:value-of select="concat(
Kim Mortimer's avatar
Kim Mortimer committed
138
      '#', count(preceding-sibling::gmi:MI_Metadata), $identifier)"/>
139
140
141
142
143
    </xsl:message>

    <!-- Create a first document representing the main record. -->
    <doc>
      <documentType>metadata</documentType>
Kim Mortimer's avatar
Kim Mortimer committed
144
      <documentStandard>iso19139.MERIDIAN</documentStandard>
145
146

      <!-- Index the metadata document as XML -->
147
148
      <!-- MERIDIAN edit - this field will be used instead for holding an APA style citation.
      To reduce the risk that our Discovery layer reads in 'citations' from things that are not actually citations, 
149
      I use a check for a controlled leading string that would be unlikely to be present in other records. -->                                                                                     
150
151
152
153
154
      
        <xsl:if test="./gmd:identificationInfo/MER:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:otherCitationDetails/gco:CharacterString[starts-with(text(),'MERIDIAN-CITE-AS|')]">
          <citationItalics>
		    <xsl:value-of select="substring-before(substring-after(./gmd:identificationInfo/MER:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:otherCitationDetails/gco:CharacterString, '|'), '|')"/>
          </citationItalics>
Kim Mortimer's avatar
Kim Mortimer committed
155
		  <citationAuthor>
156
		    <xsl:value-of select="substring-before(substring-after(substring-after(./gmd:identificationInfo/MER:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:otherCitationDetails/gco:CharacterString, '|'), '|'), '|')"/>
Kim Mortimer's avatar
Kim Mortimer committed
157
          </citationAuthor>
158
159
160
161
162
163
164
165
		  <citationTitle>
		    <xsl:value-of select="substring-before(substring-after(substring-after(substring-after(./gmd:identificationInfo/MER:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:otherCitationDetails/gco:CharacterString, '|'), '|'), '|'), '|')"/>
          </citationTitle>
		  <citationRemainder>
		    <xsl:value-of select="substring-before(substring-after(substring-after(substring-after(substring-after(./gmd:identificationInfo/MER:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:otherCitationDetails/gco:CharacterString, '|'), '|'), '|'), '|'), '|')"/>
          </citationRemainder>
		</xsl:if>
      
166
      <uuid>
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
        <xsl:value-of select="$identifier"/>
      </uuid>
      <metadataIdentifier>
        <xsl:value-of select="$identifier"/>
      </metadataIdentifier>

      <xsl:for-each select="gmd:metadataStandardName/gco:CharacterString">
        <standardName>
          <xsl:value-of select="normalize-space(.)"/>
        </standardName>
      </xsl:for-each>

      <harvestedDate>
        <xsl:value-of select="format-dateTime(current-dateTime(), $dateFormat)"/>
      </harvestedDate>


      <!-- Indexing record information -->
      <!-- # Date -->
      <!-- TODO improve date formatting maybe using Joda parser
      Select first one because some records have 2 dates !
      eg. fr-784237539-bdref20100101-0105

      Remove millisec and timezone until not supported
      eg. 2017-02-08T13:18:03.138+00:02
      -->
      <xsl:for-each select="gmd:dateStamp/*[text() != '' and position() = 1]">
        <dateStamp>
          <xsl:variable name="date"
                        select="if (name() = 'gco:Date' and string-length(.) = 4)
                                then concat(., '-01-01T00:00:00')
                                else if (name() = 'gco:Date' and string-length(.) = 7)
                                then concat(., '-01T00:00:00')
                                else if (name() = 'gco:Date' or string-length(.) = 10)
                                then concat(., 'T00:00:00')
                                else if (contains(., '.'))
                                then tokenize(., '\.')[1]
                                else ."/>

          <xsl:value-of select="translate(string(
                                   adjust-dateTime-to-timezone(
                                      xs:dateTime($date),
                                      xs:dayTimeDuration('PT0H'))
                                     ), 'Z', '')"/>
        </dateStamp>
      </xsl:for-each>


      <!-- # Languages -->
      <mainLanguage>
        <xsl:value-of select="$mainLanguage"/>
      </mainLanguage>

      <xsl:for-each select="$otherLanguages">
        <otherLanguage>
          <xsl:value-of select="."/>
        </otherLanguage>
      </xsl:for-each>


      <!-- # Resource type -->
      <xsl:choose>
        <xsl:when test="$isDataset">
          <resourceType>dataset</resourceType>
        </xsl:when>
        <xsl:otherwise>
          <xsl:for-each select="gmd:hierarchyLevel/gmd:MD_ScopeCode/
                              @codeListValue[normalize-space(.) != '']">
            <resourceType>
236
              <xsl:value-of select="../../gmd:MD_ScopeCode[text()]"/>
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
            </resourceType>
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>


      <!-- Indexing metadata contact -->
      <xsl:apply-templates mode="index-contact" select="gmd:contact">
        <xsl:with-param name="fieldSuffix" select="''"/>
      </xsl:apply-templates>

      <!-- Indexing all codelist

      Indexing method is:
      <gmd:accessConstraints>
        <gmd:MD_RestrictionCode codeListValue="otherRestrictions"
        is indexed as
        codelist_accessConstraints:otherRestrictions

        Exclude some useless codelist like
        Contact role, Date type.
      -->
      <xsl:for-each select=".//*[@codeListValue != '' and
                            name() != 'gmd:CI_RoleCode' and
                            name() != 'gmd:CI_DateTypeCode' and
                            name() != 'gmd:LanguageCode'
                            ]">
        <xsl:element name="codelist_{local-name(..)}">
265
          <xsl:value-of select="."/>
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
        </xsl:element>
      </xsl:for-each>


      <!-- Indexing resource information
      TODO: Should we support multiple identification in the same record
      eg. nl db60a314-5583-437d-a2ff-1e59cc57704e
      Also avoid error when records contains multiple MD_IdentificationInfo
      or SRV_ServiceIdentification or a mix
      eg. de 8bb5334f-558b-982b-7b12-86ea486540d7
      -->
      <xsl:for-each select="gmd:identificationInfo[1]/*[1]">
        <xsl:for-each select="gmd:citation/gmd:CI_Citation">
          <resourceTitle>
            <xsl:value-of select="gmd:title/gco:CharacterString/text()"/>
          </resourceTitle>
          <resourceAltTitle>
            <xsl:value-of
              select="gmd:alternateTitle/gco:CharacterString/text()"/>
          </resourceAltTitle>

          <xsl:for-each select="gmd:date/gmd:CI_Date[gmd:date/*/text() != '' and
                                  matches(gmd:date/*/text(), '[0-9]{4}.*')]">
            <xsl:variable name="dateType"
290
                          select="gmd:dateType[1]/gmd:CI_DateTypeCode[text()]"
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
                          as="xs:string?"/>
            <xsl:variable name="date"
                          select="string(gmd:date[1]/gco:Date|gmd:date[1]/gco:DateTime)"/>
            <xsl:element name="{$dateType}DateForResource">
              <xsl:value-of select="$date"/>
            </xsl:element>
            <xsl:element name="{$dateType}YearForResource">
              <xsl:value-of select="substring($date, 0, 5)"/>
            </xsl:element>
            <xsl:element name="{$dateType}MonthForResource">
              <xsl:value-of select="substring($date, 0, 8)"/>
            </xsl:element>
          </xsl:for-each>

          <xsl:for-each
            select="gmd:presentationForm/gmd:CI_PresentationFormCode/@codeListValue[. != '']">
            <presentationForm>
              <xsl:value-of select="."/>
            </presentationForm>
          </xsl:for-each>
        </xsl:for-each>

        <resourceAbstract>
          <xsl:value-of select="substring(
                gmd:abstract/gco:CharacterString,
                0, $maxFieldLength)"/>
        </resourceAbstract>


        <!-- Indexing resource contact -->
        <xsl:apply-templates mode="index-contact"
                             select="gmd:pointOfContact">
          <xsl:with-param name="fieldSuffix" select="'ForResource'"/>
        </xsl:apply-templates>
325
326
327
        <xsl:apply-templates mode="index-contact" select="gmd:citation/gmd:CI_Citation/gmd:citedResponsibleParty">
          <xsl:with-param name="fieldSuffix" select="'ForCitation'"/>
        </xsl:apply-templates>
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
        <xsl:for-each select="gmd:credit/*[. != '']">
          <resourceCredit>
            <xsl:value-of select="."/>
          </resourceCredit>
        </xsl:for-each>


        <xsl:variable name="overviews"
                      select="gmd:graphicOverview/gmd:MD_BrowseGraphic/
                              gmd:fileName/gco:CharacterString[. != '']"/>
        <hasOverview>
          <xsl:value-of select="if (count($overviews) > 0) then 'true' else 'false'"/>
        </hasOverview>

        <xsl:for-each select="$overviews">
          <overviewUrl>
            <xsl:value-of select="."/>
          </overviewUrl>
        </xsl:for-each>

        <xsl:for-each
349
          select="gmd:language/gco:CharacterString|gmd:language/gmd:LanguageCode[@codeListValue]">
350
          <resourceLanguage>
Kim Mortimer's avatar
Kim Mortimer committed
351
            <xsl:value-of select="."/>
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
          </resourceLanguage>
        </xsl:for-each>


        <!-- TODO: create specific INSPIRE template or mode -->
        <!-- INSPIRE themes

        Select the first thesaurus title because some records
        may contains many even if invalid.

        Also get the first title at it may happen that a record
        have more than one.

        Select any thesaurus having the title containing "INSPIRE themes".
        Some records have "GEMET-INSPIRE themes" eg. sk:ee041534-b8f3-4683-b9dd-9544111a0712
        Some other "GEMET - INSPIRE themes"

        Take in account gmd:descriptiveKeywords or srv:keywords
        -->
        <!-- TODO: Some MS may be using a translated version of the thesaurus title -->
        <xsl:variable name="inspireKeywords"
                      select="*/gmd:MD_Keywords[
                      contains(lower-case(
                       gmd:thesaurusName[1]/*/gmd:title[1]/*[1]/text()
                       ), 'gemet') and
                       contains(lower-case(
                       gmd:thesaurusName[1]/*/gmd:title[1]/*[1]/text()
                       ), 'inspire')]
                  /gmd:keyword"/>
        <xsl:for-each
          select="$inspireKeywords">
          <xsl:variable name="position" select="position()"/>
          <xsl:for-each select="gco:CharacterString[. != '']|
                                gmx:Anchor[. != '']">
            <xsl:variable name="inspireTheme" as="xs:string"
                          select="text()"/>

            <inspireTheme_syn>
              <xsl:value-of select="text()"/>
            </inspireTheme_syn>
            <inspireTheme>
              <xsl:value-of select="$inspireTheme"/>
            </inspireTheme>

            <!--
            WARNING: Here we only index the first keyword in order
            to properly compute one INSPIRE annex.
            -->
            <xsl:if test="$position = 1">
              <inspireThemeFirst_syn>
                <xsl:value-of select="text()"/>
              </inspireThemeFirst_syn>
              <inspireThemeFirst>
                <xsl:value-of select="$inspireTheme"/>
              </inspireThemeFirst>
              <inspireAnnexForFirstTheme>
                <xsl:value-of select="$inspireTheme"/>
              </inspireAnnexForFirstTheme>
            </xsl:if>
            <inspireAnnex>
              <xsl:value-of select="text()"/>
            </inspireAnnex>
          </xsl:for-each>
        </xsl:for-each>

        <!-- For services, the count does not take into account
        dataset's INSPIRE themes which are transfered to the service
        by service-dataset-task. -->
        <inspireThemeNumber>
          <xsl:value-of
            select="count($inspireKeywords)"/>
        </inspireThemeNumber>

        <hasInspireTheme>
          <xsl:value-of
            select="if (count($inspireKeywords) > 0) then 'true' else 'false'"/>
        </hasInspireTheme>

        <!-- Index all keywords -->
        <xsl:variable name="keywords"
                      select="*/gmd:MD_Keywords/
                                gmd:keyword/gco:CharacterString|
                              */gmd:MD_Keywords/
                                gmd:keyword/gmd:PT_FreeText/gmd:textGroup/
                                  gmd:LocalisedCharacterString"/>

        <tagNumber>
          <xsl:value-of select="count($keywords)"/>
        </tagNumber>

        <xsl:for-each select="$keywords">
          <tag>
            <xsl:value-of select="text()"/>
          </tag>
        </xsl:for-each>

        <xsl:variable name="isOpenData">
          <xsl:for-each select="$keywords">
            <xsl:if test="matches(
                            normalize-unicode(replace(normalize-unicode(
                              lower-case(normalize-space(text())), 'NFKD'), '\p{Mn}', ''), 'NFKC'),
                            $openDataKeywords)">
              <xsl:value-of select="'true'"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="normalize-space($isOpenData) != ''">
            <isOpenData>true</isOpenData>
          </xsl:when>
          <xsl:otherwise>
            <isOpenData>false</isOpenData>
          </xsl:otherwise>
        </xsl:choose>

        <!-- Index keywords which are of type place -->
        <xsl:for-each
          select="*/gmd:MD_Keywords/
                          gmd:keyword[gmd:type/gmd:MD_KeywordTypeCode/@codeListValue = 'place']/
                            gco:CharacterString|
                        */gmd:MD_Keywords/
                          gmd:keyword[gmd:type/gmd:MD_KeywordTypeCode/@codeListValue = 'place']/
                            gmd:PT_FreeText/gmd:textGroup/gmd:LocalisedCharacterString">
          <geotag>
            <xsl:value-of select="text()"/>
          </geotag>
        </xsl:for-each>


        <!-- Index all keywords having a specific thesaurus -->
        <xsl:for-each
          select="*/gmd:MD_Keywords[gmd:thesaurusName]/
                            gmd:keyword">

          <xsl:variable name="thesaurusName"
                        select="../gmd:thesaurusName[1]/gmd:CI_Citation/
                                  gmd:title[1]/gco:CharacterString"/>

          <xsl:variable name="thesaurusId"
                        select="normalize-space(../gmd:thesaurusName/gmd:CI_Citation/
                                  gmd:identifier[position() = 1]/gmd:MD_Identifier/
                                    gmd:code/(gco:CharacterString|gmx:Anchor)/text())"/>

          <xsl:variable name="key">
            <xsl:choose>
              <xsl:when test="$thesaurusId != ''">
                <xsl:value-of select="$thesaurusId"/>
              </xsl:when>
              <!-- Try to build a thesaurus key based on the name
              by removing space - to be improved. -->
              <xsl:when test="normalize-space($thesaurusName) != ''">
                <!--TODO handle special character to build a valid key usable for field
                <xsl:value-of select="replace($thesaurusName, ' ', '-')"/>-->
              </xsl:when>
            </xsl:choose>
          </xsl:variable>

          <xsl:if test="normalize-space($key) != ''">
            <!-- Index keyword characterString including multilingual ones
             and element like gmx:Anchor including the href attribute
             which may contains keyword identifier. -->
            <xsl:for-each select="*[normalize-space() != '']|
                                  */@xlink:href[normalize-space() != '']|
                                  gmd:PT_FreeText/gmd:textGroup/gmd:LocalisedCharacterString[normalize-space() != '']">
              <xsl:element name="thesaurus_{replace($key, '[^a-zA-Z0-9]', '')}">
                <xsl:value-of select="normalize-space(.)"/>
              </xsl:element>
            </xsl:for-each>
          </xsl:if>
        </xsl:for-each>


        <xsl:for-each select="gmd:topicCategory/gmd:MD_TopicCategoryCode">
          <topic>
            <xsl:value-of select="."/>
          </topic>
          <!-- TODO: Get translation ? -->
        </xsl:for-each>


        <xsl:for-each select="gmd:spatialResolution/gmd:MD_Resolution">
          <xsl:for-each
            select="gmd:equivalentScale/gmd:MD_RepresentativeFraction/gmd:denominator/gco:Integer[. != '']">
            <resolutionScaleDenominator>
              <xsl:value-of select="."/>
            </resolutionScaleDenominator>
          </xsl:for-each>

          <xsl:for-each select="gmd:distance/gco:Distance[. != '']">
            <resolutionDistance>
              <xsl:value-of select="concat(., ' ', @uom)"/>
            </resolutionDistance>
          </xsl:for-each>
        </xsl:for-each>

        <xsl:for-each
          select="gmd:spatialRepresentationType/gmd:MD_SpatialRepresentationTypeCode/@codeListValue[. != '']">
          <spatialRepresentationType>
            <xsl:value-of select="."/>
          </spatialRepresentationType>
        </xsl:for-each>


        <xsl:for-each select="gmd:resourceConstraints">
          <xsl:for-each
            select="*/gmd:accessConstraints/gmd:MD_RestrictionCode/@codeListValue[. != '']">
            <accessConstraints>
559
              <xsl:value-of select="../../gmd:MD_RestrictionCode[text()]"/>
560
561
562
563
564
565
566
567
568
569
570
            </accessConstraints>
          </xsl:for-each>
          <xsl:for-each
            select="*/gmd:otherConstraints/gco:CharacterString[. != '']">
            <otherConstraints>
              <xsl:value-of select="."/>
            </otherConstraints>
          </xsl:for-each>
          <xsl:for-each
            select="*/gmd:classification/gmd:MD_ClassificationCode/@codeListValue[. != '']">
            <constraintClassification>
571
              <xsl:value-of select="../../gmd:MD_ClassificationCode[text()]"/>
572
573
574
575
576
577
578
579
580
            </constraintClassification>
          </xsl:for-each>
          <xsl:for-each
            select="*/gmd:useLimitation/gco:CharacterString[. != '']">
            <useLimitation>
              <xsl:value-of select="."/>
            </useLimitation>
          </xsl:for-each>
        </xsl:for-each>
581
582
583
584
585
        <xsl:for-each select="gmd:resourceMaintenance/gmd:MD_MaintenanceInformation[gmd:dateOfNextUpdate/*/text() != '']">
            <dateOfNextUpdate>
                <xsl:value-of select="string(gmd:dateOfNextUpdate[1]/gco:Date|gmd:dateOfNextUpdate[1]/gco:DateTime)"/>
            </dateOfNextUpdate>
        </xsl:for-each>
586
587
588
589
590
591
592
        <xsl:for-each select="gmd:supplementalInformation/gco:CharacterString">
            <supplementalInformation>
                <xsl:value-of select="."/>
            </supplementalInformation>
        </xsl:for-each>
        
        <xsl:for-each select="dwr:DarwinRecordSet">
593
          <xsl:for-each select="./dwc:Occurrence">
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
            <xsl:variable name="occID" select="./dwc:occurrenceID"/>
            <occurrenceHasID>
              <xsl:value-of select="$occID"/>
            </occurrenceHasID>
            <occurrenceRecordedBy>
              <xsl:value-of select="./dwc:recordedBy"/>
            </occurrenceRecordedBy>
            <occurrenceBasis>
              <xsl:value-of select="./dwc:basisOfRecord"/>
            </occurrenceBasis>
            <occurrenceCount>
              <xsl:value-of select="./dwc:organismQuantity"/><xsl:text> </xsl:text><xsl:value-of select="./dwc:organismQuantityType"/>
            </occurrenceCount>
            <occurrenceRemarks>
              <xsl:value-of select="./dwc:occurrenceRemarks"/>
            </occurrenceRemarks>
610
611
          </xsl:for-each>
          <xsl:for-each select="./dwc:Event">
612
613
614
615
616
617
            <occurrenceSamplingProtocol>
              <xsl:value-of select="./dwc:samplingProtocol"/>
            </occurrenceSamplingProtocol>
            <occurrenceSamplingEffort>
              <xsl:value-of select="./dwc:samplingEffort"/>
            </occurrenceSamplingEffort>
618
619
          </xsl:for-each>
          <xsl:for-each select="./dwc:Organism">
620
621
622
623
624
625
626
627
628
629
630
            <xsl:variable name="orgID" select="./dwc:organismID"/>
            <xsl:variable name="taxonID" select="./dwc:taxonID"/>
            <organismHasID>
              <xsl:value-of select="$orgID"/>
            </organismHasID>
            <organismBelongsToTaxon>
              <xsl:value-of select="$taxonID"/>
            </organismBelongsToTaxon>
            <organismRemarks>
              <xsl:value-of select="./dwc:organismRemarks"/>
            </organismRemarks>
631
632
          </xsl:for-each>
          <xsl:for-each select="./dwc:Identification">
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
            <xsl:variable name="occID" select="./dwc:occurrenceID"/>
            <xsl:variable name="identID" select="./dwc:identificationID"/>
            <identificationHasID>
              <xsl:value-of select="$identID"/>
            </identificationHasID>
            <identificationBelongsToOccurrence>
              <xsl:value-of select="$occID"/>
            </identificationBelongsToOccurrence>
            <identificationBy>
              <xsl:value-of select="./dwc:identifiedBy"/>
            </identificationBy>
            <identificationRemarks>
              <xsl:value-of select="./dwc:identificationRemarks"/>
            </identificationRemarks>
            <identificationReferences>
              <xsl:value-of select="./dwc:identificationReferences"/>
            </identificationReferences>
650
651
          </xsl:for-each>
          <xsl:for-each select="./dwc:Taxon">
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
            <xsl:variable name="identID" select="./dwc:identificationID"/>
            <xsl:variable name="taxonID" select="./dwc:taxonID"/>
            <taxonHasID>
            <xsl:value-of select="$taxonID"/>
            </taxonHasID>
            <taxonBelongsToIdentification>
              <xsl:value-of select="$identID"/>
            </taxonBelongsToIdentification>
            <taxonVernacularName>
              <xsl:value-of select="./dwc:vernacularName"/>
            </taxonVernacularName>
            <taxonScientificName>
              <xsl:value-of select="./dwc:scientificName"/>
            </taxonScientificName>
            <taxonRemarks>
              <xsl:value-of select="./dwc:taxonRemarks"/>
            </taxonRemarks>
669
          </xsl:for-each>
670
        </xsl:for-each>
671
672
673
674
675
676
677
678
679
680
681
682

        <xsl:for-each select="*/gmd:EX_Extent">

          <xsl:for-each select="gmd:geographicElement/gmd:EX_GeographicDescription/
            gmd:geographicIdentifier/gmd:MD_Identifier/
            gmd:code/gco:CharacterString[normalize-space(.) != '']">
            <geoTag>
              <xsl:value-of select="."/>
            </geoTag>
          </xsl:for-each>

          <!-- TODO: index bounding polygon -->
683
684
685
          <xsl:if test="count(.//gmd:EX_GeographicBoundingBox)">
            <geojson>
              <xsl:variable name="isMulti">
686
                <xsl:choose>
687
688
                  <xsl:when test="count(.//gmd:EX_GeographicBoundingBox) = 1">
                    <xsl:text>false</xsl:text>
689
690
                  </xsl:when>
                  <xsl:otherwise>
691
                    <xsl:text>true</xsl:text>
692
693
                  </xsl:otherwise>
                </xsl:choose>
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
              </xsl:variable>
              <xsl:choose>
                <xsl:when test="count(.//gmd:EX_GeographicBoundingBox) = 1">
                  <xsl:text>{"type": "Polygon",</xsl:text>
                  <xsl:text>"coordinates": [ 
                  </xsl:text>
                </xsl:when>                
                <xsl:otherwise>
                  <xsl:text>{"type": "MultiPolygon",</xsl:text>
                  <xsl:text>"coordinates": [ <!-- curly brace opened, 1 square brackets opened -->
                  </xsl:text>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:for-each select=".//gmd:EX_GeographicBoundingBox[
                                    ./gmd:westBoundLongitude/gco:Decimal castable as xs:decimal and
                                    ./gmd:eastBoundLongitude/gco:Decimal castable as xs:decimal and
                                    ./gmd:northBoundLatitude/gco:Decimal castable as xs:decimal and
                                    ./gmd:southBoundLatitude/gco:Decimal castable as xs:decimal
                                    ]">
                <xsl:variable name="format" select="'#0.000000'"></xsl:variable>

                <xsl:variable name="w"
                              select="format-number(./gmd:westBoundLongitude/gco:Decimal/text(), $format)"/>
                <xsl:variable name="e"
                              select="format-number(./gmd:eastBoundLongitude/gco:Decimal/text(), $format)"/>
                <xsl:variable name="n"
                              select="format-number(./gmd:northBoundLatitude/gco:Decimal/text(), $format)"/>
                <xsl:variable name="s"
                              select="format-number(./gmd:southBoundLatitude/gco:Decimal/text(), $format)"/>

                <!-- Example: ENVELOPE(-10, 20, 15, 10) which is minX, maxX, maxY, minY order
                http://wiki.apache.org/solr/SolrAdaptersForLuceneSpatial4
                https://cwiki.apache.org/confluence/display/solr/Spatial+Search

                bbox field type limited to one. TODO
                <xsl:if test="position() = 1">
                  <bbox>
                    <xsl:text>ENVELOPE(</xsl:text>
                    <xsl:value-of select="$w"/>
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="$e"/>
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="$n"/>
                    <xsl:text>,</xsl:text>
                    <xsl:value-of select="$s"/>
                    <xsl:text>)</xsl:text>
                  </field>
                </xsl:if>
                -->
                <xsl:if test="$isMulti = 'true'">
                  <xsl:text>[</xsl:text> <!-- wraps the entire polygon component in a square bracket -->
                </xsl:if>
                <xsl:text>[</xsl:text> <!-- wraps exterior -->
                <xsl:value-of select="concat('[', $w, ',', $s, ']')"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="concat('[', $e, ',', $s, ']')"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="concat('[', $e, ',', $n, ']')"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="concat('[', $w, ',', $n, ']')"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="concat('[', $w, ',', $s, ']')"/>
                <xsl:text>]</xsl:text><!-- closes the exterior -->
                <xsl:if test="$isMulti = 'true'">
                  <xsl:text>]</xsl:text> <!-- closes the wrapping -->
                </xsl:if>
                <xsl:if test="position() != last() and $isMulti = 'true'">
                  <xsl:text>,</xsl:text> <!-- adds a comma to allow the next polygon -->
                </xsl:if>       
                <!--<xsl:value-of select="($e + $w) div 2"/>,<xsl:value-of select="($n + $s) div 2"/></field>-->
Kim Mortimer's avatar
Kim Mortimer committed
764
                
765
766
          
              </xsl:for-each>
Kim Mortimer's avatar
Kim Mortimer committed
767
              <xsl:text>]}</xsl:text>
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
            </geojson>
            <xsl:for-each select=".//gmd:EX_GeographicBoundingBox[
                                    ./gmd:westBoundLongitude/gco:Decimal castable as xs:decimal and
                                    ./gmd:eastBoundLongitude/gco:Decimal castable as xs:decimal and
                                    ./gmd:northBoundLatitude/gco:Decimal castable as xs:decimal and
                                    ./gmd:southBoundLatitude/gco:Decimal castable as xs:decimal
                                    ]">
              <xsl:variable name="format" select="'#0.000000'"></xsl:variable>

              <xsl:variable name="w"
                            select="format-number(./gmd:westBoundLongitude/gco:Decimal/text(), $format)"/>
              <xsl:variable name="e"
                            select="format-number(./gmd:eastBoundLongitude/gco:Decimal/text(), $format)"/>
              <xsl:variable name="n"
                            select="format-number(./gmd:northBoundLatitude/gco:Decimal/text(), $format)"/>
              <xsl:variable name="s"
                            select="format-number(./gmd:southBoundLatitude/gco:Decimal/text(), $format)"/>
              <xsl:choose>
                <xsl:when test="$e = $w and $s = $n">
                  <location><xsl:value-of select="concat($s, ',', $w)"/></location>
                </xsl:when>
                <xsl:when
                  test="($e = $w and $s != $n) or ($e != $w and $s = $n)">
                  <!-- Probably an invalid bbox indexing a point only -->
                  <location><xsl:value-of select="concat($s, ',', $w)"/></location>
                </xsl:when>
                <xsl:otherwise>
                  <location><xsl:value-of select="concat(
                                            (number($s) + number($n)) div 2,
                                            ',',
                                            (number($w) + number($e)) div 2)"/></location>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:for-each>
          </xsl:if>
803
804
          <xsl:if test="count(.//gmd:EX_BoundingPolygon/gmd:polygon/gml:Polygon)>0 and count(.//gmd:EX_BoundingPolygon/gmd:polygon/gml:Point)=0">
            <geojson>
805
806
807
            <xsl:variable name="isMulti">
              <xsl:choose>
                <xsl:when test="count(.//gmd:EX_BoundingPolygon/gmd:polygon/gml:Polygon) = 1">
Kim Mortimer's avatar
Closer!    
Kim Mortimer committed
808
                  <xsl:text>false</xsl:text>
809
810
                </xsl:when>
                <xsl:otherwise>
Kim Mortimer's avatar
Closer!    
Kim Mortimer committed
811
                  <xsl:text>true</xsl:text>
812
813
814
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
815
816
817
818
819
            <xsl:choose>
            <xsl:when test="count(.//gmd:EX_BoundingPolygon/gmd:polygon/gml:Polygon) = 1">
              <xsl:text>{"type": "Polygon",</xsl:text>
                <xsl:text>"coordinates": [ 
                </xsl:text>
820
            </xsl:when>                
821
822
            <xsl:otherwise>
                <xsl:text>{"type": "MultiPolygon",</xsl:text>
823
                <xsl:text>"coordinates": [ <!-- curly brace opened, 1 square brackets opened -->
824
825
826
827
828
                </xsl:text>
            </xsl:otherwise>
            </xsl:choose>
            <xsl:for-each select=".//gmd:EX_BoundingPolygon/gmd:polygon/gml:Polygon">
              <xsl:if test="ends-with(@srsName, '84') or ends-with(@srsName, '4326') or not(@srsName)">
829
                <xsl:if test="$isMulti = 'true'">
830
831
                  <xsl:text>[</xsl:text> <!-- wraps the entire polygon component in a square bracket -->
                </xsl:if>
832
             <!--if it's not in WGS 84 we can't convert to geoJson
833
834
               if it's a polygon it has an exterior
               initialize geoJson -->
835
                <xsl:text>[</xsl:text><!-- square bracket wrapping exterior -->                
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
                <xsl:choose>
                   <!--determine what type of coordinates are being used-->
                  <xsl:when test="./gml:exterior/gml:LinearRing/gml:pos">

                    <!--read each position in the list 
                     TODO: Figure out how to compute handedness of the polygon 
                     also N.B. that a WGS 84 object is going to be in latitude, longitude but GeoJSON expects longitude, latitude 
                    - if it complains about separator we need to try an XSLT-1.0 implementation, I'm hopeful it might be 2.0 -->
                    <xsl:for-each select="gml:exterior/gml:LinearRing/gml:pos">
                      <xsl:value-of select="concat('[', substring-before(. ,'\s'), ', ', substring-after(., '\s'), ']')" separator=","/>
                    </xsl:for-each>
                  </xsl:when>
                  <xsl:when test="./gml:exterior/gml:LinearRing/gml:pointProperty">
                     <!--pretty much the same as the above except deeper inside 
                     TODO: Figure out how to compute handedness of the polygon -->
                    <xsl:for-each select="gml:exterior/gml:LinearRing/gml:pointProperty/gml:Point/gml:pos">
                      <xsl:value-of select="concat('[', substring-before(. ,'\s'), ', ', substring-after(., '\s'), ']')" separator=","/>
                    </xsl:for-each>
                  </xsl:when>
                  <xsl:when test="./gml:exterior/gml:LinearRing/gml:posList">                                
                    <!--This is an unbroken list of coordinates so there is only one value to get -->
                    <xsl:variable name="tokenizedPos" as="xs:string*" select="tokenize(gml:exterior/gml:LinearRing/gml:posList, '\s')"/>
                    <xsl:value-of 
                      select="
                      substring(string-join(MER:ByTupleEncloseWithBracket($tokenizedPos), ' '),1, string-length(string-join(MER:ByTupleEncloseWithBracket($tokenizedPos), ' '))-1)
                      "/>   
                  </xsl:when>
                  <xsl:when test="./gml:LinearRing/gml:coordinates">
                    <xsl:variable name="tokenizedCoord" as="xs:string*" select="tokenize(gml:LinearRing/gml:coordinates, '\s')"/>
                    <xsl:for-each select="tokenizedCoord">
                    <xsl:value-of select="concat('[', . , ']')" separator=","/>
                    </xsl:for-each>
                  </xsl:when>
                </xsl:choose>
                <xsl:text>]</xsl:text> <!-- closes exterior -->
                <xsl:if test="./gml:interior">  <!--ie, there is a hole -->
                  <xsl:text>,</xsl:text>
                  <xsl:for-each select="gml:interior">
                    <xsl:text>[</xsl:text> <!-- wrapping interior -->
Kim Mortimer's avatar
Kim Mortimer committed
875
                    <xsl:choose>
876
                      <xsl:when test="./gml:LinearRing/gml:pos">                                            
877
878
879
880
                        <xsl:for-each select="gml:LinearRing/gml:pos">
                          <xsl:value-of select="concat('[', substring-before(. ,'\s'), ', ', substring-after(., '\s'), ']')" separator=","/>
                        </xsl:for-each>
                      </xsl:when>
881
                      <xsl:when test="./gml:LinearRing/gml:pointProperty">                                            
882
883
884
885
                        <xsl:for-each select="gml:LinearRing/gml:pointProperty/gml:Point/gml:pos">
                          <xsl:value-of select="concat('[', substring-before(. ,'\s'), ', ', substring-after(., '\s'), ']')" separator=","/>
                        </xsl:for-each>
                      </xsl:when>
886
                      <xsl:when test="./gml:LinearRing/gml:posList">                                            
887
888
889
890
891
892
                        <xsl:variable name="tokenizedPos" as="xs:string*" select="tokenize(gml:LinearRing/gml:posList, '\s')"/>
                          <xsl:value-of 
                          select="
                          substring(string-join(MER:ByTupleEncloseWithBracket($tokenizedPos), ' '),1,string-length(string-join(MER:ByTupleEncloseWithBracket($tokenizedPos), ' '))-1)
                          "/>
                      </xsl:when>
893
                      <xsl:when test="./gml:LinearRing/gml:coordinates">                                          
894
895
896
                        <xsl:variable name="tokenizedCoord" as="xs:string*" select="tokenize(gml:LinearRing/gml:coordinates, '\s')"/>
                        <xsl:for-each select="tokenizedCoord">
                        <xsl:value-of select="concat('[', . , ']')" separator=", "/>
897
                        </xsl:for-each>                                          
898
                      </xsl:when>
Kim Mortimer's avatar
Kim Mortimer committed
899
                    </xsl:choose>
900
901
902
903
904
905
906
907
                    <xsl:text>]</xsl:text> <!-- closing interior -->
                    <xsl:if test="position() != last()">
                      <xsl:text>,</xsl:text> <!-- if there are more interiors, need to add a comma-->
                    </xsl:if>
                  </xsl:for-each>
                </xsl:if>
              <xsl:if test="$isMulti = 'true'">
                <xsl:text>]</xsl:text> <!-- closes the wrapping -->
908
              </xsl:if>
909
910
              <xsl:if test="position() != last() and $isMulti = 'true'">
                <xsl:text>,</xsl:text> <!-- adds a comma to allow the next polygon -->
911
              </xsl:if>                                        
912
913
914
915
916
917
918
              </xsl:if>
            </xsl:for-each>
            <xsl:text>]}</xsl:text> <!-- ends the polygon wrapping as a whole -->
          </geojson>
          </xsl:if>
          <!-- how to determine the average location of a poylgon? -->
          <xsl:if test="count(.//gmd:EX_BoundingPolygon/gmd:polygon/gml:Point)>0 and count(.//gmd:EX_BoundingPolygon/gmd:polygon/gml:Polygon)=0">
919
            <geojson>
920
921
              <xsl:variable name="isMulti">
                <xsl:choose>
Kim Mortimer's avatar
Closer!    
Kim Mortimer committed
922
923
                  <xsl:when test="count(.//gmd:EX_BoundingPolygon/gmd:polygon/gml:Point) = 1">
                    <xsl:text>false</xsl:text>
924
925
                  </xsl:when>
                  <xsl:otherwise>
Kim Mortimer's avatar
Closer!    
Kim Mortimer committed
926
                    <xsl:text>true</xsl:text>
927
928
929
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:variable>
930
931
              <xsl:choose>
                <xsl:when test="count(.//gmd:EX_BoundingPolygon/gmd:polygon/gml:Point) = 1">
932
                <xsl:text>{"type": "Point",</xsl:text>
933
                  <xsl:text>"coordinates":</xsl:text>
934
                </xsl:when>                
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
                <xsl:otherwise>
                  <xsl:text>{"type": "MultiPoint",</xsl:text>
                  <xsl:text>"coordinates": [</xsl:text> <!-- curly brace opened, 1 or 2 square brackets opened -->
                </xsl:otherwise>
              </xsl:choose>
              <xsl:for-each select=".//gmd:EX_BoundingPolygon/gmd:polygon/gml:Point">
                <xsl:if test="ends-with(@srsName, '84') or ends-with(@srsName, '4326') or not(@srsName)">
                  <xsl:value-of select="concat('[', substring-before(./gml:pos,'&#32;'), ', ', substring-after(./gml:pos, '&#32;'), ']')"/>
                  <xsl:if test="position() != last() and $isMulti = 'true'">
                    <xsl:text>,</xsl:text>
                  </xsl:if>
                </xsl:if>                 
              </xsl:for-each>
              <xsl:if test="$isMulti = 'true'">
                <xsl:text>]</xsl:text>
              </xsl:if>
              <xsl:text>}</xsl:text>
952
            </geojson>
953
954
955
            <xsl:for-each select=".//gmd:EX_BoundingPolygon/gmd:polygon/gml:Point">
              <xsl:if test="ends-with(@srsName, '84') or ends-with(@srsName, '4326') or not(@srsName)">   
                <location><xsl:value-of select="concat(
956
957
958
                                              number(substring-before(./gml:pos,'&#32;')),
                                              ',',
                                              number(substring-before(./gml:pos,'&#32;')))"/></location>
959
960
961
              </xsl:if>
            </xsl:for-each>
          </xsl:if>
962
          
963
          <xsl:for-each select="./gmd:temporalElement">
964
            <textTemprlElement>
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
              <xsl:for-each select=".//gml:TimeInstant">
                <xsl:text>Instant in time </xsl:text>
                <xsl:if test="./gml:timePosition[@indeterminatePosition]">
                  <xsl:value-of select="./gml:timePosition/@indeterminatePosition"/><xsl:text> </xsl:text> 
                </xsl:if>
                <xsl:value-of select="./gml:timePosition"/>
              </xsl:for-each>
              <xsl:for-each select=".//gml:TimePeriod">
                <xsl:text>Time period from </xsl:text>
                <xsl:if test="./gml:beginPosition[@indeterminatePosition]">
                  <xsl:value-of select="./gml:beginPosition/@indeterminatePosition"/><xsl:text> </xsl:text> 
                </xsl:if>
                <xsl:value-of select="./gml:beginPosition"/>
                <xsl:text> to </xsl:text>
                <xsl:if test="./gml:endPosition[@indeterminatePosition]">
                  <xsl:value-of select="./gml:endPosition/@indeterminatePosition"/><xsl:text> </xsl:text> 
                </xsl:if>
                <xsl:value-of select="./gml:endPosition"/>
              </xsl:for-each>
984
            </textTemprlElement>
Kim Mortimer's avatar
Kim Mortimer committed
985
          </xsl:for-each>-->
986
        </xsl:for-each>
987
        
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
        <!-- Service information -->
        <xsl:for-each select="srv:serviceType/gco:LocalName">
          <serviceType>
            <xsl:value-of select="text()"/>
          </serviceType>
          <xsl:variable name="inspireServiceType" as="xs:string"
                        select="text()"/>
          <xsl:if test="$inspireServiceType != ''">
            <inspireServiceType>
              <xsl:value-of select="lower-case($inspireServiceType)"/>
            </inspireServiceType>
          </xsl:if>
          <xsl:if test="following-sibling::srv:serviceTypeVersion">
            <serviceTypeAndVersion>
              <xsl:value-of select="concat(
                        text(),
                        $separator,
                        following-sibling::srv:serviceTypeVersion/gco:CharacterString/text())"/>
            </serviceTypeAndVersion>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>


      <xsl:for-each select="gmd:referenceSystemInfo/gmd:MD_ReferenceSystem">
        <xsl:for-each select="gmd:referenceSystemIdentifier/gmd:RS_Identifier">
          <xsl:variable name="crs" select="gmd:code/gco:CharacterString"/>

          <xsl:if test="$crs != ''">
            <coordinateSystem>
              <xsl:value-of select="$crs"/>
            </coordinateSystem>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>


      <!-- INSPIRE Conformity -->

      <!-- Conformity for services -->
      <xsl:choose>
        <xsl:when test="$isService">
          <xsl:for-each-group select="gmd:dataQualityInfo/*/gmd:report"
                              group-by="*/gmd:result/*/gmd:specification/gmd:CI_Citation/
1032
                                      gmd:title/gco:CharacterString">
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
            <xsl:variable name="title" select="current-grouping-key()"/>
            <xsl:variable name="matchingEUText"
                          select="if ($inspireRegulationLaxCheck)
                                  then daobs:search-in-contains($eu9762009/*, $title)
                                  else daobs:search-in($eu9762009/*, $title)"/>
            <xsl:if test="count($matchingEUText) = 1">
              <xsl:variable name="pass"
                            select="*/gmd:result/*/gmd:pass/gco:Boolean"/>
              <inspireConformResource>
                <xsl:value-of select="$pass"/>
              </inspireConformResource>
            </xsl:if>
          </xsl:for-each-group>
        </xsl:when>
        <xsl:otherwise>
          <!-- Conformity for dataset -->
          <xsl:for-each-group select="gmd:dataQualityInfo/*/gmd:report"
                              group-by="*/gmd:result/*/gmd:specification/gmd:CI_Citation/
1051
                                    gmd:title/gco:CharacterString">
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
            <xsl:variable name="title" select="current-grouping-key()"/>
            <xsl:variable name="matchingEUText"
                          select="if ($inspireRegulationLaxCheck)
                                  then daobs:search-in-contains($eu10892010/*, $title)
                                  else daobs:search-in($eu10892010/*, $title)"/>

            <xsl:if test="count($matchingEUText) = 1">
              <xsl:variable name="pass"
                            select="*/gmd:result/*/gmd:pass/gco:Boolean"/>
              <inspireConformResource>
                <xsl:value-of select="$pass"/>
              </inspireConformResource>
            </xsl:if>
          </xsl:for-each-group>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:for-each-group select="gmd:dataQualityInfo/*/gmd:report"
                          group-by="*/gmd:result/*/gmd:specification/
                                      */gmd:title/gco:CharacterString">
        <xsl:variable name="title" select="current-grouping-key()"/>
        <xsl:variable name="pass" select="*/gmd:result/*/gmd:pass/gco:Boolean"/>
        <xsl:if test="$pass">
          <xsl:element name="conformTo_{replace(normalize-space($title), '[^a-zA-Z0-9]', '')}">
            <xsl:value-of select="$pass"/>
          </xsl:element>
        </xsl:if>
      </xsl:for-each-group>

      <xsl:for-each select="gmd:dataQualityInfo/*">

        <xsl:for-each select="gmd:lineage/gmd:LI_Lineage/
                                gmd:statement/gco:CharacterString[. != '']">
          <lineage>
            <xsl:value-of select="."/>
          </lineage>
        </xsl:for-each>

        <!-- Indexing measure value -->
        <xsl:for-each select="gmd:report/*[
                normalize-space(gmd:nameOfMeasure[0]/gco:CharacterString) != '']">
          <xsl:variable name="measureName"
                        select="replace(
                                normalize-space(
Kim Mortimer's avatar
Kim Mortimer committed
1095
                                  gmd:nameOfMeasure[0]/gco:CharacterString), '\s', '-')"/>
1096
1097
1098
1099
1100
1101
1102
1103
1104
          <xsl:for-each select="gmd:result/gmd:DQ_QuantitativeResult/gmd:value">
            <xsl:if test=". != ''">
              <xsl:element name="measure_{replace($measureName, '[^a-zA-Z0-9]', '')}">
                <xsl:value-of select="."/>
              </xsl:element>
            </xsl:if>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
1105
        
1106
1107
1108
1109
      <xsl:for-each select="gmd:distributionInfo/*">
        <xsl:for-each
          select="gmd:distributionFormat/*/gmd:name/gco:CharacterString">
          <format>
1110
            <xsl:value-of select="."/> (Version <xsl:value-of select="../../gmd:version/gco:CharacterString"/>)
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
          </format>
        </xsl:for-each>

        <xsl:for-each select="gmd:transferOptions/*/
                                gmd:onLine/*[gmd:linkage/gmd:URL != '']">

          <xsl:variable name="protocol" select="gmd:protocol/gco:CharacterString/text()"/>

          <linkUrl>
            <xsl:value-of select="gmd:linkage/gmd:URL"/>
          </linkUrl>
          <linkProtocol>
            <xsl:value-of select="$protocol"/>
          </linkProtocol>
          <link>
            <xsl:value-of select="gmd:protocol/*/text()"/>
            <xsl:text>|</xsl:text>
            <xsl:value-of select="gmd:linkage/gmd:URL"/>
            <xsl:text>|</xsl:text>
            <xsl:value-of select="gmd:name/*/text()"/>
            <xsl:text>|</xsl:text>
            <xsl:value-of select="gmd:description/*/text()"/>
          </link>

          <xsl:if test="$operatesOnSetByProtocol and normalize-space($protocol) != ''">
            <xsl:if test="daobs:contains($protocol, 'wms')">
              <recordOperatedByType>view</recordOperatedByType>
            </xsl:if>
            <xsl:if test="daobs:contains($protocol, 'wfs') or
                          daobs:contains($protocol, 'wcs') or
                          daobs:contains($protocol, 'download')">
              <recordOperatedByType>download</recordOperatedByType>
            </xsl:if>
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>
1147
1148
1149
1150
1151
      <xsl:for-each select="gmd:metadataMaintenance/gmd:MD_MaintenanceInformation/gmd:maintenanceNote">
        <versionInfo>
            <xsl:value-of select="./gco:CharacterString"/>
        </versionInfo>
      </xsl:for-each>
1152
      <xsl:for-each select="gmi:acquisitionInformation/*">
1153
        <xsl:for-each select=".//gmi:MI_Operation">
1154
1155
1156
1157
1158
1159
          <operationStatus><xsl:value-of select="./gmi:status/gmd:MD_ProgressCode[text()]"/></operationStatus>
          <operationIdentifier><xsl:value-of select="./gmi:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString"/></operationIdentifier>
          <operationTitle><xsl:value-of select="./gmi:citation/gmd:CI_Citation/gmd:title/gco:CharacterString"/></operationTitle>
          
          <xsl:for-each select="./gmi:citation/gmd:CI_Citation/gmd:date/gmd:CI_Date[gmd:date/*/text() != '' and
                                matches(gmd:date/*/text(), '[0-9]{4}.*')]">
1160
1161
1162
1163
1164
            <xsl:variable name="dateType"
                          select="gmd:dateType[1]/gmd:CI_DateTypeCode[text()]"
                          as="xs:string?"/>
            <xsl:variable name="date"
                          select="string(gmd:date[1]/gco:Date|gmd:date[1]/gco:DateTime)"/>
1165
            <operationDteType><xsl:value-of select="$dateType"/></operationDteType>
1166
            <operationDate><xsl:value-of select="$date"/></operationDate>            
1167
1168
1169
          </xsl:for-each>
          
          <xsl:apply-templates mode="index-contact" select="gmi:citation/gmd:CI_Citation/gmd:citedResponsibleParty">
Kim Mortimer's avatar
Kim Mortimer committed
1170
            <xsl:with-param name="fieldSuffix" select="'ForOperation'"/>
1171
          </xsl:apply-templates>
1172
1173
        </xsl:for-each>       
        
1174
        <xsl:for-each select=".//gmi:MI_Platform">
1175
1176
1177
          <platformIdentifier><xsl:value-of select="./gmi:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString"/></platformIdentifier>
          <platformDescription><xsl:value-of select="./gmi:description/gco:CharacterString"/></platformDescription>
          <xsl:apply-templates mode="index-contact" select="gmi:sponsor">
Kim Mortimer's avatar
Kim Mortimer committed
1178
            <xsl:with-param name="fieldSuffix" select="'ForPlatform'"/>
1179
          </xsl:apply-templates>
1180
1181
        </xsl:for-each>        
        
1182
       <xsl:for-each select=".//gmi:MI_Instrument|.//MER:MI_Instrument">
1183
1184
1185
1186
1187
1188
          <instrumentIdentifier><xsl:value-of select="./gmi:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString"/></instrumentIdentifier>
          <instrumentType><xsl:value-of select="./gmi:type/gco:CharacterString"/></instrumentType>
          <xsl:for-each select="./MER:history/MER:MI_InstrumentationEventList/MER:instrumentationEvent">
            <instrumentEventType><xsl:value-of select="../../../gmi:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString"/>|<xsl:value-of select="./MER:MI_InstrumentEvent/MER:type/MER:MI_EventTypeCode"/></instrumentEventType>
            <instrumentEventDesc><xsl:value-of select="../../../gmi:identifier/gmd:MD_Identifier/gmd:code/gco:CharacterString"/>|<xsl:value-of select="./MER:MI_InstrumentEvent/MER:description/gco:CharacterString"/></instrumentEventDesc>
          </xsl:for-each>
1189
        </xsl:for-each>      
1190
      </xsl:for-each>
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
      <!-- Service/dataset relation. Create document for the association.
      Note: not used for indicators anymore
       This could be used to retrieve :
      {!child of=documentType:metadata}+documentType:metadata +id:9940c446-6fd4-4ab3-a4de-7d0ee028a8d1
      {!child of=documentType:metadata}+documentType:metadata +resourceType:service +serviceType:view
      {!child of=documentType:metadata}+documentType:metadata +resourceType:service +serviceType:download
       -->
      <xsl:for-each
        select="gmd:identificationInfo/srv:SV_ServiceIdentification/srv:operatesOn">
        <xsl:variable name="associationType" select="'operatesOn'"/>
        <xsl:variable name="serviceType"
                      select="../srv:serviceType/gco:LocalName"/>
        <!--<xsl:variable name="relatedTo" select="@uuidref"/>-->
        <xsl:variable name="getRecordByIdId">
          <xsl:if test="@xlink:href != ''">
            <xsl:analyze-string select="@xlink:href"
                                regex=".*[i|I][d|D]=([\w\-\.\{{\}}]*).*">
              <xsl:matching-substring>
                <xsl:value-of select="regex-group(1)"/>
              </xsl:matching-substring>
            </xsl:analyze-string>
          </xsl:if>
        </xsl:variable>

        <xsl:variable name="datasetId">
          <xsl:choose>
            <xsl:when test="$getRecordByIdId != ''">
              <xsl:value-of select="$getRecordByIdId"/>
            </xsl:when>
            <xsl:when test="@uuidref != ''">
              <xsl:value-of select="@uuidref"/>
            </xsl:when>
          </xsl:choose>
        </xsl:variable>

        <xsl:if test="$datasetId != ''">
          <recordOperateOn>
            <xsl:value-of select="$datasetId"/>
          </recordOperateOn>
        </xsl:if>
      </xsl:for-each>

      <!-- Index more fields in this element -->
      <xsl:apply-templates mode="index-extra-fields" select="."/>
    </doc>

    <!-- Index more documents for this element -->
    <xsl:apply-templates mode="index-extra-documents" select="."/>
  </xsl:template>

  <xsl:template mode="index-contact" match="*[gmd:CI_ResponsibleParty]">
    <xsl:param name="fieldSuffix" select="''" as="xs:string"/>

    <!-- Select the first child which should be a CI_ResponsibleParty.
    Some records contains more than one CI_ResponsibleParty which is
    not valid and they will be ignored.
     Same for organisationName eg. de:b86a8604-bf78-480f-a5a8-8edff5586679 -->
Kim Mortimer's avatar
Kim Mortimer committed
1248
1249
    <xsl:variable name="organisationName" select="*[1]/gmd:organisationName[1]/(gco:CharacterString|gmx:Anchor)" as="xs:string*"/>
    <xsl:variable name="individualName" select="*[1]/gmd:individualName[1]/(gco:CharacterString|gmx:Anchor)" as="xs:string*"/>
1250
    <xsl:variable name="role"
Kim Mortimer's avatar
Kim Mortimer committed
1251
                  select="replace(*[1]/gmd:role/*/@codeListValue, '\s', '')"
1252
                  as="xs:string?"/>
1253
    <xsl:variable name="emailAddress" select="*[1]/gmd:contactInfo[1]/gmd:CI_Contact[1]/gmd:address[1]/gmd:CI_Address[1]/gmd:electronicMailAddress[1]/(gco:CharacterString|gmx:Anchor)" as="xs:string*"/>
1254
1255
1256
    <xsl:variable name="phone" select="*[1]/gmd:contactInfo[1]/gmd:CI_Contact[1]/gmd:phone[1]/gmd:CI_Telephone[1]/gmd:voice[1]/(gco:CharacterString|gmx:Anchor)" as="xs:string*"/>
    <xsl:variable name="instructions" select="*[1]/gmd:contactInfo[1]/gmd:CI_Contact[1]/gmd:contactInstructions[1]/(gco:CharacterString|gmx:Anchor)" as="xs:string*"/>

1257
    <!--<xsl:if test="normalize-space($organisationName) != ''">-->
1258
1259
1260
1261
1262
1263
    <xsl:element name="Org{$fieldSuffix}">
      <xsl:value-of select="$organisationName"/>
    </xsl:element>
    <xsl:element name="Role{$fieldSuffix}">
      <xsl:value-of select="$role"/>
    </xsl:element>
1264
1265
    <!--</xsl:if>-->
    <!--<xsl:if test="normalize-space($individualName) != ''">-->
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
    <xsl:element name="Person{$fieldSuffix}">
      <xsl:value-of select="$individualName"/>
    </xsl:element>
    <xsl:element name="Email{$fieldSuffix}">
      <xsl:value-of select="$emailAddress"/>
    </xsl:element>
    <xsl:element name="Phone{$fieldSuffix}">
      <xsl:value-of select="$phone"/>
    </xsl:element>
    <xsl:element name="Instructions{$fieldSuffix}">
      <xsl:value-of select="$instructions"/>
    </xsl:element>
1278
    <!--</xsl:if>-->   
1279
1280
    <!--<xsl:choose>
      <xsl:when test="normalize-space($organisationName) != '' and normalize-space($individualName) != '' and normalize-space($emailAddress) != ''">-->
1281
1282
1283
1284
1285
1286
1287
1288
1289
    <xsl:element name="contact{$fieldSuffix}">{
      "person":"<xsl:value-of select="replace($individualName, '&quot;', '\\&quot;')"/>",
      "org":"<xsl:value-of select="replace($organisationName, '&quot;', '\\&quot;')"/>",
      "role":"<xsl:value-of select="$role"/>",
      "email":"<xsl:value-of select="$emailAddress"/>",
      "phone":"<xsl:value-of select="$phone"/>",
      "instructions":"<xsl:value-of select="$instructions"/>"
      }
    </xsl:element> 
1290
     <!-- </xsl:when>
1291
1292
      <xsl:when test="normalize-space($organisationName) != '' and normalize-space($individualName) = '' and normalize-space($emailAddress) != ''">
        <xsl:element name="contact{$fieldSuffix}">{
1293
1294
1295
        "org":"<xsl:value-of select="replace($organisationName, '&quot;', '\\&quot;')"/>",
        "role":"<xsl:value-of select="$role"/>",
        "email":"<xsl:value-of select="$emailAddress"/>"
1296
1297
        }</xsl:element> 
      </xsl:when>
1298
      <xsl:when test="normalize-space($organisationName) != '' and normalize-space($individualName) = '' and normalize-space($emailAddress) = ''">
1299
        <xsl:element name="contact{$fieldSuffix}">{
1300
1301
        "org":"<xsl:value-of select="replace($organisationName, '&quot;', '\\&quot;')"/>",
        "role":"<xsl:value-of select="$role"/>"
1302
1303
        }</xsl:element> 
      </xsl:when>
1304
      <xsl:when test="normalize-space($organisationName) != '' and normalize-space($individualName) != '' and normalize-space($emailAddress) = ''">
1305
        <xsl:element name="contact{$fieldSuffix}">{
1306
1307
1308
        "person":"<xsl:value-of select="replace($individualName, '&quot;', '\\&quot;')"/>",
        "org":"<xsl:value-of select="replace($organisationName, '&quot;', '\\&quot;')"/>",
        "role":"<xsl:value-of select="$role"/>"
1309
        }</xsl:element> 
1310
      </xsl:when>
1311
      <xsl:when test="normalize-space($organisationName) = '' and normalize-space($individualName) != '' and normalize-space($emailAddress) != ''">
1312
        <xsl:element name="contact{$fieldSuffix}">{
1313
1314
1315
        "person":"<xsl:value-of select="replace($individualName, '&quot;', '\\&quot;')"/>",
        "role":"<xsl:value-of select="$role"/>",
        "email":"<xsl:value-of select="$emailAddress"/>"
1316
        }</xsl:element> 
1317
1318
1319
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="contact{$fieldSuffix}">{
1320
1321
        "person":"<xsl:value-of select="replace($individualName, '&quot;', '\\&quot;')"/>",
        "role":"<xsl:value-of select="$role"/>"
1322
1323
        }</xsl:element>
      </xsl:otherwise>
1324
    </xsl:choose>-->
1325
  </xsl:template>
Kim Mortimer's avatar
Kim Mortimer committed
1326

1327
</xsl:stylesheet>