prelude

@prelude/xml · v1.0.1

SourceAPI referenceChangelognpm

Xml

An XML 1.0 parser with namespace processing. parse turns a document into a small tagged AST; json converts an element tree into plain objects. Errors carry the offset, line and column where the document stopped being well-formed.

Usage

npm i -E @prelude/xml
import * as Xml from '@prelude/xml'

const document = Xml.parse('<note id="1" xmlns:x="urn:x"><x:to>Ada &amp; Bob</x:to></note>')
document.root
// {
//   type: 'Element',
//   name: { prefix: '', local: 'note', namespace: undefined },
//   attributes: [
//     { type: 'Attribute', name: { prefix: '', local: 'id', namespace: undefined }, value: '1' },
//     { type: 'Attribute', name: { prefix: 'xmlns', local: 'x', namespace: 'http://www.w3.org/2000/xmlns/' }, value: 'urn:x' }
//   ],
//   items: [ { type: 'Element', name: { prefix: 'x', local: 'to', namespace: 'urn:x' }, attributes: [], items: [ { type: 'Text', value: 'Ada & Bob' } ] } ]
// }

try {
  Xml.parse('<a><b></a>')
} catch (error) {
  if (error instanceof Xml.XmlError) {
    error.message // 'End tag "</a>" does not match start tag "<b>" (line 1, column 7)'
    error.offset  // 6
  }
}

Supported XML

parse implements XML 1.0 (Fifth Edition) well-formedness for documents without DTD processing, plus Namespaces in XML 1.0:

Not supported, by design

XmlError is thrown at the first problem with offset (UTF-16 code units into the line-end normalized text), line and column (both one-based).

AST

Xml.Ast.Document = { type: 'Document', declaration?, prelude, root, epilogue } where prelude holds comments, processing instructions and the doctype before the root and epilogue the comments and processing instructions after it. Element content (items) holds Text, Cdata, Comment, ProcessingInstruction and nested Element nodes; every Name is { prefix, local, namespace }. Xml.Name.string(name) renders prefix:local, Xml.Name.eq compares names as written and Xml.Name.same compares expanded names.

Xml.json(element, options) produces { type, attributes | @-prefixed attributes, text, elements } objects; Xml.Json.attributes, Xml.Json.text and Xml.Json.property are the pieces.

License

This package is dedicated to the public domain under CC0 1.0.