Validating your GPX output

Why Validate?

Your application needs to produce valid GPX output before it can be used with other programs and web services that support GPX. If you are adding GPX output to your application, you MUST ensure that your output validates correctly. When a developer encounters a GPX file she can't parse, the first question should always be "Is this a valid GPX file?" The GPX validation test provides a consistant answer to this question.

Validating your GPX document

GPX is an XML-based format. A GPX document is an XML document with a root <gpx> element referencing the GPX namespace. To be a valid GPX document, the file must first be well-formed XML, and then it must conform to all of the rules in the GPX schema that define the legal elements and attributes that make a valid GPX document.

There are many online tools to check that an XML document is well-formed. Unfortunately, many of these are described as "XML Validators" when they are really XML syntax checkers. To truly validate a GPX file, you must use a tool that checks every aspect of the file against the core GPX schema, and any GPX extension schemas the file references.

Best Practices

GPX files should declare each namespace they reference, and also use xsi:schemaLocation to give the URLs of schema definitions for each of those namespaces. Unfortunately, many of the GPX files out there today do not properly fill in xsi:schemaLocation. This, combined with the "lax" processing of elements inside <extensions> means that a GPX validator won't necessarily give an error if the file contains an error in an extension element if there's no link to a valid extension schema.

To avoid this source of false positives when validating your application's output, you can replace the GPX 1.0 and GPX 1.1 schemas with a "strict" version which will enforce strict schema validation for any extensions. Only use these strict schemas for validating your output - don't reference them in your production GPX output.

GPX 1.0 strict schema: https://www.topografix.com/GPX/1/0/gpx-strict.xsd

GPX 1.1 strict schema: https://www.topografix.com/GPX/1/1/gpx-strict.xsd

Missing and Redirected Schemas

Some manufacturers create a GPX extension and namespace for their GPX output, but don't link to or provide a schema for that extension namespace. This makes it impossible to validate those extensions.

Other manufacturers properly reference a schemaLocation for their extensions, but over time, the location of that schema changes on their web server, and the original link to the .xsd is 301, 302, or 307-redirected or goes 404. Many online XML validators don't handle these redirections correctly, or interpret the XHTML in a "file not found" page as the XSD schema contents. This can lead to weird error messages. Online XML validators are not recommended for this reason.

An XML Validator which provides the ability to load missing or redirected extension schemas into a local schema cache is the best current solution for validating GPX files.

True XML Validators

XML Notepad - the recommended validation tool. XML Notebook will attempt to download any schemas listed in xsi:schemaLocation. You can override those schemas or add local versions of missing schemas through the schema cache (View menu, Schemas). If you are making changes to an extension schema that you are developing, be sure to refresh the schema cache and reload the GPX document.

https://www.freeformatter.com/xml-validator-xsd.html - a handy online tool for quickly checking a document. But it does not deal well with schemaLocation references that have moved (301 or 307 redirects) and can give strange error messages that mask the underlying issue.

https://www.truugo.com/xml_validator/ - similar issues

Visual Studio's code editor will attempt to validate GPX documents as you edit them.

Apache Xerces-C++ and Xerces2-Java tools like SAXCount or DOMCount. These tools don't deal well with schemaLocations that use https protocol or redirects. SaxCount.exe -v=always -n -s -f my_gpx_file.gpx If your file validates successfully, SAXCount will display a count of the elements in your file. Any other output from SAXCount.exe indicates that your GPX file is incorrect.

GPX Home