mirror of
https://github.com/Kong/insomnia
synced 2024-11-12 17:26:32 +00:00
32 lines
785 B
JavaScript
32 lines
785 B
JavaScript
// @flow
|
|
import xpath from 'xpath';
|
|
import {DOMParser} from 'xmldom';
|
|
|
|
export function query (xml: string, query: string): Array<{outer: string, inner: string}> {
|
|
const dom = new DOMParser().parseFromString(xml);
|
|
let rawResults = [];
|
|
|
|
try {
|
|
rawResults = xpath.select(query, dom);
|
|
} catch (err) {
|
|
throw new Error(`Invalid XPath query: ${query}`);
|
|
}
|
|
|
|
const results = [];
|
|
for (const result of rawResults || []) {
|
|
if (result.constructor.name === 'Attr') {
|
|
results.push({
|
|
outer: result.toString().trim(),
|
|
inner: result.nodeValue
|
|
});
|
|
} else if (result.constructor.name === 'Element') {
|
|
results.push({
|
|
outer: result.toString().trim(),
|
|
inner: result.childNodes.toString()
|
|
});
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|