forked from camptocamp/ogc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml-utils.ts
More file actions
123 lines (111 loc) · 3.23 KB
/
xml-utils.ts
File metadata and controls
123 lines (111 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { parseXml, XmlDocument, XmlElement, XmlText } from '@rgrove/parse-xml';
export class XmlParseError extends Error {
constructor(message) {
super(message);
}
}
/**
* Parses a XML document as string, return a document object
*/
export function parseXmlString(xmlString: string) {
let doc: XmlDocument = null;
try {
doc = parseXml(xmlString);
} catch (e) {
throw new XmlParseError(e.message);
}
return doc;
}
/**
* Will do nothing if no namespace present
* @param {string} name
* @return {string}
*/
export function stripNamespace(name) {
const colon = name.indexOf(':');
return colon > -1 ? name.substr(colon + 1) : name;
}
export function getRootElement(xmlDoc: XmlDocument) {
return xmlDoc.children[0] as XmlElement;
}
export function getElementName(element: XmlElement) {
return element.name || '';
}
/**
* Will return all matching elements (namespace will be ignored)
* @param element Element to look into
* @param name element name
* @param [nested] if true, will lookup children of children too
* @return Returns an empty array if no match found
*/
export function findChildrenElement(
element: XmlElement,
name: string,
nested: boolean = false
): XmlElement[] {
const strippedName = stripNamespace(name);
function reducer(prev, curr) {
if (stripNamespace(getElementName(curr)) === strippedName) {
prev.push(curr);
}
if (nested && Array.isArray(curr.children)) {
return [...prev, ...curr.children.reduce(reducer, [])];
} else {
return prev;
}
}
return element && Array.isArray(element.children)
? element.children.reduce(reducer, [])
: [];
}
/**
* Will return the first matching element
* @param element Element to look into
* @param name element name
* @param [nested] if true, will lookup children of children too
* @return Returns null if no matching element found
*/
export function findChildElement(
element: XmlElement,
name: string,
nested: boolean = false
) {
return (findChildrenElement(element, name, nested)[0] as XmlElement) || null;
}
/**
* Will return all children elements
* @param {XmlElement} element Element to look into
* @return {XmlElement[]} Returns empty array if no element found
*/
export function getChildrenElement(element: XmlElement) {
return element && Array.isArray(element.children)
? [
...(element.children.filter(
(el) => el instanceof XmlElement
) as XmlElement[]),
]
: [];
}
/**
* Returns the text node in the element. Note that giving an null element
* will simply return an empty string.
* @param element
* @return found text or empty string if no text node found
*/
export function getElementText(element: XmlElement) {
const textNode =
element && Array.isArray(element.children)
? (element.children.find((node) => node.type === 'text') as XmlText)
: null;
return textNode ? textNode.text : '';
}
/**
* Returns the element's attribute value. Note that giving an null element
* will simply return an empty string.
* @param element
* @param attrName
* @return found attribute value or empty if non-existent
*/
export function getElementAttribute(element: XmlElement, attrName: string) {
return (element && element.attributes[attrName]) || '';
}