support all xml namespaces

This commit is contained in:
vcoppe
2025-01-09 20:45:11 +01:00
parent a502980a39
commit 9a028b9d5d

View File

@@ -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 <power> tag to the more complex <gpxpx:PowerExtension> tag, the nested <gpxpx:PowerInWatts> 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;
},