MyNixOS website logo
Description

XML parser and renderer with HTML 5 quirks mode.

Contains renderers and parsers for both XML and HTML 5 document fragments, which share data structures so that it's easy to work with both. Document fragments are bits of documents, which are not constrained by some of the high-level structure rules (in particular, they may contain more than one root element).

Note that this is not a compliant HTML 5 parser. Rather, it is a parser for HTML 5 compliant documents. It does not implement the HTML 5 parsing algorithm, and should generally be expected to perform correctly only on documents that you trust to conform to HTML 5. This is not a suitable library for implementing web crawlers or other software that will be exposed to documents from outside sources. The result is also not the HTML 5 node structure, but rather something closer to the physical structure. For example, omitted start tags are not inserted (and so, their corresponding end tags must also be omitted).

xmlhtml - XML and HTML 5 parsing and rendering

GitHub CI

This library implements both parsers and renderers for XML and HTML 5 document fragments. The two share data structures to represent the document tree, so that you can write code to easily work with either XML or HTML 5. Convenience functions are also available to work with the internal data structure in several natural ways.

Caveats:

  • Both parsers are written to parse document fragments, not complete documents. This means that they do not enforce rules about overall document structure. There does not need to be only a single root node, and the HTML 5 implementation never inserts any missing start tags.

  • The XML parser is incapable of handling processing instructions, or defined entities. If will silently drop processing instructions, and will fail if encounters an entity reference for anything by the predefined entities (apos, quot, amp, lt, and gt).

  • The HTML parser is really an XML parser with HTML 5 quirks mode. It should be just fine for parsing documents that conform to the HTML 5 specification. However, it is not a compliant HTML 5 parser, as compliant parsers are required to be compatible with non-compliant documents in many ways that we aren't interested in. So this is a great basis for a template system, for example, but a very poor basis for a web browser or web spider.

To get started, just use the parseHTML or parseXML functions from Text.XmlHtml to parse a ByteString into a document tree. On the other side, use render to write the document tree back to a ByteString.

Working with document trees is easily done in two ways.

  1. Text.XmlHtml exports the document tree types (notably, Document and Node) and functions like getAttribute, setAttribute, tagName, childNodes, etc. for working with them.

  2. Text.XmlHtml.Cursor exports a zipper for node forests, which you can use to navigate and modify the document tree positionally.

That's it, basically. This is hopefully a pretty simple package to use.

TO DO Items:

  1. Do something better with character encodings. For now, they are basically ignored, and we just use the byte order mark to distinguish between the three required encodings. We should implement the encoding sniffing rules for both XML (the <?xml ... ?> declaration) and HTML 5.

  2. Benchmark and improve performance of the parsers and renderers.

  3. Ensure that rendering always gives an error rather than writing an invalid document. (Is this a good idea? It does limit rendering speed.)

Metadata

Version

0.2.5.4

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows