From 9a028b9d5d2111419418355d833cb8bc09332e04 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Thu, 9 Jan 2025 20:45:11 +0100 Subject: [PATCH] support all xml namespaces --- gpx/src/io.ts | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/gpx/src/io.ts b/gpx/src/io.ts index ce0e8c78..65c074c2 100644 --- a/gpx/src/io.ts +++ b/gpx/src/io.ts @@ -2,11 +2,29 @@ import { XMLParser, XMLBuilder } from "fast-xml-parser"; import { GPXFileType } from "./types"; import { GPXFile } from "./gpx"; +const attributesWithNamespace = { + 'RoutePointExtension': 'gpxx:RoutePointExtension', + 'rpt': 'gpxx:rpt', + 'TrackPointExtension': 'gpxtpx:TrackPointExtension', + 'PowerExtension': 'gpxpx:PowerExtension', + 'atemp': 'gpxtpx:atemp', + 'hr': 'gpxtpx:hr', + 'cad': 'gpxtpx:cad', + 'Extensions': 'gpxtpx:Extensions', + 'PowerInWatts': 'gpxpx:PowerInWatts', + 'power': 'gpxpx:PowerExtension', + 'line': 'gpx_style:line', + 'color': 'gpx_style:color', + 'opacity': 'gpx_style:opacity', + 'width': 'gpx_style:width', +}; + export function parseGPX(gpxData: string): GPXFile { const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "", attributesGroupName: 'attributes', + removeNSPrefix: true, isArray(name: string) { return name === 'trk' || name === 'trkseg' || name === 'trkpt' || name === 'wpt' || name === 'rte' || name === 'rtept' || name === 'gpxx:rpt'; }, @@ -17,22 +35,8 @@ export function parseGPX(gpxData: string): GPXFile { return attrValue; }, transformTagName(tagName: string) { - if (tagName === 'power') { - // Transform the simple tag to the more complex tag, the nested tag is then handled by the tagValueProcessor - return 'gpxpx:PowerExtension'; - } - // Add the gpx_style namespace to the line style tags - if (tagName === 'line') { - return 'gpx_style:line'; - } - if (tagName === 'color') { - return 'gpx_style:color'; - } - if (tagName === 'opacity') { - return 'gpx_style:opacity'; - } - if (tagName === 'width') { - return 'gpx_style:width'; + if (attributesWithNamespace[tagName]) { + return attributesWithNamespace[tagName]; } return tagName; },