The Global Positioning of XML
XML is everywhere. If you don’t believe it, get a USB cable and connect a GPS device to a computer, then browse for any file with the extension .gpx. We quickly found a 2 MB file named Current.gpx on a Garmin GPS. Opening it in XMLSpy reveals XML data and an XML Schema assignment at the top.
Scrolling through the body of the file displays numerous <trkpt> tags that record latitude and longitude along with <ele> and <time> tags reporting the elevation, date and time at that location.
Any place you may go in the world, XML is there to tell you where you are.
Back at the top of the .gpx file, we can copy the URL for the XML Schema and open it in XMLSpy directly from the Web.
The GPX Schema opens in XMLSpy Schema view, where we can see it is well-documented with explanatory text. Double-clicking the root element opens the XMLSpy Content Model view.
This XML Schema describes a specialized data architecture using tags that are accessible for a casual user. Expanding the <trk> element drills down to definitions of the <trkseg> and <trkpt> elements we saw in Current.gpx. These are also documented in the XML Schema with clear explanations of their purpose.
The X in XML stands for eXtensible. The extensions element in the gpx XML Schema signals the developers knew their design would not suit all possible requirements. Back at the top of the Current.gpx file, we can see the assignment of extension schemas on line 2, and the metadata starting on line 3 indicates that Garmin International created extensions for this file.
The body of Current.gpx includes Garmin extensions to record speed in <gpxtpx:speed> and directional heading in <gpxtpx:course>.
Web sites like GPS Visualizer let you upload .gpx files and plot their data on Google maps, but when we uploaded the Current.gpx file, the map showed a confusing jumble of many overlapping and intersecting colored lines. A legend on the Web page listed nearly two dozen individual trip logs, identified by the words ACTIVE LOG and a date.
The legend entries all look suspiciously like line 83731 in the screenshot of partial contents of the .gpx file. Using XMLSpy to search for “ACTIVE LOG” verifies our file actually does include records for numerous road trips. Like many other car navigation and handheld devices, the Garmin GPS accumulates data over time in a single large file, with each individual trip encapsulated between <trk> and </trk>.
Altova MapForce is a convenient tool to split these large .gpx files into smaller files for each trip. We can assign the gpx.xsd XML schema for both the input and output side of a mapping and use the group-by function to split the Current.gpx file based on each new occurrence of the <trk> element, highlighted below by the red connector. We used the <name> element just below <trk> as the key to mark each break, and to construct each output filename, after stripping the “:” character that is illegal in filenames.
When we click the MapForce Output button to execute the mapping, MapForce generates a new output file each time <trk> is encountered in the input stream. The screenshot below shows file 14 of 20 total files created. Notice the schema assignment and metadata (lines 2-8) are included in each output file.
Now we can map any individual file at the GPS Visualizer Web site to see a single trip:
An application like this is handy for athletes like hikers, mountain climbers, bicyclists, or runners, as well as road trip adventurers who like to keep records of their journeys. More information about the GPS Exchange Format, the GPX schema, and a list of supporting resources is available at http://www.topografix.com/gpx.asp. If you’d like to try out XMLSpy, MapForce, or any other Altova tool, you can click here to download a free 30-day trial.
Editor’s Note: On April 10, 2012 we published an associated post titled Creating Elegant Reports for GPS XML Data that describes a StyleVision stylesheet to draw a graph of elevation changes recorded in the GPS XML trip log and render a report in HTML, RTF, PDF, and Microsoft Word formats.