Last active
June 24, 2020 14:02
-
-
Save hubgit/e1c170b94e1f5a6383c7d5e3605f8d62 to your computer and use it in GitHub Desktop.
Creating a namespace resolver for document.evaluate
This file contains hidden or 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
| // from the doc or node being parsed | |
| const namespaceResolver = doc.createNSResolver(doc) | |
| // from XML you control | |
| const namespaceResolverDoc = new DOMParser().parseFromString( | |
| `<TEI xmlns="http://www.tei-c.org/ns/1.0"/>`, | |
| 'application/xml' | |
| ) | |
| const namespaceResolver = namespaceResolverDoc.createNSResolver( | |
| namespaceResolverDoc | |
| ) | |
| // from a Document created from scratch | |
| /* | |
| const namespaceResolverDoc = new Document() | |
| namespaceResolverDoc.appendChild( | |
| namespaceResolverDoc.createElementNS('http://www.tei-c.org/ns/1.0', 'TEI') | |
| ) | |
| const namespaceResolver = namespaceResolverDoc.createNSResolver( | |
| namespaceResolverDoc | |
| ) | |
| */ | |
| // from a map | |
| const namespaces = new Map<string | null, string>([ | |
| [null, 'http://www.tei-c.org/ns/1.0'], | |
| ]) | |
| const namespaceResolver = (prefix: string | null) => | |
| namespaces.get(prefix) || null | |
| // or | |
| const namespaceResolver = { | |
| lookupNamespaceURI: (prefix: string | null) => namespaces.get(prefix) || null | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment