CloudSurferUK
CloudSurferUK

Reputation: 95

Split Coordinates in XSLT

I have an XML node of coordinates which contains the fully geolocated lat/long combination. However on a new system it must be sent as individual nodes. The XMl is transfomed with XSLT before its sent so I was wondering how i could effectively seperate it into the component parts.

XML Node

<coordinates>-3.166610, 51.461231</coordinates>

I need to transform into:

<latitude>-3.166610</latitude>
<longitude>51.461231</longitude>

Thanks. Oh, should mention its XSLT 1.0

Upvotes: 1

Views: 194

Answers (1)

kjhughes
kjhughes

Reputation: 111591

As Ian commented, substring-before and substring-after can handle this for you:

This XML:

<coordinates>-3.166610, 51.461231</coordinates>

Given to this XSLT transformation:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="coordinates">
    <xsl:copy>
      <latitude>
        <xsl:value-of select="normalize-space(substring-before(., ','))"/>
      </latitude>
      <longitude>
        <xsl:value-of select="normalize-space(substring-after(., ','))"/>
      </longitude>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Will produce the desired output XML:

<?xml version="1.0" encoding="UTF-8"?>
<coordinates>
   <latitude>-3.166610</latitude>
   <longitude>51.461231</longitude>
</coordinates>

Upvotes: 2

Related Questions