sylar_80
sylar_80

Reputation: 375

xslt extract numeric value from a node using substring

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>

Upvotes: 0

Views: 577

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116992

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>

Upvotes: 1

Related Questions