Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active June 24, 2020 14:02
Show Gist options
  • Select an option

  • Save hubgit/e1c170b94e1f5a6383c7d5e3605f8d62 to your computer and use it in GitHub Desktop.

Select an option

Save hubgit/e1c170b94e1f5a6383c7d5e3605f8d62 to your computer and use it in GitHub Desktop.
Creating a namespace resolver for document.evaluate
// 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