-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: enhance all kinds xpath queries and clean code
- Loading branch information
Showing
3 changed files
with
43 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
import { DOMParser } from 'xmldom'; | ||
import xpath, { SelectedValue } from 'xpath'; | ||
import xpath from 'xpath'; | ||
|
||
/** | ||
* A revised type for the return value of `xpath.select(expression, node)` | ||
*/ | ||
type SelectedValue = string | number | boolean | Node[] | ||
|
||
/** | ||
* Query an XML blob with XPath | ||
*/ | ||
export const queryXPath = (xml: string, query?: string) => { | ||
const dom = new DOMParser().parseFromString(xml); | ||
let selectedValues: SelectedValue[] = []; | ||
if (query === undefined) { | ||
throw new Error('Must pass an XPath query.'); | ||
} | ||
const dom = new DOMParser().parseFromString(xml); | ||
let selectedValues: SelectedValue = []; | ||
try { | ||
selectedValues = xpath.select(query, dom); | ||
selectedValues = xpath.select(query, dom) as SelectedValue; | ||
} catch (err) { | ||
throw new Error(`Invalid XPath query: ${query}`); | ||
} | ||
// Functions return plain strings | ||
if (typeof selectedValues === 'string') { | ||
return [{ outer: selectedValues, inner: selectedValues }]; | ||
} | ||
|
||
return (selectedValues as Node[]) | ||
.filter(sv => sv.nodeType === Node.ATTRIBUTE_NODE | ||
|| sv.nodeType === Node.ELEMENT_NODE | ||
|| sv.nodeType === Node.TEXT_NODE) | ||
.map(selectedValue => { | ||
const outer = selectedValue.toString().trim(); | ||
if (selectedValue.nodeType === Node.ATTRIBUTE_NODE) { | ||
return { outer, inner: selectedValue.nodeValue }; | ||
} | ||
if (selectedValue.nodeType === Node.ELEMENT_NODE) { | ||
return { outer, inner: selectedValue.childNodes.toString() }; | ||
} | ||
if (selectedValue.nodeType === Node.TEXT_NODE) { | ||
return { outer, inner: selectedValue.toString().trim() }; | ||
} | ||
return { outer, inner: null }; | ||
}); | ||
if (Array.isArray(selectedValues)) { | ||
return selectedValues | ||
.filter(sv => sv.nodeType === Node.ATTRIBUTE_NODE | ||
|| sv.nodeType === Node.ELEMENT_NODE | ||
|| sv.nodeType === Node.TEXT_NODE) | ||
.map(selectedValue => { | ||
const outer = selectedValue.toString().trim(); | ||
if (selectedValue.nodeType === Node.ATTRIBUTE_NODE) { | ||
return { outer, inner: selectedValue.nodeValue }; | ||
} | ||
if (selectedValue.nodeType === Node.ELEMENT_NODE) { | ||
return { outer, inner: selectedValue.childNodes.toString() }; | ||
} | ||
if (selectedValue.nodeType === Node.TEXT_NODE) { | ||
return { outer, inner: selectedValue.toString().trim() }; | ||
} | ||
return { outer, inner: null }; | ||
}); | ||
} | ||
|
||
return [{ outer: selectedValues, inner: selectedValues }]; | ||
}; |