0

I have the following lines in my .xml file

<metadataObject ID="measurementFrameSet" classification="DESCRIPTION" category="DMD">
  <metadataWrap mimeType="text/xml" vocabularyName="SAFE" textInfo="Frame Set">
    <xmlData>
      <safe:frameSet>
        <safe:frame>
          <safe:footPrint srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
            <gml:coordinates>43.838726,8.275868 44.232952,11.408423 42.557594,11.770112 42.163200,8.725094</gml:coordinates>
          </safe:footPrint>
        </safe:frame>
      </safe:frameSet>
    </xmlData>
   </metadataWrap>

I'm able to read the entire node

<xsl:for-each select="//metadataSection/metadataObject/metadataWrap/xmlData/safe:frameSet/safe:frame/safe:footPrint" >
    <xsl:value-of select="gml:coordinates" />
</xsl:for-each>

but I would like to extract only the following numeric values in the "gml:coordinates" node in a separate way: 43.838726,8.275868 42.557594,11.770112 because in my final xml they will be inserted in to separate field.

Is there a way with xslt to obtain a substring from that node ? the final xml should look like this:

<gmd:eastBoundLongitude>
    <gco:Decimal>
      43.838726
    </gco:Decimal>
</gmd:eastBoundLongitude>
sylar_80
  • 251
  • 3
  • 18
  • **1.** If you want to extract four coordinates, why does your output show only one? **2.** How exactly does one identify the coordinates to extract? **3.** Can you use XSLTT 2.0? If not, which XSLT 1.0 processor will you be using? – michael.hor257k Aug 02 '16 at 09:43
  • P.S. Do a search for `tokenize`. – michael.hor257k Aug 02 '16 at 09:46
  • Hi Michael, 1.the output is showing only one because it was an example of the four coordinates that will be present in the final xml: westBoundLongitude, southBoundLongitude, northBoundLongitude. sorry for not beeing enough clear. 2. the format of the input xml file is always the same, as a consequence, Iwas thinking to extract substring considering the position of the numeric values; 3. I'm using I'm using a WINDOWS machine with ECLIPSE Version: Mars.2 Release (4.5.2) – sylar_80 Aug 02 '16 at 10:05
  • Re #3: please identify the specific processor - see here how: http://stackoverflow.com/questions/25244370/how-can-i-check-which-xslt-processor-is-being-used-in-solr/25245033#25245033 – michael.hor257k Aug 02 '16 at 10:07
  • Also please amend your XML input so that we can see the namespaces associated with the `safe:` and `gml:` prefixes. – michael.hor257k Aug 02 '16 at 10:10
  • Hi michael, I'm using XSLT 2.0 and in my sxl file I already inserted the namespaces _safe:_ and _gml:_ thanks a lot! – sylar_80 Aug 02 '16 at 12:47

1 Answers1

1

Here's a very simple (not to say primitive) way to extract the wanted coordinates:

<xsl:template match="/">
    <xsl:for-each select="//safe:footPrint" >
        <xsl:variable name="set1" select="substring-before(gml:coordinates, ' ')" />
        <xsl:variable name="set3" select="substring-before(substring-after(substring-after(gml:coordinates, ' '), ' '), ' ')" />
        <output>
            <coor1A>
                <xsl:value-of select="substring-before($set1, ',')" />
            </coor1A>
            <coor1B>
                <xsl:value-of select="substring-after($set1, ',')" />
            </coor1B>
            <coor3A>
                <xsl:value-of select="substring-before($set3, ',')" />
            </coor3A>
            <coor3B>
                <xsl:value-of select="substring-after($set3, ',')" />
            </coor3B>
        </output>
    </xsl:for-each>
</xsl:template>

Of course, your stylesheet must contain namespace declarations for the safe: and gml: prefixes (as must your XML input).

And to produce an output in the format shown, you must also bind the gmd: and gco: prefixes to their namespace URIs.


Added:

I'm using XSLT 2.0

In XSLT 2.0, you could do it much more elegantly as:

<xsl:template match="/">
    <xsl:for-each select="//safe:footPrint" >
        <xsl:variable name="coordinates" select="tokenize(gml:coordinates, '\s|,')" />
        <output>
            <coor1A>
                <xsl:value-of select="$coordinates[1]" />
            </coor1A>
            <coor1B>
                <xsl:value-of select="$coordinates[2]" />
            </coor1B>
            <coor3A>
                <xsl:value-of select="$coordinates[5]" />
            </coor3A>
            <coor3B>
                <xsl:value-of select="$coordinates[6]" />
            </coor3B>
        </output>
    </xsl:for-each>
</xsl:template>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51