1

I'm trying to create an XSLT for XML below. The purpose is to build the following URL. I'm trying to figure out how to loop through the XSLT in order to create the latitude,longitude| parings and then finish the url with &size=300x300&maptype=hybrid&sensor=false" /> when the "end" is reached.

<img alt="" src="http://maps.googleapis.com/maps/api/staticmap?
  path=color:0x0000ff|weight:5|42.312620297384676,-70.95182336425782
  |42.31230294498018,-70.95255292510987
  &amp;size=300x300&amp;maptype=hybrid&amp;sensor=false" />

There can be many timestamp parameters, but they will all have a type value of either "gps", "pause", "resume", or "manual". They will always begin with a type of "start" and end with a type of "end".

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root type="object">
  <path type="array">
    <item type="object">
      <timestamp type="number">0</timestamp>
      <altitude type="number">274.1666666666667</altitude>
      <longitude type="number">-84.387467</longitude>
      <latitude type="number">33.877038</latitude>
      <type type="string">start</type>
    </item>
    <item type="object">
      <timestamp type="number">3548.7729999999997</timestamp>
      <altitude type="number">269.2857142857143</altitude>
      <longitude type="number">-84.387616</longitude>
      <latitude type="number">33.876494</latitude>
      <type type="string">manual</type>
    </item>
    <item type="object">
      <timestamp type="number">3600</timestamp>
      <altitude type="number">270.8333333333333</altitude>
      <longitude type="number">-84.387498</longitude>
      <latitude type="number">33.877011</latitude>
      <type type="string">end</type>
    </item>
  </path>
  <calories type="array">
  </calories>
  <total_calories type="number">259</total_calories>
</root>

Result would be..

http://maps.googleapis.com/maps/api/staticmap?path=color:0x0000ff|weight:5|33.877038,-84.387467|33.876494,-84.387616|33.877011,-84.387498&size=300x300&maptype=hybrid&sensor=false

Any help would be appreciated.

2 Answers2

1

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pUrlHead" select=
 "'http://maps.googleapis.com/maps/api/staticmap?path=color:0x0000ff|weight:5'"/>

 <xsl:param name="pUrlTail" select=
 "'&amp;size=300x300&amp;maptype=hybrid&amp;sensor=false'"/>

 <xsl:template match="/*/path">
     <xsl:copy-of select="$pUrlHead"/>
     <xsl:apply-templates select="item[@type='object']"/>
     <xsl:copy-of select="$pUrlTail"/>
 </xsl:template>

 <xsl:template match="item">
  <xsl:value-of select="concat('|', latitude, ',', longitude)"/>
 </xsl:template>

 <xsl:template match="/*/*[not(self::path)]"/>
</xsl:stylesheet>

when applied on the provided XML document:

<root type="object">
  <path type="array">
    <item type="object">
      <timestamp type="number">0</timestamp>
      <altitude type="number">274.1666666666667</altitude>
      <longitude type="number">-84.387467</longitude>
      <latitude type="number">33.877038</latitude>
      <type type="string">start</type>
    </item>
    <item type="object">
      <timestamp type="number">3548.7729999999997</timestamp>
      <altitude type="number">269.2857142857143</altitude>
      <longitude type="number">-84.387616</longitude>
      <latitude type="number">33.876494</latitude>
      <type type="string">manual</type>
    </item>
    <item type="object">
      <timestamp type="number">3600</timestamp>
      <altitude type="number">270.8333333333333</altitude>
      <longitude type="number">-84.387498</longitude>
      <latitude type="number">33.877011</latitude>
      <type type="string">end</type>
    </item>
  </path>
  <calories type="array">
  </calories>
  <total_calories type="number">259</total_calories>
</root>

produces the wanted, correct result:

http://maps.googleapis.com/maps/api/staticmap?path=color:0x0000ff|weight:5|33.877038,-84.387467|33.876494,-84.387616|33.877011,-84.387498&size=300x300&maptype=hybrid&sensor=false259
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

As @Dimitre pointed out, the source data you are showing is not XML but looks like a fragment of JSON.

Did you really have some XML source data? or do you need to process the JSON?

If the latter, why are you trying to use XSLT? It seems like trying to use a screwdriver to drive a nail. If you must use XSLT, you could try this answer: XSLT equivalent for JSON

But to generate a series of URL parameters based on JSON input, a language like Javascript seems much more suitable.

Community
  • 1
  • 1
LarsH
  • 27,481
  • 8
  • 94
  • 152