helium

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2026 License: MIT Imports: 34 Imported by: 0

README

helium

CI Go Reference Ask DeepWiki

Helium is a fast XML toolkit for Go covering XML parsing, SAX2-style streaming, XPath 3.1, XSLT 3.0, XInclude, XSD, Relax NG, and Schematron.

The root helium package handles parsing, DOM building, and serialization, but the module is broader than an XML parser. It also includes xpath3 for XPath 3.1 querying and xslt3 for XSLT 3.0 transformations, alongside xpath1 for XPath 1.0 compatibility, xsd, relaxng, and schematron for validation, xinclude for inclusion processing, c14n for canonicalization, html for HTML parsing, and shim for encoding/xml-compatible APIs.

It started as an effort to port libxml2-style capabilities to Go, but grew broader native Go APIs along the way. The goal is to provide a full Go XML stack for parsing, querying, transforming, and validating documents, with each major feature area documented in its own package README.

SYNOPSIS

package examples_test

import (
  "context"
  "fmt"

  "github.com/lestrrat-go/helium"
)

func Example_helium_parse() {
  // helium.NewParser().Parse is the simplest way to parse an XML document from a byte slice.
  // It returns a *helium.Document representing the parsed DOM tree.
  doc, err := helium.NewParser().Parse(context.Background(), []byte(`<root><child>hello</child></root>`))
  if err != nil {
    fmt.Printf("failed to parse: %s\n", err)
    return
  }

  // WriteString serializes the entire document back to an XML string,
  // including the XML declaration (<?xml version="1.0"?>).
  s, err := helium.WriteString(doc)
  if err != nil {
    fmt.Printf("failed to serialize: %s\n", err)
    return
  }
  fmt.Println(s)
  // Output:
  // <?xml version="1.0"?>
  // <root><child>hello</child></root>
}

source: examples/helium_parse_example_test.go

Packages

Each public subpackage has its own README.md with package-specific details and an embedded example.

Package Description Notes
c14n W3C Canonical XML support. C14N 1.0, exclusive C14N 1.0, and C14N 1.1.
catalog OASIS XML Catalog loading and resolution. Useful with parsers, validators, and external resources.
enum Shared typed enums for DTD declarations. Low-level support package; no standalone example.
html HTML parser and serializer on top of helium nodes. Produces helium DOM nodes or SAX-style events.
relaxng RELAX NG compilation and validation. Schema compile step plus document validation.
sax SAX2 handler interfaces and helpers. Event-driven parsing surface used by helium and html.
schematron Schematron compilation and validation. Rule-based XML validation with XPath assertions.
shim encoding/xml-compatible API backed by helium. Import-path swap for existing stdlib-style code.
sink Generic async event sink. Also satisfies helium.ErrorHandler when T is error.
stream Streaming XML writer. Writes XML directly without building a DOM.
xinclude XInclude processing for helium documents. Supports recursive inclusion and custom resolvers.
xmldsig1 W3C XML Digital Signatures 1.1 over helium documents. Experimental; API may change.
xmlenc1 W3C XML Encryption 1.1 over helium documents. Experimental; API may change.
xpath1 XPath 1.0 compilation and evaluation. Includes convenience helpers like Find and Evaluate.
xpath3 XPath 3.1 compilation and evaluation. Includes a compiler, evaluator, maps, arrays, and HOFs.
xpointer XPointer evaluation. Supports shorthand, element(), and XPath-backed schemes.
xsd XML Schema compilation and validation. XSD 1.0 compiler plus validator APIs.
xslt3 XSLT 3.0 stylesheet compilation and execution. Targets Basic XSLT 3.0 conformance.

helium CLI

The command-line interface is exposed as helium. Currently implemented subcommands: lint, xpath, xslt, xsd validate, relaxng validate, schematron validate. Use helium lint in place of the old heliumlint command.

Command Purpose
helium lint Parse and lint XML documents
helium xpath Evaluate XPath expressions against XML input
helium xslt Transform XML with XSLT 3.0 stylesheets
helium relaxng validate Validate XML documents against a RELAX NG schema
helium schematron validate Validate XML documents against a Schematron schema
helium xsd validate Validate XML documents against an XML Schema

See cmd/helium/README.md for command-specific documentation.

Performance

Helium parses XML into a full DOM tree. The benchmark below compares that DOM build against two lower-level baselines: an encoding/xml token loop (Decoder.Token) and libxml2 via cgo.

That is a narrower benchmark than every real encoding/xml workload. Many Go programs use encoding/xml to decode directly into structs, and this section is not meant to dismiss that use case or the package. The point here is simply that Helium's DOM parse is already quite fast: it is materially faster than the stdlib token benchmark on all three corpora, it now edges past libxml2 on the medium corpus, and it is clearly ahead on the largest corpus.

Benchmarks parse real-world XML files of varying sizes (AMD Ryzen 9 7900X3D, Go 1.26.1, go test -run '^$' -bench 'Benchmark(HeliumParse|StdlibXMLDecode|Libxml2Parse)$' -benchmem -count=5 -tags libxml2bench ./bench, median shown):

File Helium encoding/xml libxml2 (cgo)
109 KB 139 MB/s 77 MB/s 158 MB/s
196 KB 124 MB/s 66 MB/s 109 MB/s
3 MB 497 MB/s 120 MB/s 366 MB/s

Helium also allocates far fewer objects than encoding/xml in this benchmark. On the 3 MB corpus, the current Helium DOM parse lands around 94 allocs/op versus about 155k allocs/op for encoding/xml.

To run the benchmarks yourself:

go test -bench='BenchmarkHeliumParse|BenchmarkStdlibXMLDecode' -benchmem ./bench/
# Include libxml2 (requires cgo and libxml2-dev):
go test -tags cgo,libxml2bench -bench=. -benchmem ./bench/

Current status

  • Core functionality is implemented: XML/HTML parsing, DOM building, SAX2, XPath 1.0, XPath 3.1, Basic XSLT 3.0, XInclude, C14N, RELAX NG, Schematron, XSD, XML Catalog, streaming XML writer, and encoding/xml compatibility (shim package).
  • Experimental: W3C XML Digital Signatures 1.1 (xmldsig1) and XML Encryption 1.1 (xmlenc1). These APIs may change and may move to a separate repository.
  • W3C conformance suites: ~22,250 / 22,744 QT3 tests pass for XPath 3.1; ~11,780 / 13,129 W3C tests pass for XSLT 3.0 (skips are XSLT 1.0/2.0 backwards compatibility and other out-of-scope features).
  • libxml2-compat golden tests: core XML parsing 100%, XSD 99.6%, RELAX NG 100%, Schematron 100%, C14N 87%, HTML 100%.
  • XSLT support is intentionally scoped to Basic XSLT 3.0. Backwards compatibility modes for XSLT 1.0/2.0 are not part of the target feature set.
  • A helium CLI provides lint, xpath, xslt, xsd validate, relaxng validate, and schematron validate subcommands.
  • Some edge cases and parity gaps are still being iterated on; contributions and issue reports are welcome.

For coding agents

If you are an AI coding agent (Claude Code, Codex, Gemini, etc.) working in this repository, start with AGENTS.md (also available as CLAUDE.md). It points to the cached navigation and architecture docs under .claude/docs/ and lists the pre-read rules, scope boundaries, and generated-file policy you must follow before making changes.

Runnable usage examples live in the examples/ directory as *_example_test.go files — read those first to see how the public APIs are meant to be used.

Contributing

Issues

For bug reports and feature requests, please follow the issue template when possible. If you can include a minimal reproduction or failing test case, that helps a lot.

Pull Requests

Please include tests that cover your changes.

If your change touches generated files, update the generator/source first, regenerate, and commit both the source and generated outputs together.

Please keep pull requests focused and small enough to review quickly.

Discussions / Usage

For usage questions, design discussion, or "is this approach reasonable?" questions, please open a GitHub Discussion first.

Documentation

Overview

Package helium is a fast, pure-Go XML toolkit covering XML parsing, SAX2-style streaming, XPath 3.1, XSLT 3.0, XInclude, XSD, Relax NG, and Schematron. This root package provides tree-based XML parsing, a DOM interface, SAX2 callbacks, namespace handling, DTD validation, and serialization. See the sub-packages listed below for additional features.

Parsing

Use NewParser to create a parser, configure it with fluent builder methods, and call a terminal method to parse XML:

doc, err := helium.NewParser().
    SubstituteEntities(true).
    Parse(ctx, xmlBytes)

For file-based input, use Parser.ParseFile:

doc, err := helium.NewParser().ParseFile(ctx, "input.xml")

For streaming input, use Parser.ParseReader or Parser.NewPushParser.

Serialization

Use NewWriter to serialize documents or nodes back to XML:

err := helium.NewWriter().Format(true).WriteTo(os.Stdout, doc)

DOM

The document tree consists of Node values. Concrete types include Document, Element, Text, Comment, CDATASection, ProcessingInstruction, Attribute, DTD, and Namespace. Tree traversal helpers include Walk, Children, Descendants, and ChildElements.

Sub-packages provide additional XML processing:

Examples

Example code for this package lives in the examples/ directory at the repository root (files prefixed with helium_). Because examples are in a separate test module they do not appear in the generated documentation.

Index

Constants

View Source
const (
	StandaloneInvalidValue = -99
	StandaloneExplicitYes  = 1
	StandaloneExplicitNo   = 0
	StandaloneNoXMLDecl    = -1
	StandaloneImplicitNo   = -2
)
View Source
const MaxExternalDTDSize = 10 << 20 // 10 MiB

MaxExternalDTDSize is the maximum number of bytes read from an external DTD subset. Loading is gated by LoadExternalDTD/ValidateDTD/ DefaultDTDAttributes, so an unbounded read of a hostile or pathological source (e.g. /dev/zero) could exhaust memory before any entity or parse limits apply. The DTD is read through a strict byte cap and rejected when it is exceeded.

View Source
const MaxNameLength = 50000

Variables

View Source
var (
	EntityLT         = newEntity("lt", enum.InternalPredefinedEntity, "", "", "<", "&lt;")
	EntityGT         = newEntity("gt", enum.InternalPredefinedEntity, "", "", ">", "&gt;")
	EntityAmpersand  = newEntity("amp", enum.InternalPredefinedEntity, "", "", "&", "&amp;")
	EntityApostrophe = newEntity("apos", enum.InternalPredefinedEntity, "", "", "'", "&apos;")
	EntityQuote      = newEntity("quot", enum.InternalPredefinedEntity, "", "", `"`, "&quot;")
)
View Source
var (
	ErrNilNode            = errors.New("nil node")
	ErrInvalidOperation   = errors.New("operation cannot be performed")
	ErrDuplicateAttribute = errors.New("duplicate attribute")
	ErrEntityBoundary     = errors.New("entity boundary violation")
	// ErrExternalDTDTooLarge is returned when an external DTD subset exceeds
	// the configured byte cap (set via Parser.MaxExternalDTDBytes), or
	// MaxExternalDTDSize when no cap is configured. The cap is enforced
	// against the actual number of bytes read, not any advisory Stat size.
	ErrExternalDTDTooLarge = errors.New("external DTD exceeds maximum allowed size")
)

Sentinel errors for DOM operations.

View Source
var (
	ErrAmpersandRequired             = errors.New("'&' was required here")
	ErrAttrListNotFinished           = errors.New("attrlist must finish with a ')'")
	ErrAttrListNotStarted            = errors.New("attrlist must start with a '('")
	ErrAttributeNameRequired         = errors.New("attribute name was required here (ATTLIST)")
	ErrByteCursorRequired            = errors.New("inconsistent state: required ByteCursor")
	ErrDocTypeNameRequired           = errors.New("doctype name required")
	ErrDocTypeNotFinished            = errors.New("doctype not finished")
	ErrDocumentEnd                   = errors.New("extra content at document end")
	ErrEOF                           = errors.New("end of file reached")
	ErrElementContentNotFinished     = errors.New("element content not finished")
	ErrEmptyDocument                 = errors.New("start tag expected, '<' not found")
	ErrEntityNotFound                = errors.New("entity not found")
	ErrEqualSignRequired             = errors.New("'=' was required here")
	ErrGtRequired                    = errors.New("'>' was required here")
	ErrHyphenInComment               = errors.New("'--' not allowed in comment")
	ErrInvalidChar                   = errors.New("invalid char")
	ErrInvalidComment                = errors.New("invalid comment section")
	ErrInvalidCDSect                 = errors.New("invalid CDATA section")
	ErrInvalidDocument               = errors.New("invalid document")
	ErrInvalidDTD                    = errors.New("invalid DTD section")
	ErrInvalidElementDecl            = errors.New("invalid element declaration")
	ErrInvalidEncodingName           = errors.New("invalid encoding name")
	ErrInvalidName                   = errors.New("invalid xml name")
	ErrInvalidProcessingInstruction  = errors.New("invalid processing instruction")
	ErrInvalidVersionNum             = errors.New("invalid version")
	ErrInvalidXMLDecl                = errors.New("invalid XML declaration")
	ErrInvalidParserCtx              = errors.New("invalid parser context")
	ErrLtSlashRequired               = errors.New("'</' is required")
	ErrMisplacedCDATAEnd             = errors.New("misplaced CDATA end ']]>'")
	ErrNameTooLong                   = errors.New("name is too long")
	ErrNameRequired                  = errors.New("name is required")
	ErrNmtokenRequired               = errors.New("nmtoken is required")
	ErrNotationNameRequired          = errors.New("notation name expected in NOTATION declaration")
	ErrNotationNotFinished           = errors.New("notation must finish with a ')'")
	ErrNotationNotStarted            = errors.New("notation must start with a '('")
	ErrOpenParenRequired             = errors.New("'(' is required")
	ErrPCDATARequired                = errors.New("'#PCDATA' required")
	ErrPercentRequired               = errors.New("'%' is required")
	ErrPrematureEOF                  = errors.New("end of document reached")
	ErrUndeclaredEntity              = errors.New("undeclared entity")
	ErrSemicolonRequired             = errors.New("';' is required")
	ErrConditionalSectionNotFinished = errors.New("conditional section ']]>' expected")
	ErrConditionalSectionKeyword     = errors.New("INCLUDE or IGNORE keyword expected in conditional section")
	ErrSpaceRequired                 = errors.New("space required")
	ErrStartTagRequired              = errors.New("start tag expected, '<' not found")
	ErrValueRequired                 = errors.New("value required")
)

Sentinel errors for XML parse failures. Each corresponds to a specific syntactic violation detected by the parser. These errors typically appear as the Err field of an ErrParseError.

View Source
var (
	ErrCDATANotFinished = errors.New("invalid CDATA section (premature end)")
	ErrCDATAInvalid     = errors.New("invalid CDATA section")
)
View Source
var ErrDTDValidationFailed = errors.New("dtd: validation failed")

ErrDTDValidationFailed is returned by DTD validation when the document does not conform to the DTD. Individual validation errors are delivered to the configured ErrorHandler.

View Source
var ErrParseSucceeded = errors.New("parse succeeded")

Functions

func AppendChildFast added in v0.3.0

func AppendChildFast(parent MutableNode, child Node) error

AppendChildFast links child as the last child of parent without running the cycle-guard and duplicate-attribute preflight that AddChild performs. It is intended for callers that are building a freshly-constructed, provably acyclic and duplicate-free tree (e.g. a deep copy), where those safety checks are pure overhead. Misuse on an arbitrary live tree can corrupt linkage; prefer AddChild unless the no-preflight contract is known to hold.

func AsNode

func AsNode[T Node](n Node) (T, bool)

AsNode performs a safe type assertion on a Node, returning the concrete type T and true if the assertion succeeds, or the zero value of T and false otherwise.

if elem, ok := helium.AsNode[*helium.Element](node); ok {
    // use elem
}

func BuildURI

func BuildURI(systemID, base string) string

BuildURI resolves a relative system ID against a base URI. For local file paths (no scheme or file: scheme), it uses filepath.Join. For other schemes, it uses url.ResolveReference.

func ChildElements

func ChildElements(n Node) iter.Seq[*Element]

ChildElements returns an iterator over the direct child elements of n, skipping non-element children such as text, comments, and processing instructions. Use ChildElements when you only care about element children and want to avoid type-checking each node yourself.

To iterate over all child nodes including non-elements, use Children instead.

If n is nil or has no element children, the iterator yields nothing.

The caller must not modify the tree structure (add, remove, or reorder nodes) during iteration. Doing so may cause nodes to be skipped or visited more than once.

func Children

func Children(n Node) iter.Seq[Node]

Children returns an iterator over all direct child nodes of n, including elements, text, comments, processing instructions, and any other node types. Use Children when you need to inspect or process every node in the child list regardless of type.

To iterate over child elements only, use ChildElements instead.

If n is nil or has no children, the iterator yields nothing.

The caller must not modify the tree structure (add, remove, or reorder nodes) during iteration. Doing so may cause nodes to be skipped or visited more than once.

func ClarkName

func ClarkName(uri, local string) string

ClarkName returns the Clark notation "{uri}local" for a namespace URI and local name pair.

func CopyDTDInfo

func CopyDTDInfo(src, dst *Document)

CopyDTDInfo copies DTD information (entities, notations, element/attribute declarations) from src to dst. This preserves unparsed entity information when creating document copies via xsl:copy.

func CopyExtSubset added in v0.3.0

func CopyExtSubset(src, dst *Document)

CopyExtSubset deep-copies src's external DTD subset into dst, installing it as dst's external subset. The copy is fully independent: it owns its own *DTD and its own entity/element/attribute/notation declarations, so mutating dst's external subset (e.g. via AddNotation/AddEntity/AddElementDecl) never affects src's external subset, and vice versa.

Unlike CopyDTDInfo (which copies the internal subset and links it into the document tree before the root element), the external subset is not a child of the document — it is referenced only via ExtSubset — so the copy is not added to dst's child list. If src has no external subset this is a no-op.

func Descendants

func Descendants(n Node) iter.Seq[Node]

Descendants returns an iterator that performs a depth-first pre-order traversal of all descendants of n (not including n itself). If n is nil or has no children, the iterator yields nothing.

The caller must not modify the tree structure (add, remove, or reorder nodes) during iteration. Doing so may cause nodes to be skipped or visited more than once.

func NewLeveledError

func NewLeveledError(msg string, level ErrorLevel) error

NewLeveledError creates an error that implements ErrorLeveler.

func NodeGetBase

func NodeGetBase(doc *Document, n Node) string

NodeGetBase returns the effective base URI for a node by walking ancestors and resolving xml:base attributes. Returns empty string if no base URI found.

func SetNodeBaseURI

func SetNodeBaseURI(n Node, uri string)

SetNodeBaseURI sets an explicit base URI on a node. This is used by xsl:copy to preserve the original element's base URI on the copy.

func StopParser

func StopParser(ctx context.Context)

StopParser tells the parser to stop at the next opportunity. Call this from any SAX callback to abort parsing early. The parse functions will return the partial document built so far with a nil error.

func UnlinkNode

func UnlinkNode(n MutableNode)

UnlinkNode detaches a node from its parent and sibling chain. After unlinking, the node has no parent, prev, or next pointers.

func Walk

func Walk(n Node, w NodeWalker) error

Walk performs a depth-first traversal of the node tree rooted at n, calling w.Visit for each node. There is no direct libxml2 equivalent; callers typically write manual tree traversal loops in C.

func Write

func Write(out io.Writer, node Node) error

Write serializes a node (document or element) to the given writer using default settings.

func WriteString

func WriteString(node Node) (string, error)

WriteString serializes a node (document or element) to a string using default settings.

Types

type AttrNotFoundError

type AttrNotFoundError struct {
	Token string
}

AttrNotFoundError is returned when a referenced attribute token cannot be found in the DTD attribute declarations.

func (AttrNotFoundError) Error

func (e AttrNotFoundError) Error() string

type Attribute

type Attribute struct {
	// contains filtered or unexported fields
}

Attribute represents an XML attribute (libxml2: xmlAttr).

func (*Attribute) AType

func (n *Attribute) AType() enum.AttributeType

AType returns the attribute type (e.g. enum.AttrID, enum.AttrCDATA).

func (*Attribute) AddChild

func (n *Attribute) AddChild(cur Node) error

AddChild adds cur as a child of this attribute node. For attributes the child is typically a text node holding the attribute value.

func (*Attribute) AddSibling

func (n *Attribute) AddSibling(cur Node) error

AddSibling inserts cur as the next sibling of this attribute, effectively appending another attribute to the owning element's attribute list.

func (*Attribute) AppendText

func (n *Attribute) AppendText(b []byte) error

AppendText appends the given bytes to the attribute's text content.

func (Attribute) Content

func (n Attribute) Content() []byte

func (Attribute) FirstChild

func (n Attribute) FirstChild() Node

func (*Attribute) IsDefault

func (n *Attribute) IsDefault() bool

IsDefault reports whether this attribute was supplied by the DTD as a default value rather than being explicitly specified in the source document.

func (Attribute) LastChild

func (n Attribute) LastChild() Node

func (Attribute) Line

func (n Attribute) Line() int

func (Attribute) LocalName

func (n Attribute) LocalName() string

func (Attribute) Name

func (n Attribute) Name() string

Name returns the qualified (prefixed) name of the attribute. If the attribute belongs to a namespace with a non-empty prefix, the result is "prefix:localname"; otherwise it is just the local name.

func (*Attribute) NextAttribute

func (n *Attribute) NextAttribute() *Attribute

NextAttribute is a thin wrapper around NextSibling() so that the caller does not have to constantly type assert

func (Attribute) NextSibling

func (n Attribute) NextSibling() Node

func (Attribute) OwnerDocument

func (n Attribute) OwnerDocument() *Document

func (Attribute) Parent

func (n Attribute) Parent() Node

func (Attribute) Prefix

func (n Attribute) Prefix() string

Prefix returns the namespace prefix of the attribute, or an empty string if the attribute is not in a namespace.

func (Attribute) PrevSibling

func (n Attribute) PrevSibling() Node

func (*Attribute) Replace

func (n *Attribute) Replace(nodes ...Node) error

Replace replaces this attribute node in its parent's attribute list with the given nodes.

func (*Attribute) SetAType

func (n *Attribute) SetAType(v enum.AttributeType)

SetAType sets the attribute type.

func (*Attribute) SetDefault

func (n *Attribute) SetDefault(b bool)

SetDefault marks (or unmarks) this attribute as a default attribute, i.e. one supplied by the DTD rather than present in the source document.

func (*Attribute) SetLine

func (n *Attribute) SetLine(line int)

func (*Attribute) SetNextSibling

func (n *Attribute) SetNextSibling(cur Node)

func (*Attribute) SetOwnerDocument

func (n *Attribute) SetOwnerDocument(doc *Document)

func (*Attribute) SetParent

func (n *Attribute) SetParent(cur Node)

func (*Attribute) SetPrevSibling

func (n *Attribute) SetPrevSibling(cur Node)

func (*Attribute) SetTreeDoc

func (n *Attribute) SetTreeDoc(doc *Document)

SetTreeDoc recursively sets the owner document for this attribute and all of its children (e.g. its text-node value).

func (Attribute) Type

func (n Attribute) Type() ElementType

func (Attribute) URI

func (n Attribute) URI() string

URI returns the namespace URI of the attribute, or an empty string if the attribute is not in a namespace.

func (Attribute) Value

func (n Attribute) Value() string

Value returns the attribute's text value as a string.

type AttributeDecl

type AttributeDecl struct {
	// contains filtered or unexported fields
}

AttributeDecl is an xml attribute declaration from DTD.

func (*AttributeDecl) AType

func (n *AttributeDecl) AType() enum.AttributeType

AType returns the attribute type (e.g. enum.AttrID, enum.AttrCDATA).

func (*AttributeDecl) AddChild

func (n *AttributeDecl) AddChild(cur Node) error

AddChild adds cur as a child of this attribute declaration node.

func (*AttributeDecl) AddSibling

func (n *AttributeDecl) AddSibling(cur Node) error

AddSibling inserts cur as the next sibling of this attribute declaration in the DTD's declaration list.

func (*AttributeDecl) AppendText

func (n *AttributeDecl) AppendText(b []byte) error

AppendText appends the given bytes to this attribute declaration's text content.

func (AttributeDecl) Content

func (n AttributeDecl) Content() []byte

func (*AttributeDecl) Elem

func (n *AttributeDecl) Elem() string

Elem returns the element name this attribute declaration belongs to.

func (AttributeDecl) FirstChild

func (n AttributeDecl) FirstChild() Node

func (AttributeDecl) LastChild

func (n AttributeDecl) LastChild() Node

func (AttributeDecl) Line

func (n AttributeDecl) Line() int

func (AttributeDecl) LocalName

func (n AttributeDecl) LocalName() string

func (AttributeDecl) Name

func (n AttributeDecl) Name() string

func (AttributeDecl) NextSibling

func (n AttributeDecl) NextSibling() Node

func (AttributeDecl) OwnerDocument

func (n AttributeDecl) OwnerDocument() *Document

func (AttributeDecl) Parent

func (n AttributeDecl) Parent() Node

func (AttributeDecl) PrevSibling

func (n AttributeDecl) PrevSibling() Node

func (*AttributeDecl) Replace

func (n *AttributeDecl) Replace(nodes ...Node) error

Replace replaces this attribute declaration node with the given nodes.

func (*AttributeDecl) SetLine

func (n *AttributeDecl) SetLine(line int)

func (*AttributeDecl) SetNextSibling

func (n *AttributeDecl) SetNextSibling(cur Node)

func (*AttributeDecl) SetOwnerDocument

func (n *AttributeDecl) SetOwnerDocument(doc *Document)

func (*AttributeDecl) SetParent

func (n *AttributeDecl) SetParent(cur Node)

func (*AttributeDecl) SetPrevSibling

func (n *AttributeDecl) SetPrevSibling(cur Node)

func (*AttributeDecl) SetTreeDoc

func (n *AttributeDecl) SetTreeDoc(doc *Document)

SetTreeDoc recursively sets the owner document for this attribute declaration and all of its children.

func (AttributeDecl) Type

func (n AttributeDecl) Type() ElementType

type AttributePredicate

type AttributePredicate interface {
	Match(*Attribute) bool
}

AttributePredicate reports whether an attribute matches a lookup. Implementations are used by FindAttribute to support alternate matching semantics without exposing the property list layout.

type CDATASection

type CDATASection struct {
	// contains filtered or unexported fields
}

CDATASection represents a CDATA section node in the DOM tree (libxml2: xmlNode with type XML_CDATA_SECTION_NODE).

func (*CDATASection) AddChild

func (n *CDATASection) AddChild(cur Node) error

func (*CDATASection) AddNamespaceDecl added in v0.3.0

func (n *CDATASection) AddNamespaceDecl(ns *Namespace)

AddNamespaceDecl appends an existing Namespace to this node's declarations (nsDefs) without allocating a new one. Unlike DeclareNamespace it does not create a fresh Namespace, so a caller building a tree can reuse one Namespace object as both the declaration and an element's active namespace. The caller owns ns; it must not be shared as a declaration across nodes that could be mutated independently.

func (*CDATASection) AddSibling

func (n *CDATASection) AddSibling(cur Node) error

func (*CDATASection) AppendText

func (n *CDATASection) AppendText(b []byte) error

func (CDATASection) Content

func (n CDATASection) Content() []byte

Content returns a defensive copy of the CDATA section's content. Mutating the returned slice does NOT affect the node; re-reading returns the original bytes.

func (*CDATASection) DeclareNamespace

func (n *CDATASection) DeclareNamespace(prefix, uri string) error

DeclareNamespace declares a namespace on this node without making it the node's active namespace (libxml2: xmlNewNs).

func (*CDATASection) Name

func (n *CDATASection) Name() string

func (CDATASection) Namespace

func (n CDATASection) Namespace() *Namespace

func (CDATASection) Namespaces

func (n CDATASection) Namespaces() []*Namespace

func (CDATASection) Prefix

func (n CDATASection) Prefix() string

func (*CDATASection) RemoveNamespaceByPrefix

func (n *CDATASection) RemoveNamespaceByPrefix(prefix string) bool

RemoveNamespaceByPrefix removes a namespace declaration with the given prefix. Returns true if a declaration was removed.

func (*CDATASection) Replace

func (n *CDATASection) Replace(nodes ...Node) error

func (*CDATASection) SetActiveNamespace

func (n *CDATASection) SetActiveNamespace(prefix, uri string) error

SetActiveNamespace declares a namespace and sets it as this node's active namespace (libxml2: xmlSetNs).

func (*CDATASection) SetNs

func (n *CDATASection) SetNs(ns *Namespace)

SetNs sets the node's active namespace to an existing Namespace object without creating a new declaration.

func (*CDATASection) SetTreeDoc

func (n *CDATASection) SetTreeDoc(doc *Document)

func (CDATASection) URI

func (n CDATASection) URI() string

type CatalogResolver

type CatalogResolver interface {
	// Resolve resolves a public/system identifier pair to a URI.
	// Either pubID or sysID may be empty. Returns "" if no match is found.
	Resolve(ctx context.Context, pubID, sysID string) string

	// ResolveURI resolves a URI reference. Returns "" if no match is found.
	ResolveURI(ctx context.Context, uri string) string
}

CatalogResolver resolves external entity identifiers and URIs via an XML catalog. Implementations are passed to Parser.Catalog to enable catalog-based resolution during parsing.

The github.com/lestrrat-go/helium/catalog package provides a standard implementation backed by OASIS XML Catalog files. Custom implementations may also be used.

type Comment

type Comment struct {
	// contains filtered or unexported fields
}

Comment represents an XML comment node (libxml2: xmlNode with type XML_COMMENT_NODE).

func (*Comment) AddChild

func (n *Comment) AddChild(cur Node) error

func (*Comment) AddNamespaceDecl added in v0.3.0

func (n *Comment) AddNamespaceDecl(ns *Namespace)

AddNamespaceDecl appends an existing Namespace to this node's declarations (nsDefs) without allocating a new one. Unlike DeclareNamespace it does not create a fresh Namespace, so a caller building a tree can reuse one Namespace object as both the declaration and an element's active namespace. The caller owns ns; it must not be shared as a declaration across nodes that could be mutated independently.

func (*Comment) AddSibling

func (n *Comment) AddSibling(cur Node) error

AddSibling adds a new sibling to the end of the sibling nodes.

func (*Comment) AppendText

func (n *Comment) AppendText(b []byte) error

func (Comment) Content

func (n Comment) Content() []byte

Content returns a defensive copy of the comment's content. Mutating the returned slice does NOT affect the node; re-reading returns the original bytes.

func (*Comment) DeclareNamespace

func (n *Comment) DeclareNamespace(prefix, uri string) error

DeclareNamespace declares a namespace on this node without making it the node's active namespace (libxml2: xmlNewNs).

func (*Comment) Name

func (n *Comment) Name() string

func (Comment) Namespace

func (n Comment) Namespace() *Namespace

func (Comment) Namespaces

func (n Comment) Namespaces() []*Namespace

func (Comment) Prefix

func (n Comment) Prefix() string

func (*Comment) RemoveNamespaceByPrefix

func (n *Comment) RemoveNamespaceByPrefix(prefix string) bool

RemoveNamespaceByPrefix removes a namespace declaration with the given prefix. Returns true if a declaration was removed.

func (*Comment) Replace

func (n *Comment) Replace(nodes ...Node) error

func (*Comment) SetActiveNamespace

func (n *Comment) SetActiveNamespace(prefix, uri string) error

SetActiveNamespace declares a namespace and sets it as this node's active namespace (libxml2: xmlSetNs).

func (*Comment) SetNs

func (n *Comment) SetNs(ns *Namespace)

SetNs sets the node's active namespace to an existing Namespace object without creating a new declaration.

func (*Comment) SetTreeDoc

func (n *Comment) SetTreeDoc(doc *Document)

func (Comment) URI

func (n Comment) URI() string

type DTD

type DTD struct {
	// contains filtered or unexported fields
}

DTD represents an XML Document Type Definition (libxml2: xmlDtd).

func (*DTD) AddChild

func (dtd *DTD) AddChild(cur Node) error

AddChild appends cur as the last child of the DTD, detaching it from any previous parent first. It returns an error if cur is nil or if the insertion would create a cycle.

func (*DTD) AddElementDecl

func (dtd *DTD) AddElementDecl(name string, typ enum.ElementType, content *ElementContent) (*ElementDecl, error)

AddElementDecl declares an element content model in the DTD and registers it as a child node. The name may be a QName; its prefix is split off for keying. content must be nil for EMPTY/ANY element types and non-nil for MIXED/ELEMENT types. A previously undefined declaration (created when one of the element's attributes was declared first) is completed in place; a second concrete declaration of the same element is an error.

func (*DTD) AddEntity

func (dtd *DTD) AddEntity(name string, typ enum.EntityType, publicID, systemID, content string) (*Entity, error)

AddEntity declares an entity in the DTD and registers it as a child node, routing general entities and parameter entities into their respective tables based on typ. Predefined entities cannot be registered, and an unknown typ is rejected. Redeclaring an existing general/parameter entity is a no-op that returns the existing declaration (first definition wins, per XML §4.2); redeclaring a predefined entity (lt, gt, amp, apos, quot) with content that does not resolve to the same character is an error.

func (*DTD) AddNotation

func (dtd *DTD) AddNotation(name, publicID, systemID string) (*Notation, error)

AddNotation declares a notation in the DTD and registers it as a child node. It returns an error if a notation with the same name is already declared.

func (*DTD) AddSibling

func (dtd *DTD) AddSibling(cur Node) error

AddSibling appends cur as the last sibling of the DTD.

func (*DTD) AppendText

func (dtd *DTD) AppendText(b []byte) error

AppendText appends b as a Text child of the DTD.

func (*DTD) AttributesForElement

func (dtd *DTD) AttributesForElement(elem string) []*AttributeDecl

AttributesForElement returns all attribute declarations for the named element.

func (DTD) Content

func (n DTD) Content() []byte

func (*DTD) ExternalID

func (dtd *DTD) ExternalID() string

ExternalID returns the DTD external ID (PUBLIC identifier).

func (DTD) FirstChild

func (n DTD) FirstChild() Node

func (*DTD) ForEachEntity

func (dtd *DTD) ForEachEntity(fn func(name string, ent *Entity))

ForEachEntity calls fn for every general entity declared in the DTD. The iteration order is unspecified.

func (*DTD) Free

func (dtd *DTD) Free()

Free is a no-op; it exists to satisfy the Node interface.

func (*DTD) GetElementDesc

func (dtd *DTD) GetElementDesc(name string) (*ElementDecl, bool)

GetElementDesc returns the element declaration for the given QName, splitting off any prefix to compose the lookup key, and reports whether it was found.

func (DTD) LastChild

func (n DTD) LastChild() Node

func (DTD) Line

func (n DTD) Line() int

func (DTD) LocalName

func (n DTD) LocalName() string

func (*DTD) LookupAttribute

func (dtd *DTD) LookupAttribute(name, prefix, elem string) (*AttributeDecl, bool)

LookupAttribute returns the attribute declaration registered for the given attribute local name, prefix, and owning element name, and reports whether it was found.

func (*DTD) LookupElement

func (dtd *DTD) LookupElement(name, prefix string) (*ElementDecl, bool)

LookupElement returns the element declaration registered under the given local name and prefix, and reports whether it was found.

func (*DTD) LookupEntity

func (dtd *DTD) LookupEntity(name string) (*Entity, bool)

LookupEntity returns the general entity declared under name, and reports whether it was found.

func (*DTD) LookupNotation

func (dtd *DTD) LookupNotation(name string) (*Notation, bool)

LookupNotation returns the notation declared under name, and reports whether it was found.

func (*DTD) LookupParameterEntity

func (dtd *DTD) LookupParameterEntity(name string) (*Entity, bool)

LookupParameterEntity returns the parameter entity declared under name, and reports whether it was found.

func (DTD) Name

func (n DTD) Name() string

func (DTD) NextSibling

func (n DTD) NextSibling() Node

func (DTD) OwnerDocument

func (n DTD) OwnerDocument() *Document

func (DTD) Parent

func (n DTD) Parent() Node

func (DTD) PrevSibling

func (n DTD) PrevSibling() Node

func (*DTD) RegisterAttribute

func (dtd *DTD) RegisterAttribute(attr *AttributeDecl) error

RegisterAttribute records an attribute declaration in the DTD, keyed by its name, prefix, and owning element. It returns an error if an attribute with the same key is already declared.

func (*DTD) RemoveElement

func (dtd *DTD) RemoveElement(name, prefix string)

RemoveElement deletes the element declaration registered under the given local name and prefix, if present.

func (*DTD) Replace

func (dtd *DTD) Replace(nodes ...Node) error

Replace swaps the DTD out of its parent, inserting nodes in its place.

func (*DTD) SetLine

func (n *DTD) SetLine(line int)

func (*DTD) SetNextSibling

func (n *DTD) SetNextSibling(cur Node)

func (*DTD) SetOwnerDocument

func (n *DTD) SetOwnerDocument(doc *Document)

func (*DTD) SetParent

func (n *DTD) SetParent(cur Node)

func (*DTD) SetPrevSibling

func (n *DTD) SetPrevSibling(cur Node)

func (*DTD) SetTreeDoc

func (dtd *DTD) SetTreeDoc(doc *Document)

SetTreeDoc sets the owning document of the DTD and its subtree.

func (*DTD) SystemID

func (dtd *DTD) SystemID() string

SystemID returns the DTD system ID (SYSTEM identifier).

func (DTD) Type

func (n DTD) Type() ElementType

type DTDDupTokenError

type DTDDupTokenError struct {
	Name string
}

DTDDupTokenError is returned when a DTD attribute enumeration contains a duplicate token value.

func (DTDDupTokenError) Error

func (e DTDDupTokenError) Error() string

Error returns the diagnostic message for this duplicate token error.

type DocProperties

type DocProperties int

DocProperties is a bitmask of document properties, mirroring libxml2's xmlDocProperties. Properties are set by the parser or by user code to record how the document was produced and what validations it passed.

const (
	DocWellFormed DocProperties = 1 << iota // document is XML well-formed
	DocNSValid                              // document is namespace-valid
	DocOld10                                // parsed with XML 1.0 (4th edition or earlier)
	DocDTDValid                             // DTD validation was successful
	DocXInclude                             // XInclude substitution was done
	DocUserBuilt                            // built via API, not by parsing
	DocInternal                             // built for internal processing
	DocHTML                                 // parsed or built as HTML
)

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document represents an XML document (libxml2: xmlDoc).

func CopyDoc

func CopyDoc(src *Document) (*Document, error)

CopyDoc creates a deep copy of a document including all children.

func NewDefaultDocument

func NewDefaultDocument() *Document

NewDefaultDocument creates a minimal user-built document with version "1.0", no encoding, and implicit-no standalone (libxml2: xmlNewDoc).

func NewDocument

func NewDocument(version, encoding string, standalone DocumentStandaloneType) *Document

NewDocument allocates an empty XML Document with the given XML declaration version, character encoding, and standalone status. The returned document owns no children yet; use CreateElement and the other Create* methods to build a tree, then attach the root via AddChild.

func NewHTMLDocument

func NewHTMLDocument() *Document

NewHTMLDocument creates a new HTML document (HTMLDocumentNode type).

func (*Document) AddChild

func (d *Document) AddChild(cur Node) error

AddChild appends cur as the last child of the document, detaching it from any previous parent first. It returns an error if cur is nil or if the insertion would create a cycle.

func (*Document) AddEntity

func (d *Document) AddEntity(name string, typ enum.EntityType, externalID, systemID, content string) (*Entity, error)

func (*Document) AddSibling

func (d *Document) AddSibling(_ Node) error

func (*Document) AppendText

func (d *Document) AppendText(b []byte) error

AppendText appends b as a Text child of the document, merging into a trailing Text node when possible.

func (Document) Content

func (n Document) Content() []byte

func (*Document) CreateAttribute

func (d *Document) CreateAttribute(name, value string, ns *Namespace) (attr *Attribute, err error)

func (*Document) CreateCDATASection

func (d *Document) CreateCDATASection(value []byte) *CDATASection

CreateCDATASection mirrors xmlNewCDataBlock in libxml2's tree.c.

func (*Document) CreateCharRef

func (d *Document) CreateCharRef(name string) (*EntityRef, error)

func (*Document) CreateComment

func (d *Document) CreateComment(value []byte) *Comment

CreateComment allocates a new Comment node holding value, owned by this document, without attaching it to the tree.

func (*Document) CreateDTD

func (d *Document) CreateDTD() (*DTD, error)

func (*Document) CreateElement

func (d *Document) CreateElement(name string) *Element

CreateElement allocates a new Element named name and owned by this document, but does not attach it to the tree; the caller must insert it via AddChild or a sibling/parent method. When d is non-nil the element is drawn from the document's slab allocator, so the element must not outlive a call to Document.Free. A nil receiver allocates a standalone element with no owning document.

func (*Document) CreateElementContent

func (d *Document) CreateElementContent(name string, etype ElementContentType) (*ElementContent, error)

CreateElementContent builds an ElementContent node of the given type for use in a DTD content model. It returns an error if name and etype are not a valid combination.

func (*Document) CreateInternalSubset

func (d *Document) CreateInternalSubset(name, externalID, systemID string) (*DTD, error)

func (*Document) CreateNamespace

func (d *Document) CreateNamespace(prefix, uri string) (*Namespace, error)

func (*Document) CreatePI

func (d *Document) CreatePI(target, data string) *ProcessingInstruction

func (*Document) CreateReference

func (d *Document) CreateReference(name string) (*EntityRef, error)

func (*Document) CreateText

func (d *Document) CreateText(value []byte) *Text

CreateText allocates a new Text node holding a copy of value, owned by this document, without attaching it to the tree. When d is non-nil both the node and its content bytes come from the document's slab allocators and must not outlive a call to Document.Free. A nil receiver allocates a standalone text node.

func (*Document) DocumentElement

func (d *Document) DocumentElement() *Element

DocumentElement returns the root element of the document, or nil if none exists.

func (*Document) Encoding

func (d *Document) Encoding() string

func (*Document) ExtSubset

func (d *Document) ExtSubset() *DTD

func (Document) FirstChild

func (n Document) FirstChild() Node

func (*Document) Free

func (d *Document) Free()

Free returns pooled slab chunks for reuse by future parse calls. This is optional — if not called, GC handles cleanup normally. Calling Free on a document that is still in use causes undefined behavior.

func (*Document) GetElementByID

func (d *Document) GetElementByID(id string) *Element

GetElementByID returns the first element in the document whose ID matches the given value. If the document's ID table has been populated (during parsing), it performs an O(1) hash lookup. Otherwise it falls back to an O(n) tree walk checking xml:id and DTD-declared ID attributes.

The ID-skip state (see SetSkipIDs) is authoritative and is checked FIRST: a document with SkipIDs() == true resolves NO ids — it returns nil without consulting the ID table or performing the lazy walk. This keeps GetElementByID and fn:id consistent with the SkipIDs contract even on a document that already has a populated ID table (e.g. one parsed normally and then SetSkipIDs(true)).

func (*Document) GetEntity

func (d *Document) GetEntity(name string) (ent *Entity, found bool)

GetEntity looks up a general entity declaration by name, searching the internal subset first and then the external subset (the latter is skipped for standalone="yes" documents). found reports whether a declaration was located.

func (*Document) GetParameterEntity

func (d *Document) GetParameterEntity(name string) (*Entity, bool)

GetParameterEntity looks up a parameter entity declaration by name, searching the internal subset first and then the external subset (the latter is skipped for standalone="yes" documents). The boolean result reports whether a declaration was located.

func (*Document) HasProperty

func (d *Document) HasProperty(p DocProperties) bool

HasProperty reports whether all bits in p are set.

func (*Document) IDTable added in v0.3.0

func (d *Document) IDTable() map[string]*Element

IDTable returns the document's ID->element table populated during parsing (mirroring libxml2's xmlAddID). The returned map is the document's own, not a copy, and is nil for documents built without an interned ID table (e.g. via the API rather than the parser); callers must not mutate it. It lets a derived document (e.g. an xsl:strip-space copy) rebuild an equivalent table by translating each entry's element through the original->copy correspondence, faithfully preserving id()/GetElementByID semantics — including for prefixed elements whose qualified DTD ATTLIST the lazy GetElementByID fallback would otherwise miss.

func (*Document) IntSubset

func (d *Document) IntSubset() *DTD

func (*Document) InternalSubset

func (d *Document) InternalSubset() (*DTD, error)

func (*Document) IsMixedElement

func (d *Document) IsMixedElement(name string) (bool, error)

IsMixedElement reports whether the element named name is declared with mixed (or EMPTY/ANY) content in the document's internal subset. It returns an error if no element declaration is found for name.

func (Document) LastChild

func (n Document) LastChild() Node

func (Document) Line

func (n Document) Line() int

func (Document) LocalName

func (n Document) LocalName() string

func (Document) Name

func (n Document) Name() string

func (Document) NextSibling

func (n Document) NextSibling() Node

func (Document) OwnerDocument

func (n Document) OwnerDocument() *Document

func (Document) Parent

func (n Document) Parent() Node

func (Document) PrevSibling

func (n Document) PrevSibling() Node

func (*Document) Properties

func (d *Document) Properties() DocProperties

Properties returns the document's property flags.

func (*Document) RawEncoding added in v0.3.0

func (d *Document) RawEncoding() string

RawEncoding returns the document's encoding exactly as recorded, WITHOUT the "utf8" default that Encoding() synthesizes for documents whose XML declaration omitted an encoding. An empty result means the source had no encoding declaration. This is the value the XML serializer consults (it emits encoding="..." only when the raw encoding is non-empty), so a faithful copy of a document — e.g. an xsl:strip-space copy — must propagate this rather than Encoding(), which would synthesize a spurious encoding="utf8".

func (*Document) RegisterID

func (d *Document) RegisterID(id string, elem *Element)

RegisterID associates an ID value with an element in the document's ID table. This is called during parsing to build an O(1) lookup table for GetElementByID, mirroring libxml2's xmlAddID.

func (*Document) Replace

func (d *Document) Replace(_ ...Node) error

func (*Document) SetDocumentElement

func (d *Document) SetDocumentElement(root MutableNode) error

func (*Document) SetEncoding

func (d *Document) SetEncoding(enc string)

func (*Document) SetLine

func (n *Document) SetLine(line int)

func (*Document) SetNextSibling

func (n *Document) SetNextSibling(cur Node)

func (*Document) SetOwnerDocument

func (n *Document) SetOwnerDocument(doc *Document)

func (*Document) SetParent

func (n *Document) SetParent(cur Node)

func (*Document) SetPrevSibling

func (n *Document) SetPrevSibling(cur Node)

func (*Document) SetProperties

func (d *Document) SetProperties(p DocProperties)

SetProperties replaces the document's property flags.

func (*Document) SetSkipIDs added in v0.3.0

func (d *Document) SetSkipIDs(v bool)

SetSkipIDs sets the document's ID-skip state. This is used when producing a derived document (e.g. an xsl:strip-space copy) that must mirror the source's ID semantics.

The ID-skip state is authoritative: while it is true, GetElementByID (and therefore fn:id) resolves NO ids and returns nil — even if the document already has a populated ID table from a normal parse. Setting it back to false restores normal resolution against the existing table (or the lazy tree walk for API-built documents).

func (*Document) SetTreeDoc

func (d *Document) SetTreeDoc(doc *Document)

func (*Document) SetURL

func (d *Document) SetURL(url string)

SetURL sets the document URI.

func (*Document) SkipIDs added in v0.3.0

func (d *Document) SkipIDs() bool

SkipIDs reports whether ID attribute interning was skipped when this document was produced (mirrors the parser's SkipIDs option). When true, GetElementByID and fn:id always return no match without an O(n) tree walk.

func (*Document) Standalone

func (d *Document) Standalone() DocumentStandaloneType

func (Document) Type

func (n Document) Type() ElementType

func (*Document) URL

func (d *Document) URL() string

URL returns the document URI, used as the base for relative URI resolution. This mirrors libxml2's xmlDoc.URL field.

func (*Document) Version

func (d *Document) Version() string

type DocumentStandaloneType

type DocumentStandaloneType int

type Element

type Element struct {
	// contains filtered or unexported fields
}

Element represents an XML element node (libxml2: xmlNode with type XML_ELEMENT_NODE).

func (*Element) AddChild

func (n *Element) AddChild(cur Node) error

AddChild adds a new child node to the end of the children nodes (libxml2: xmlAddChild).

func (*Element) AddNamespaceDecl added in v0.3.0

func (n *Element) AddNamespaceDecl(ns *Namespace)

AddNamespaceDecl appends an existing Namespace to this node's declarations (nsDefs) without allocating a new one. Unlike DeclareNamespace it does not create a fresh Namespace, so a caller building a tree can reuse one Namespace object as both the declaration and an element's active namespace. The caller owns ns; it must not be shared as a declaration across nodes that could be mutated independently.

func (*Element) AddSibling

func (n *Element) AddSibling(cur Node) error

AddSibling adds a new sibling to the end of the sibling nodes (libxml2: xmlAddSibling).

func (*Element) AppendText

func (n *Element) AppendText(b []byte) error

AppendText appends text content to this node (libxml2: xmlNodeAddContent).

func (Element) Attributes

func (n Element) Attributes() []*Attribute

Attributes returns a newly allocated slice of the element's attributes in property order. The returned slice is a snapshot: appending to or reordering it does not affect the element, though the *Attribute elements still point at the live attribute nodes. Use ForEachAttribute to avoid the slice allocation.

func (*Element) DeclareNamespace

func (n *Element) DeclareNamespace(prefix, uri string) error

DeclareNamespace declares a namespace on this node without making it the node's active namespace (libxml2: xmlNewNs).

func (*Element) FindAttribute

func (n *Element) FindAttribute(ap AttributePredicate) (*Attribute, bool)

FindAttribute returns the first attribute that matches ap in property order. A nil predicate matches nothing and returns nil, false.

func (Element) ForEachAttribute

func (n Element) ForEachAttribute(fn func(*Attribute) bool)

ForEachAttribute calls fn for each attribute on the element. If fn returns false, iteration stops early. This avoids the slice allocation of Attributes().

No dedicated unit tests: iteration order and early-stop semantics are exercised transitively by XPath attribute-axis and doc-order tests. All current callers always return true; the early-stop path exists as a natural consequence of the iterator pattern.

The unchecked type assertion on NextSibling is safe: the properties chain is attribute-only by construction (field typed *Attribute, only *Attribute nodes are ever linked in).

func (*Element) GetAttribute

func (n *Element) GetAttribute(name string) (string, bool)

GetAttribute returns the value of the attribute with the given QName, or empty string and false if not found.

func (*Element) GetAttributeNS

func (n *Element) GetAttributeNS(localName, nsURI string) (string, bool)

GetAttributeNS returns the value of the attribute with the given local name and namespace URI, or empty string and false if not found.

func (*Element) GetAttributeNodeNS

func (n *Element) GetAttributeNodeNS(localName, nsURI string) *Attribute

GetAttributeNodeNS returns the Attribute node with the given local name and namespace URI, or nil if not found. This is the equivalent of libxml2's xmlHasNsProp, returning the node itself for further inspection (e.g., checking atype or whether it is a default attribute).

func (*Element) HasAttribute

func (n *Element) HasAttribute(name string) bool

HasAttribute reports whether the element has an attribute with the given name.

func (*Element) Name

func (n *Element) Name() string

func (Element) Namespace

func (n Element) Namespace() *Namespace

func (Element) Namespaces

func (n Element) Namespaces() []*Namespace

func (Element) Prefix

func (n Element) Prefix() string

func (*Element) RemoveAttribute

func (n *Element) RemoveAttribute(name string) bool

RemoveAttribute removes the attribute with the given QName from the element. Returns true if an attribute was removed.

func (*Element) RemoveAttributeNS

func (n *Element) RemoveAttributeNS(localName, nsURI string) bool

RemoveAttributeNS removes the attribute with the given local name and namespace URI. Returns true if an attribute was removed.

func (*Element) RemoveNamespaceByPrefix

func (n *Element) RemoveNamespaceByPrefix(prefix string) bool

RemoveNamespaceByPrefix removes a namespace declaration with the given prefix. Returns true if a declaration was removed.

func (*Element) Replace

func (n *Element) Replace(nodes ...Node) error

Replace swaps this element out of its parent, inserting nodes in its place (libxml2: xmlReplaceNode). It returns an error if any operand is nil.

func (*Element) SetActiveNamespace

func (n *Element) SetActiveNamespace(prefix, uri string) error

SetActiveNamespace declares a namespace and sets it as this node's active namespace (libxml2: xmlSetNs).

func (*Element) SetAttribute

func (n *Element) SetAttribute(name, value string) (*Element, error)

SetAttribute creates or replaces the attribute named name, parsing value for entity references, and returns the element for chaining. The name must not contain a colon; use SetAttributeNS for namespaced attributes.

func (*Element) SetAttributeNS

func (n *Element) SetAttributeNS(localname, value string, ns *Namespace) (*Element, error)

SetAttributeNS creates an attribute with the given local name, value, and namespace.

func (*Element) SetBooleanAttribute

func (n *Element) SetBooleanAttribute(name string) error

SetBooleanAttribute creates a boolean attribute (name only, no value). The attribute has no children, distinguishing it from an attribute with an empty string value.

func (*Element) SetLiteralAttribute

func (n *Element) SetLiteralAttribute(name, value string) error

SetLiteralAttribute creates or replaces an attribute with a literal text value. Unlike SetAttribute, the value is not parsed for entity references. This is useful for HTML where the parser has already resolved entities. An empty value creates a text child with empty content (distinguishing it from a boolean attribute which has no children).

func (*Element) SetLiteralAttributeNS

func (n *Element) SetLiteralAttributeNS(localname, value string, ns *Namespace) error

SetLiteralAttributeNS creates or replaces an attribute with a literal text value and namespace. Unlike SetAttributeNS, the value is not parsed for entity references. This is useful when the parser has already resolved entities in attribute values.

func (*Element) SetNs

func (n *Element) SetNs(ns *Namespace)

SetNs sets the node's active namespace to an existing Namespace object without creating a new declaration.

func (*Element) SetTreeDoc

func (n *Element) SetTreeDoc(doc *Document)

SetTreeDoc sets the owning document of this element and of every node in its subtree (libxml2: xmlSetTreeDoc).

func (Element) URI

func (n Element) URI() string

type ElementContent

type ElementContent struct {
	// contains filtered or unexported fields
}

ElementContent represents the content model of a DTD element declaration as a binary tree. Leaf nodes are either #PCDATA or named element references; interior nodes are sequence (,) or choice (|) operators. Each node carries an occurrence constraint (once, ?, *, +).

For example, the declaration <!ELEMENT doc (a, (b | c)+)> produces a sequence node at the root with element "a" as c1 and a choice node (b | c, occurrence +) as c2.

type ElementContentOccur

type ElementContentOccur int

ElementContentOccur describes the occurrence constraint on an ElementContent node.

const (
	// ElementContentOnce means the content must appear exactly once.
	ElementContentOnce ElementContentOccur = iota + 1
	// ElementContentOpt means the content is optional (?).
	ElementContentOpt
	// ElementContentMult means the content may appear zero or more times (*).
	ElementContentMult
	// ElementContentPlus means the content must appear one or more times (+).
	ElementContentPlus
)

type ElementContentType

type ElementContentType int

ElementContentType describes the kind of node in an ElementContent tree.

const (
	// ElementContentPCDATA indicates a #PCDATA leaf.
	ElementContentPCDATA ElementContentType = iota + 1
	// ElementContentElement indicates a named element reference leaf.
	ElementContentElement
	// ElementContentSeq indicates a sequence (,) node with two children.
	ElementContentSeq
	// ElementContentOr indicates a choice (|) node with two children.
	ElementContentOr
)

type ElementDecl

type ElementDecl struct {
	// contains filtered or unexported fields
}

ElementDecl is an xml element declaration from DTD.

func (*ElementDecl) AddChild

func (n *ElementDecl) AddChild(cur Node) error

AddChild adds cur as a child of this element declaration node.

func (*ElementDecl) AddSibling

func (n *ElementDecl) AddSibling(cur Node) error

AddSibling inserts cur as the next sibling of this element declaration in the DTD's declaration list.

func (*ElementDecl) AppendText

func (n *ElementDecl) AppendText(b []byte) error

AppendText appends the given bytes to this element declaration's text content.

func (ElementDecl) Content

func (n ElementDecl) Content() []byte

func (*ElementDecl) DeclType

func (e *ElementDecl) DeclType() enum.ElementType

DeclType returns the element content type declared in the DTD (e.g., ElementElementType for element-only content).

func (ElementDecl) FirstChild

func (n ElementDecl) FirstChild() Node

func (ElementDecl) LastChild

func (n ElementDecl) LastChild() Node

func (ElementDecl) Line

func (n ElementDecl) Line() int

func (ElementDecl) LocalName

func (n ElementDecl) LocalName() string

func (ElementDecl) Name

func (n ElementDecl) Name() string

func (ElementDecl) NextSibling

func (n ElementDecl) NextSibling() Node

func (ElementDecl) OwnerDocument

func (n ElementDecl) OwnerDocument() *Document

func (ElementDecl) Parent

func (n ElementDecl) Parent() Node

func (ElementDecl) PrevSibling

func (n ElementDecl) PrevSibling() Node

func (*ElementDecl) Replace

func (n *ElementDecl) Replace(nodes ...Node) error

Replace replaces this element declaration node with the given nodes.

func (*ElementDecl) SetLine

func (n *ElementDecl) SetLine(line int)

func (*ElementDecl) SetNextSibling

func (n *ElementDecl) SetNextSibling(cur Node)

func (*ElementDecl) SetOwnerDocument

func (n *ElementDecl) SetOwnerDocument(doc *Document)

func (*ElementDecl) SetParent

func (n *ElementDecl) SetParent(cur Node)

func (*ElementDecl) SetPrevSibling

func (n *ElementDecl) SetPrevSibling(cur Node)

func (*ElementDecl) SetTreeDoc

func (n *ElementDecl) SetTreeDoc(doc *Document)

SetTreeDoc recursively sets the owner document for this element declaration and all of its children.

func (ElementDecl) Type

func (n ElementDecl) Type() ElementType

type ElementType

type ElementType int
const (
	ElementNode ElementType = iota + 1
	AttributeNode
	TextNode
	CDATASectionNode
	EntityRefNode
	EntityNode
	ProcessingInstructionNode
	CommentNode
	DocumentNode
	DocumentTypeNode
	DocumentFragNode
	NotationNode
	HTMLDocumentNode
	DTDNode
	ElementDeclNode
	AttributeDeclNode
	EntityDeclNode
	NamespaceDeclNode
	XIncludeStartNode
	XIncludeEndNode

	// NamespaceNode represents a namespace declaration (does not exist in libxml2).
	NamespaceNode
)

func (ElementType) String

func (i ElementType) String() string

type Entity

type Entity struct {
	// contains filtered or unexported fields
}

Entity represents an XML entity declaration (libxml2: xmlEntity).

func (*Entity) AddChild

func (e *Entity) AddChild(cur Node) error

func (*Entity) AddNamespaceDecl added in v0.3.0

func (n *Entity) AddNamespaceDecl(ns *Namespace)

AddNamespaceDecl appends an existing Namespace to this node's declarations (nsDefs) without allocating a new one. Unlike DeclareNamespace it does not create a fresh Namespace, so a caller building a tree can reuse one Namespace object as both the declaration and an element's active namespace. The caller owns ns; it must not be shared as a declaration across nodes that could be mutated independently.

func (*Entity) AddSibling

func (e *Entity) AddSibling(cur Node) error

func (*Entity) AppendText

func (e *Entity) AppendText(b []byte) error

func (*Entity) Checked

func (e *Entity) Checked() bool

Checked reports whether this entity's content has been parsed and validated, used to prevent infinite recursion during entity expansion (libxml2: ent->checked).

func (*Entity) Content

func (e *Entity) Content() []byte

func (*Entity) DeclareNamespace

func (n *Entity) DeclareNamespace(prefix, uri string) error

DeclareNamespace declares a namespace on this node without making it the node's active namespace (libxml2: xmlNewNs).

func (*Entity) EntityType

func (e *Entity) EntityType() enum.EntityType

func (*Entity) ExternalID

func (e *Entity) ExternalID() string

func (*Entity) MarkChecked

func (e *Entity) MarkChecked()

MarkChecked marks this entity as having been parsed and validated (libxml2: ent->checked).

func (*Entity) Name

func (n *Entity) Name() string

func (Entity) Namespace

func (n Entity) Namespace() *Namespace

func (Entity) Namespaces

func (n Entity) Namespaces() []*Namespace

func (Entity) Prefix

func (n Entity) Prefix() string

func (*Entity) RemoveNamespaceByPrefix

func (n *Entity) RemoveNamespaceByPrefix(prefix string) bool

RemoveNamespaceByPrefix removes a namespace declaration with the given prefix. Returns true if a declaration was removed.

func (*Entity) Replace

func (e *Entity) Replace(nodes ...Node) error

func (*Entity) SetActiveNamespace

func (n *Entity) SetActiveNamespace(prefix, uri string) error

SetActiveNamespace declares a namespace and sets it as this node's active namespace (libxml2: xmlSetNs).

func (*Entity) SetNs

func (n *Entity) SetNs(ns *Namespace)

SetNs sets the node's active namespace to an existing Namespace object without creating a new declaration.

func (*Entity) SetOrig

func (e *Entity) SetOrig(s string)

func (*Entity) SetTreeDoc

func (n *Entity) SetTreeDoc(doc *Document)

func (*Entity) SystemID

func (e *Entity) SystemID() string

func (*Entity) URI

func (e *Entity) URI() string

URI returns the fully resolved URI for external entities. Falls back to SystemID if no resolved URI is available.

type EntityRef

type EntityRef struct {
	// contains filtered or unexported fields
}

EntityRef represents an entity reference node in the DOM tree (libxml2: xmlNode with type XML_ENTITY_REF_NODE).

func (*EntityRef) AddChild

func (e *EntityRef) AddChild(cur Node) error

func (*EntityRef) AddNamespaceDecl added in v0.3.0

func (n *EntityRef) AddNamespaceDecl(ns *Namespace)

AddNamespaceDecl appends an existing Namespace to this node's declarations (nsDefs) without allocating a new one. Unlike DeclareNamespace it does not create a fresh Namespace, so a caller building a tree can reuse one Namespace object as both the declaration and an element's active namespace. The caller owns ns; it must not be shared as a declaration across nodes that could be mutated independently.

func (*EntityRef) AddSibling

func (e *EntityRef) AddSibling(cur Node) error

func (*EntityRef) AppendText

func (e *EntityRef) AppendText(b []byte) error

func (*EntityRef) DeclareNamespace

func (n *EntityRef) DeclareNamespace(prefix, uri string) error

DeclareNamespace declares a namespace on this node without making it the node's active namespace (libxml2: xmlNewNs).

func (*EntityRef) Name

func (n *EntityRef) Name() string

func (EntityRef) Namespace

func (n EntityRef) Namespace() *Namespace

func (EntityRef) Namespaces

func (n EntityRef) Namespaces() []*Namespace

func (EntityRef) Prefix

func (n EntityRef) Prefix() string

func (*EntityRef) RemoveNamespaceByPrefix

func (n *EntityRef) RemoveNamespaceByPrefix(prefix string) bool

RemoveNamespaceByPrefix removes a namespace declaration with the given prefix. Returns true if a declaration was removed.

func (*EntityRef) Replace

func (e *EntityRef) Replace(nodes ...Node) error

func (*EntityRef) SetActiveNamespace

func (n *EntityRef) SetActiveNamespace(prefix, uri string) error

SetActiveNamespace declares a namespace and sets it as this node's active namespace (libxml2: xmlSetNs).

func (*EntityRef) SetNs

func (n *EntityRef) SetNs(ns *Namespace)

SetNs sets the node's active namespace to an existing Namespace object without creating a new declaration.

func (*EntityRef) SetTreeDoc

func (n *EntityRef) SetTreeDoc(doc *Document)

func (EntityRef) URI

func (n EntityRef) URI() string

type Enumeration

type Enumeration []string

type ErrParseError

type ErrParseError struct {
	Column     int
	Domain     ErrorDomain
	Err        error
	File       string
	Level      ErrorLevel
	Line       string
	LineNumber int
}

ErrParseError is a structured parse error carrying the source location, context line, severity, and the underlying error. Use ErrParseError.FormatError to produce a libxml2-compatible multi-line diagnostic string. The error can be unwrapped via ErrParseError.Unwrap to access the underlying cause.

func (ErrParseError) Error

func (e ErrParseError) Error() string

Error returns a human-readable string including the file, line, column, and a context snippet showing approximately where the error occurred.

func (ErrParseError) ErrorLevel

func (e ErrParseError) ErrorLevel() ErrorLevel

ErrorLevel returns the severity of this parse error, satisfying the ErrorLeveler interface.

func (ErrParseError) FormatError

func (e ErrParseError) FormatError() string

FormatError returns a libxml2-compatible multi-line error string:

FILE:LINE: DOMAIN SEVERITY : MESSAGE
CONTEXT_LINE
     ^

func (ErrParseError) Unwrap

func (e ErrParseError) Unwrap() error

Unwrap returns the underlying error, enabling use with errors.Is and errors.As.

type ErrorCollector

type ErrorCollector struct {
	// contains filtered or unexported fields
}

ErrorCollector collects errors into a slice via an internal Sink[error]. When level is zero (ErrorLevelNone), all errors are collected. When set, only errors matching that level are collected.

Satisfies ErrorHandler and io.Closer. The parser/compiler closes it automatically at the end of the operation.

func NewErrorCollector

func NewErrorCollector(ctx context.Context, level ErrorLevel, opts ...sink.Option) *ErrorCollector

NewErrorCollector creates an ErrorCollector backed by a Sink[error]. Pass ErrorLevelNone (0) for level to collect all errors regardless of severity.

func (*ErrorCollector) Close

func (ec *ErrorCollector) Close() error

Close satisfies io.Closer. Drains the internal Sink.

func (*ErrorCollector) Errors

func (ec *ErrorCollector) Errors() []error

Errors returns a copy of the collected errors. Safe to call after Close.

func (*ErrorCollector) Handle

func (ec *ErrorCollector) Handle(ctx context.Context, err error)

Handle satisfies ErrorHandler. Sends the error to the internal Sink.

type ErrorDomain

type ErrorDomain int

ErrorDomain classifies the subsystem that produced an error.

const (
	// ErrorDomainParser indicates the error originated in the XML parser.
	ErrorDomainParser ErrorDomain = iota
	// ErrorDomainNamespace indicates the error originated in namespace
	// processing.
	ErrorDomainNamespace
)

type ErrorHandler

type ErrorHandler interface {
	Handle(context.Context, error)
}

ErrorHandler receives errors reported during parsing, compilation, or validation. Implementations may log, accumulate, or discard errors.

Handle is called synchronously at the point of error detection unless the implementation itself introduces asynchrony (e.g. Sink[error]).

Implementations must not block for extended periods.

The error value may optionally implement ErrorLeveler to indicate severity. Users can type-assert to inspect the level.

type ErrorLevel

type ErrorLevel int

ErrorLevel represents the severity of a parse error.

const (
	// ErrorLevelNone indicates no specific severity. When used with
	// [NewErrorCollector], it means all errors are collected regardless
	// of level.
	ErrorLevelNone ErrorLevel = iota
	// ErrorLevelWarning indicates a non-fatal condition that may still
	// produce correct output.
	ErrorLevelWarning
	// ErrorLevelError indicates a recoverable error in the input.
	ErrorLevelError
	// ErrorLevelFatal indicates an unrecoverable error that stops processing.
	ErrorLevelFatal
)

type ErrorLeveler

type ErrorLeveler interface {
	ErrorLevel() ErrorLevel
}

ErrorLeveler is an optional interface that errors can implement to report their severity. Errors that do not implement this interface are treated as warnings (ErrorLevelWarning).

type LoadSubsetOption

type LoadSubsetOption int
const (
	DetectIDs     LoadSubsetOption = 1 << (iota + 1)
	CompleteAttrs                  // 4
	SkipIDs                        // 8
)

func (LoadSubsetOption) IsSet

func (*LoadSubsetOption) Set

type LocalNamePredicate

type LocalNamePredicate string

LocalNamePredicate matches an attribute by local name only. If multiple attributes share the same local name, FindAttribute returns the first match in property order.

func (LocalNamePredicate) Match

func (p LocalNamePredicate) Match(a *Attribute) bool

type MutableNode

type MutableNode interface {
	Node

	AddChild(Node) error
	AddSibling(Node) error
	// AppendText appends text content to this node (libxml2: xmlNodeAddContent).
	AppendText([]byte) error
	Replace(...Node) error
	SetLine(int)
	SetNextSibling(Node)
	SetOwnerDocument(doc *Document)
	SetParent(Node)
	SetPrevSibling(Node)
	SetTreeDoc(doc *Document)
}

MutableNode extends Node with tree-mutation operations.

type NSPredicate

type NSPredicate struct {
	Local        string
	NamespaceURI string
}

NSPredicate matches an attribute by local name + namespace URI.

func (NSPredicate) Match

func (p NSPredicate) Match(a *Attribute) bool

type Namespace

type Namespace struct {
	// contains filtered or unexported fields
}

Namespace represents an XML namespace declaration (libxml2: xmlNs).

func LookupNSByHref

func LookupNSByHref(e *Element, href string) *Namespace

LookupNSByHref walks the element and its ancestors to find a namespace declaration matching the given URI.

func LookupNSByPrefix

func LookupNSByPrefix(e *Element, prefix string) *Namespace

LookupNSByPrefix walks the element and its ancestors to find a namespace declaration matching the given prefix. The "xml" prefix is always implicitly bound to the XML namespace.

func NewNamespace

func NewNamespace(prefix, uri string) *Namespace

NewNamespace creates a new Namespace with the given prefix and URI.

func (Namespace) Prefix

func (n Namespace) Prefix() string

func (Namespace) URI

func (n Namespace) URI() string

type NamespaceContainer

type NamespaceContainer interface {
	Namespaces() []*Namespace
}

NamespaceContainer is an interface for nodes that carry namespace declarations.

type NamespaceNodeWrapper

type NamespaceNodeWrapper struct {
	// contains filtered or unexported fields
}

NamespaceNodeWrapper wraps a Namespace to implement the Node interface for XPath namespace axis traversal. In XPath, namespace nodes appear as nodes with a name (the prefix), content (the URI), and a parent (the owning element).

func NewNamespaceNodeWrapper

func NewNamespaceNodeWrapper(ns *Namespace, owner Node) *NamespaceNodeWrapper

NewNamespaceNodeWrapper creates a Node that wraps a Namespace for XPath use.

func (*NamespaceNodeWrapper) Content

func (n *NamespaceNodeWrapper) Content() []byte

func (NamespaceNodeWrapper) FirstChild

func (n NamespaceNodeWrapper) FirstChild() Node

func (NamespaceNodeWrapper) LastChild

func (n NamespaceNodeWrapper) LastChild() Node

func (NamespaceNodeWrapper) Line

func (n NamespaceNodeWrapper) Line() int

func (NamespaceNodeWrapper) LocalName

func (n NamespaceNodeWrapper) LocalName() string

func (NamespaceNodeWrapper) Name

func (n NamespaceNodeWrapper) Name() string

func (NamespaceNodeWrapper) NextSibling

func (n NamespaceNodeWrapper) NextSibling() Node

func (NamespaceNodeWrapper) OwnerDocument

func (n NamespaceNodeWrapper) OwnerDocument() *Document

func (NamespaceNodeWrapper) Parent

func (n NamespaceNodeWrapper) Parent() Node

func (NamespaceNodeWrapper) PrevSibling

func (n NamespaceNodeWrapper) PrevSibling() Node

func (*NamespaceNodeWrapper) SetLine

func (n *NamespaceNodeWrapper) SetLine(line int)

func (*NamespaceNodeWrapper) SetNextSibling

func (n *NamespaceNodeWrapper) SetNextSibling(cur Node)

func (*NamespaceNodeWrapper) SetOwnerDocument

func (n *NamespaceNodeWrapper) SetOwnerDocument(doc *Document)

func (*NamespaceNodeWrapper) SetParent

func (n *NamespaceNodeWrapper) SetParent(cur Node)

func (*NamespaceNodeWrapper) SetPrevSibling

func (n *NamespaceNodeWrapper) SetPrevSibling(cur Node)

func (NamespaceNodeWrapper) Type

func (n NamespaceNodeWrapper) Type() ElementType

type Namespacer

type Namespacer interface {
	Namespace() *Namespace
	Namespaces() []*Namespace
	Prefix() string
	URI() string
	LocalName() string
}

Namespacer is an interface for things that have a namespace prefix and URI.

type NilErrorHandler

type NilErrorHandler struct{}

NilErrorHandler is an ErrorHandler that discards all errors. Use as a default when no handler is provided.

func (NilErrorHandler) Handle

type Node

type Node interface {
	Content() []byte
	FirstChild() Node
	LastChild() Node
	Line() int
	Name() string
	NextSibling() Node
	OwnerDocument() *Document
	Parent() Node
	PrevSibling() Node
	Type() ElementType
	// contains filtered or unexported methods
}

Node is a read-only view of an XML document tree node (libxml2: xmlNode).

func CopyNode

func CopyNode(src Node, targetDoc *Document) (Node, error)

CopyNode creates a deep copy of src, owned by targetDoc. Supports Element, Text, Comment, CDATASection, PI, and EntityRef nodes.

type NodeWalker

type NodeWalker interface {
	Visit(Node) error
}

NodeWalker visits nodes during tree traversal.

type NodeWalkerFunc

type NodeWalkerFunc func(Node) error

NodeWalkerFunc is an adapter to allow use of ordinary functions as NodeWalker. Similar to http.HandlerFunc.

func (NodeWalkerFunc) Visit

func (f NodeWalkerFunc) Visit(n Node) error

type Notation

type Notation struct {
	// contains filtered or unexported fields
}

Notation is a notation declaration from a DTD.

func (*Notation) AddChild

func (n *Notation) AddChild(cur Node) error

AddChild appends cur as the last child of the notation node.

func (*Notation) AddSibling

func (n *Notation) AddSibling(cur Node) error

AddSibling appends cur as the last sibling of the notation node.

func (*Notation) AppendText

func (n *Notation) AppendText(b []byte) error

AppendText appends b as a Text child of the notation node.

func (Notation) Content

func (n Notation) Content() []byte

func (Notation) FirstChild

func (n Notation) FirstChild() Node

func (*Notation) Free

func (n *Notation) Free()

Free is a no-op; it exists to satisfy the Node interface.

func (Notation) LastChild

func (n Notation) LastChild() Node

func (Notation) Line

func (n Notation) Line() int

func (Notation) LocalName

func (n Notation) LocalName() string

func (Notation) Name

func (n Notation) Name() string

func (Notation) NextSibling

func (n Notation) NextSibling() Node

func (Notation) OwnerDocument

func (n Notation) OwnerDocument() *Document

func (Notation) Parent

func (n Notation) Parent() Node

func (Notation) PrevSibling

func (n Notation) PrevSibling() Node

func (*Notation) Replace

func (n *Notation) Replace(nodes ...Node) error

Replace swaps the notation node out of its parent, inserting nodes in its place.

func (*Notation) SetLine

func (n *Notation) SetLine(line int)

func (*Notation) SetNextSibling

func (n *Notation) SetNextSibling(cur Node)

func (*Notation) SetOwnerDocument

func (n *Notation) SetOwnerDocument(doc *Document)

func (*Notation) SetParent

func (n *Notation) SetParent(cur Node)

func (*Notation) SetPrevSibling

func (n *Notation) SetPrevSibling(cur Node)

func (*Notation) SetTreeDoc

func (n *Notation) SetTreeDoc(doc *Document)

SetTreeDoc sets the owning document of the notation node and its subtree.

func (Notation) Type

func (n Notation) Type() ElementType

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser holds configuration for XML parsing (libxml2: xmlParserCtxt). It uses clone-on-write semantics: each builder method returns a new Parser sharing the underlying config until mutation.

func NewParser

func NewParser() Parser

NewParser creates a new Parser with default settings.

func (Parser) AllowNetwork

func (p Parser) AllowNetwork(v bool) Parser

AllowNetwork controls whether the parser is allowed to fetch resources over the network (e.g. external DTDs, entities). When set to false, all network access is forbidden. libxml2: XML_PARSE_NONET (note: semantics are inverted — libxml2 sets this flag to *forbid* network access, whereas AllowNetwork(true) *permits* it) Default: true (network access is allowed)

func (Parser) BaseURI

func (p Parser) BaseURI(uri string) Parser

BaseURI sets the document's base URI, used for resolving relative references such as external DTD system identifiers.

func (Parser) BigLineNumbers

func (p Parser) BigLineNumbers(v bool) Parser

BigLineNumbers controls whether large line numbers are stored in the text PSVI field, allowing line numbers above 65535. libxml2: XML_PARSE_BIG_LINES Default: false

func (Parser) BlockXXE

func (p Parser) BlockXXE(v bool) Parser

BlockXXE controls whether loading of external entities and DTDs is blocked, preventing XML External Entity (XXE) attacks. libxml2: XML_PARSE_NOXXE Default: false

func (Parser) Catalog

func (p Parser) Catalog(c CatalogResolver) Parser

Catalog sets an XML Catalog for resolving external entity identifiers (public/system IDs) during parsing. When set, the parser consults the catalog before attempting to load external DTDs and entities.

func (Parser) CharBufferSize

func (p Parser) CharBufferSize(size int) Parser

CharBufferSize sets the maximum number of bytes delivered in a single Characters or IgnorableWhitespace SAX callback. When size <= 0 (the default), all character data is delivered in one call. When size > 0, data longer than size bytes is split into chunks of at most size bytes, always respecting UTF-8 character boundaries.

func (Parser) CleanNamespaces

func (p Parser) CleanNamespaces(v bool) Parser

CleanNamespaces controls whether redundant namespace declarations are removed from the resulting DOM tree. libxml2: XML_PARSE_NSCLEAN Default: false

func (Parser) CompactTextNodes

func (p Parser) CompactTextNodes(v bool) Parser

CompactTextNodes controls whether the parser compacts small text nodes to reduce memory usage. libxml2: XML_PARSE_COMPACT Default: false

func (Parser) DefaultDTDAttributes

func (p Parser) DefaultDTDAttributes(v bool) Parser

DefaultDTDAttributes controls whether the parser adds default attributes defined in the DTD. When set to true, also enables LoadExternalDTD. libxml2: XML_PARSE_DTDATTR Default: false

func (Parser) ErrorHandler

func (p Parser) ErrorHandler(h ErrorHandler) Parser

ErrorHandler sets the handler for validation errors produced during DTD validation ([ValidateDTD]). When set, individual errors are delivered to the handler as they occur. The returned error from Parse is ErrDTDValidationFailed on failure.

func (Parser) FS added in v0.0.2

func (p Parser) FS(fsys fs.FS) Parser

FS sets the fs.FS used to load external resources referenced by the document — external DTDs ([LoadExternalDTD]) and external entities resolved through TreeBuilder.ResolveEntity. A nil value restores the default, which opens any path supplied to the parser via os.Open.

Note: the names handed to the FS are built with filepath.Join against the document's base URI, so they may be absolute and may use OS-specific separators on Windows. FS implementations that enforce fs.ValidPath (notably os.DirFS and testing/fstest.MapFS) will reject those names. Sandboxing the loader behind such an FS requires path normalization that is not yet performed by this package; for now, supply an FS implementation that accepts OS-style names.

func (Parser) FixBaseURIs

func (p Parser) FixBaseURIs(v bool) Parser

FixBaseURIs controls whether xml:base URIs are fixed up during XInclude processing. When set to false, xml:base attributes are not adjusted on included content. libxml2: XML_PARSE_NOBASEFIX (note: semantics are inverted — libxml2 sets this flag to *disable* fixup, whereas FixBaseURIs(false) disables it) Default: true (xml:base URIs are fixed up)

func (Parser) IgnoreEncoding

func (p Parser) IgnoreEncoding(v bool) Parser

IgnoreEncoding controls whether the parser ignores the encoding declaration inside the document and uses the transport-level encoding instead. libxml2: XML_PARSE_IGNORE_ENC Default: false

func (Parser) LenientXMLDecl

func (p Parser) LenientXMLDecl(v bool) Parser

LenientXMLDecl relaxes XML declaration parsing so that the version, encoding, and standalone pseudo-attributes may appear in any order. Per the XML spec (section 2.8) the order MUST be version, encoding, standalone, but some real-world producers emit them differently. This is a helium extension not present in libxml2. Default: false

func (Parser) LoadExternalDTD

func (p Parser) LoadExternalDTD(v bool) Parser

LoadExternalDTD controls whether the parser loads the external DTD subset. libxml2: XML_PARSE_DTDLOAD Default: false

func (Parser) MaxDepth

func (p Parser) MaxDepth(depth int) Parser

MaxDepth sets the maximum element nesting depth allowed during parsing. When depth is greater than zero, the parser returns an error if the input document contains elements nested deeper than this limit. A value of zero (the default) means no limit is enforced.

func (Parser) MaxExternalDTDBytes added in v0.2.0

func (p Parser) MaxExternalDTDBytes(n int) Parser

MaxExternalDTDBytes sets the maximum number of bytes read from an external DTD subset (see [LoadExternalDTD], [ValidateDTD], [DefaultDTDAttributes]). The cap is enforced against the actual number of bytes read, guarding against hostile or pathological sources (e.g. /dev/zero) that could otherwise exhaust memory before any entity or parse limits apply. A value less than or equal to zero (the default) means MaxExternalDTDSize (10 MiB) is used.

func (Parser) MergeCDATA

func (p Parser) MergeCDATA(v bool) Parser

MergeCDATA controls whether CDATA sections are merged into adjacent text nodes instead of being represented as separate CDATA nodes. libxml2: XML_PARSE_NOCDATA Default: false

func (Parser) NewPushParser

func (p Parser) NewPushParser(ctx context.Context) *PushParser

NewPushParser creates a PushParser using the given Parser's configuration. The parser runs in a background goroutine, reading from the internal stream as data is pushed.

func (Parser) Parse

func (p Parser) Parse(ctx context.Context, b []byte) (*Document, error)

Parse parses XML from a byte slice and returns the resulting Document (libxml2: xmlParseDoc / xmlParseMemory).

When [ValidateDTD] is enabled and the document fails validation, the returned error is ErrDTDValidationFailed and the document is still returned. Individual validation errors are delivered to the ErrorHandler configured via Parser.ErrorHandler.

Cancellation: if ctx is cancelled or its deadline is exceeded, Parse aborts and returns the context error (matched by errors.Is against context.Canceled / context.DeadlineExceeded) with a nil Document — never a partial tree. Because Parse reads from an in-memory byte slice there is no blocking read, so cancellation is always observed promptly: the parser checks the context between parse steps and between cursor refills.

func (Parser) ParseFile

func (p Parser) ParseFile(ctx context.Context, path string) (*Document, error)

ParseFile reads and parses an XML file. The document's URL is set to the absolute path of the file, and the file path is used as the base URI for relative URI resolution during parsing.

func (Parser) ParseInNodeContext

func (p Parser) ParseInNodeContext(ctx context.Context, node Node, data []byte) (Node, error)

ParseInNodeContext parses an XML fragment in the context of an existing node. The node provides in-scope namespace declarations and document-level DTD/entity context. Returns the first node of the parsed fragment list (siblings linked via NextSibling). The returned nodes are not attached to any parent.

func (Parser) ParseReader

func (p Parser) ParseReader(ctx context.Context, r io.Reader) (*Document, error)

ParseReader parses XML from an io.Reader and returns the resulting Document (libxml2: xmlReadIO). This is identical to [Parse] but reads from a stream instead of a byte slice. See [Parse] for DTD validation error handling.

EBCDIC encoding detection requires the full input up front: when the leading bytes carry the EBCDIC invariant prefix the reader is buffered into memory and parsed through the same path as [Parse], so an EBCDIC document parses identically via ParseReader/ParseFile as via Parse. All other inputs stream.

Cancellation: context cancellation and deadlines are observed BETWEEN read operations and parse steps. The parser checks ctx before each cursor refill (read from r) and between parse steps, so a cancelled or timed-out context is honored as soon as the parser regains control, returning the context error (matched by errors.Is against context.Canceled / context.DeadlineExceeded) with a nil Document.

A reader already blocked inside its own Read call cannot be interrupted generically: Go provides no way to unblock a Read in progress. Such a read is only interruptible if r itself honors the context or a deadline — for example a reader that sets a read deadline when ctx.Done() fires, or that returns from Read with an error on cancellation. If r can block indefinitely (e.g. a slow or never-returning network reader), wrap it so its Read observes ctx, or pass the already-read bytes to [Parse] instead.

func (Parser) PedanticErrors

func (p Parser) PedanticErrors(v bool) Parser

PedanticErrors controls whether the parser reports pedantic warnings for minor specification violations. libxml2: XML_PARSE_PEDANTIC Default: false

func (Parser) ProcessXInclude

func (p Parser) ProcessXInclude(v bool) Parser

ProcessXInclude controls whether XInclude substitution is performed during parsing. libxml2: XML_PARSE_XINCLUDE Default: false

func (Parser) RecoverOnError

func (p Parser) RecoverOnError(v bool) Parser

RecoverOnError controls whether the parser attempts to recover from well-formedness errors and returns a partial document. libxml2: XML_PARSE_RECOVER Default: false

func (Parser) RelaxLimits

func (p Parser) RelaxLimits(v bool) Parser

RelaxLimits controls whether hardcoded parser limits (name length, entity expansion) are relaxed. Use with caution — disabling limits may expose the parser to denial-of-service attacks. libxml2: XML_PARSE_HUGE Default: false

func (Parser) ReuseDict

func (p Parser) ReuseDict(v bool) Parser

ReuseDict controls whether the parser reuses the context dictionary for interned strings. When set to false, a fresh dictionary is used. libxml2: XML_PARSE_NODICT (note: semantics are inverted — libxml2 sets this flag to *disable* dictionary reuse, whereas ReuseDict(false) disables it) Default: true (dictionary is reused)

func (Parser) SAXHandler

func (p Parser) SAXHandler(s sax.SAX2Handler) Parser

SAXHandler sets the SAX2 event handler for parsing.

func (Parser) SkipIDs

func (p Parser) SkipIDs(v bool) Parser

SkipIDs controls whether ID attribute interning is skipped during parsing. When true, the parser does not build the ID table. libxml2: XML_PARSE_SKIP_IDS Default: false

func (Parser) StripBlanks

func (p Parser) StripBlanks(v bool) Parser

StripBlanks controls whether whitespace-only text nodes are removed from the resulting DOM tree. libxml2: XML_PARSE_NOBLANKS Default: false

func (Parser) SubstituteEntities

func (p Parser) SubstituteEntities(v bool) Parser

SubstituteEntities controls whether entity references are replaced with their substitution text during parsing. libxml2: XML_PARSE_NOENT Default: false

func (Parser) SuppressErrors

func (p Parser) SuppressErrors(v bool) Parser

SuppressErrors controls whether error reports from the parser are suppressed. When true, the SAX error callback is not invoked. libxml2: XML_PARSE_NOERROR Default: false

func (Parser) SuppressWarnings

func (p Parser) SuppressWarnings(v bool) Parser

SuppressWarnings controls whether warning reports from the parser are suppressed. When true, the SAX warning callback is not invoked. libxml2: XML_PARSE_NOWARNING Default: false

func (Parser) ValidateDTD

func (p Parser) ValidateDTD(v bool) Parser

ValidateDTD controls whether the parser validates the document against its DTD after parsing. When set to true, also enables LoadExternalDTD. libxml2: XML_PARSE_DTDVALID Default: false

func (Parser) XIncludeNodes

func (p Parser) XIncludeNodes(v bool) Parser

XIncludeNodes controls whether XINCLUDE START/END marker nodes are generated in the DOM tree during XInclude processing. libxml2: XML_PARSE_NOXINCNODE (note: semantics are inverted — libxml2 sets this flag to *suppress* marker nodes, whereas XIncludeNodes(false) suppresses them) Default: true (marker nodes are generated)

type ProcessingInstruction

type ProcessingInstruction struct {
	// contains filtered or unexported fields
}

ProcessingInstruction represents an XML processing instruction node (libxml2: xmlNode with type XML_PI_NODE).

func (*ProcessingInstruction) AddChild

func (p *ProcessingInstruction) AddChild(cur Node) error

AddChild on a PI does not attach child nodes. A processing instruction carries its content as a string (the "data" portion), not as element/text children. This mirrors libxml2, where an xmlNode of type XML_PI_NODE stores its content in the node's content string and has no element/text children.

A Text/CDATA child has its content appended to the PI data (mirroring xmlNodeAddContent on a PI). Any other node type is rejected, since attaching it would corrupt the tree and break serialization.

func (*ProcessingInstruction) AddSibling

func (p *ProcessingInstruction) AddSibling(cur Node) error

func (*ProcessingInstruction) AppendText

func (p *ProcessingInstruction) AppendText(b []byte) error

AppendText appends text to the PI's data string rather than creating a child text node. See AddChild for rationale.

func (*ProcessingInstruction) Content

func (p *ProcessingInstruction) Content() []byte

func (ProcessingInstruction) FirstChild

func (n ProcessingInstruction) FirstChild() Node

func (ProcessingInstruction) LastChild

func (n ProcessingInstruction) LastChild() Node

func (ProcessingInstruction) Line

func (n ProcessingInstruction) Line() int

func (ProcessingInstruction) LocalName

func (n ProcessingInstruction) LocalName() string

func (*ProcessingInstruction) Name

func (p *ProcessingInstruction) Name() string

func (ProcessingInstruction) NextSibling

func (n ProcessingInstruction) NextSibling() Node

func (ProcessingInstruction) OwnerDocument

func (n ProcessingInstruction) OwnerDocument() *Document

func (ProcessingInstruction) Parent

func (n ProcessingInstruction) Parent() Node

func (ProcessingInstruction) PrevSibling

func (n ProcessingInstruction) PrevSibling() Node

func (*ProcessingInstruction) Replace

func (p *ProcessingInstruction) Replace(nodes ...Node) error

func (*ProcessingInstruction) SetLine

func (n *ProcessingInstruction) SetLine(line int)

func (*ProcessingInstruction) SetNextSibling

func (n *ProcessingInstruction) SetNextSibling(cur Node)

func (*ProcessingInstruction) SetOwnerDocument

func (n *ProcessingInstruction) SetOwnerDocument(doc *Document)

func (*ProcessingInstruction) SetParent

func (n *ProcessingInstruction) SetParent(cur Node)

func (*ProcessingInstruction) SetPrevSibling

func (n *ProcessingInstruction) SetPrevSibling(cur Node)

func (*ProcessingInstruction) SetTreeDoc

func (p *ProcessingInstruction) SetTreeDoc(doc *Document)

func (*ProcessingInstruction) Type

type PushParser

type PushParser = push.Parser[*Document]

PushParser provides an incremental XML parsing interface (libxml2: xmlParserCtxt in push mode). Data is pushed via Push or Write, and the parser processes tokens as they become available in a background goroutine. Call [PushParser.Close] to signal end-of-input and retrieve the parsed Document.

type QNamePredicate

type QNamePredicate string

QNamePredicate matches an attribute by QName as returned by Attribute.Name.

func (QNamePredicate) Match

func (p QNamePredicate) Match(a *Attribute) bool

type SubstitutionType

type SubstitutionType int
const (
	SubstituteNone SubstitutionType = iota
	SubstituteRef
	SubstitutePERef
	SubstituteBoth
)

type Text

type Text struct {
	// contains filtered or unexported fields
}

Text represents an XML text node (libxml2: xmlNode with type XML_TEXT_NODE).

func (*Text) AddChild

func (n *Text) AddChild(cur Node) error

func (*Text) AddNamespaceDecl added in v0.3.0

func (n *Text) AddNamespaceDecl(ns *Namespace)

AddNamespaceDecl appends an existing Namespace to this node's declarations (nsDefs) without allocating a new one. Unlike DeclareNamespace it does not create a fresh Namespace, so a caller building a tree can reuse one Namespace object as both the declaration and an element's active namespace. The caller owns ns; it must not be shared as a declaration across nodes that could be mutated independently.

func (*Text) AddSibling

func (n *Text) AddSibling(cur Node) error

func (*Text) AppendText

func (n *Text) AppendText(b []byte) error

func (Text) Content

func (n Text) Content() []byte

Content returns a defensive copy of the text node's content. Mutating the returned slice does NOT affect the node; re-reading returns the original bytes.

func (*Text) DeclareNamespace

func (n *Text) DeclareNamespace(prefix, uri string) error

DeclareNamespace declares a namespace on this node without making it the node's active namespace (libxml2: xmlNewNs).

func (*Text) Name

func (n *Text) Name() string

func (Text) Namespace

func (n Text) Namespace() *Namespace

func (Text) Namespaces

func (n Text) Namespaces() []*Namespace

func (Text) Prefix

func (n Text) Prefix() string

func (*Text) RemoveNamespaceByPrefix

func (n *Text) RemoveNamespaceByPrefix(prefix string) bool

RemoveNamespaceByPrefix removes a namespace declaration with the given prefix. Returns true if a declaration was removed.

func (*Text) Replace

func (n *Text) Replace(nodes ...Node) error

func (*Text) SetActiveNamespace

func (n *Text) SetActiveNamespace(prefix, uri string) error

SetActiveNamespace declares a namespace and sets it as this node's active namespace (libxml2: xmlSetNs).

func (*Text) SetNs

func (n *Text) SetNs(ns *Namespace)

SetNs sets the node's active namespace to an existing Namespace object without creating a new declaration.

func (*Text) SetTreeDoc

func (n *Text) SetTreeDoc(doc *Document)

func (Text) URI

func (n Text) URI() string

type TreeBuilder

type TreeBuilder struct{}

TreeBuilder is a SAX2 handler that builds a DOM tree from SAX events, analogous to libxml2's default SAX handler (xmlSAX2InitDefaultSAXHandler).

func NewTreeBuilder

func NewTreeBuilder() *TreeBuilder

NewTreeBuilder creates a new TreeBuilder that builds a DOM tree from SAX events.

func (*TreeBuilder) AttributeDecl

func (t *TreeBuilder) AttributeDecl(ctxif context.Context, eName string, aName string, typ enum.AttributeType, deftype enum.AttributeDefault, value string, enumif sax.Enumeration) error

func (*TreeBuilder) CDataBlock

func (t *TreeBuilder) CDataBlock(ctxif context.Context, data []byte) error

CDataBlock mirrors xmlSAX2Text(ctxt, value, len, XML_CDATA_SECTION_NODE) in libxml2's SAX2.c. Unlike text nodes, adjacent CDATA sections are NOT merged — each callback creates a new CDATASection node.

func (*TreeBuilder) Characters

func (t *TreeBuilder) Characters(ctxif context.Context, data []byte) error

func (*TreeBuilder) Comment

func (t *TreeBuilder) Comment(ctxif context.Context, data []byte) error

Comment mirrors xmlSAX2Comment in libxml2's SAX2.c, which delegates parent selection to xmlSAX2AppendChild. When inside a DTD subset the comment is added to the DTD, not the document.

func (*TreeBuilder) ElementDecl

func (t *TreeBuilder) ElementDecl(ctxif context.Context, name string, typ enum.ElementType, content sax.ElementContent) error

func (*TreeBuilder) EndDTD

func (t *TreeBuilder) EndDTD(ctxif context.Context) error

func (*TreeBuilder) EndDocument

func (t *TreeBuilder) EndDocument(ctxif context.Context) error

func (*TreeBuilder) EndElementNS

func (t *TreeBuilder) EndElementNS(ctxif context.Context, localname, prefix, uri string) error

func (*TreeBuilder) EndEntity

func (t *TreeBuilder) EndEntity(ctxif context.Context, name string) error

func (*TreeBuilder) EntityDecl

func (t *TreeBuilder) EntityDecl(ctxif context.Context, name string, typ enum.EntityType, publicID string, systemID string, notation string) error

func (*TreeBuilder) Error

func (t *TreeBuilder) Error(ctxif context.Context, err error) error

func (*TreeBuilder) ExternalEntityDecl

func (t *TreeBuilder) ExternalEntityDecl(ctxif context.Context, name string, publicID string, systemID string) error

func (*TreeBuilder) ExternalSubset

func (t *TreeBuilder) ExternalSubset(ctxif context.Context, name, eid, uri string) error

func (*TreeBuilder) GetEntity

func (t *TreeBuilder) GetEntity(ctxif context.Context, name string) (ent sax.Entity, err error)

func (*TreeBuilder) GetExternalSubset

func (t *TreeBuilder) GetExternalSubset(ctxif context.Context, name string, baseURI string) error

func (*TreeBuilder) GetParameterEntity

func (t *TreeBuilder) GetParameterEntity(ctxif context.Context, name string) (sax.Entity, error)

func (*TreeBuilder) HasExternalSubset

func (t *TreeBuilder) HasExternalSubset(ctxif context.Context) (bool, error)

func (*TreeBuilder) HasInternalSubset

func (t *TreeBuilder) HasInternalSubset(ctxif context.Context) (bool, error)

func (*TreeBuilder) IgnorableWhitespace

func (t *TreeBuilder) IgnorableWhitespace(ctxif context.Context, content []byte) error

func (*TreeBuilder) InternalEntityDecl

func (t *TreeBuilder) InternalEntityDecl(ctxif context.Context, name string, value string) error

func (*TreeBuilder) InternalSubset

func (t *TreeBuilder) InternalSubset(ctxif context.Context, name, eid, uri string) error

func (*TreeBuilder) IsStandalone

func (t *TreeBuilder) IsStandalone(ctxif context.Context) (bool, error)

func (*TreeBuilder) NotationDecl

func (t *TreeBuilder) NotationDecl(ctxif context.Context, name string, publicID string, systemID string) error

func (*TreeBuilder) ProcessingInstruction

func (t *TreeBuilder) ProcessingInstruction(ctxif context.Context, target, data string) error

func (*TreeBuilder) Reference

func (t *TreeBuilder) Reference(ctxif context.Context, name string) error

func (*TreeBuilder) ResolveEntity

func (t *TreeBuilder) ResolveEntity(ctxif context.Context, publicID string, systemID string) (sax.ParseInput, error)

func (*TreeBuilder) SetDocumentLocator

func (t *TreeBuilder) SetDocumentLocator(ctxif context.Context, loc sax.DocumentLocator) error

func (*TreeBuilder) SkippedEntity

func (t *TreeBuilder) SkippedEntity(ctxif context.Context, name string) error

func (*TreeBuilder) StartDTD

func (t *TreeBuilder) StartDTD(ctxif context.Context, name string, publicID string, systemID string) error

func (*TreeBuilder) StartDocument

func (t *TreeBuilder) StartDocument(ctxif context.Context) error

func (*TreeBuilder) StartElementNS

func (t *TreeBuilder) StartElementNS(ctxif context.Context, localname, prefix, uri string, namespaces []sax.Namespace, attrs []sax.Attribute) error

func (*TreeBuilder) StartEntity

func (t *TreeBuilder) StartEntity(ctxif context.Context, name string) error

func (*TreeBuilder) UnparsedEntityDecl

func (t *TreeBuilder) UnparsedEntityDecl(ctxif context.Context, name string, publicID string, systemID string, notation string) error

func (*TreeBuilder) Warning

func (t *TreeBuilder) Warning(ctxif context.Context, err error) error

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer serializes an XML document tree (libxml2: xmlSaveCtxt).

It is a value-style wrapper: fluent methods return updated copies and the original is never mutated. Mutable runtime state (indent depth, resolved escapeNonASCII flag, XHTML detection) lives in a writeSession created inside each terminal method.

func NewWriter

func NewWriter() Writer

NewWriter creates a new Writer with default settings.

func (Writer) AllowPrefixUndeclarations

func (w Writer) AllowPrefixUndeclarations(v bool) Writer

AllowPrefixUndeclarations controls whether xmlns:prefix="" undeclarations may be emitted.

func (Writer) EscapeNonASCII

func (w Writer) EscapeNonASCII(v bool) Writer

EscapeNonASCII controls whether non-ASCII characters are escaped as numeric character references when serializing UTF-8 output.

func (Writer) Format

func (w Writer) Format(v bool) Writer

Format controls whether indented (pretty-printed) output is emitted.

func (Writer) IncludeDTD

func (w Writer) IncludeDTD(v bool) Writer

IncludeDTD controls whether DTD nodes are emitted.

func (Writer) IndentString

func (w Writer) IndentString(s string) Writer

IndentString sets the string used for each indent level.

func (Writer) SelfCloseEmptyElements

func (w Writer) SelfCloseEmptyElements(v bool) Writer

SelfCloseEmptyElements controls whether empty elements are serialized as self-closing tags (for example, <br/>). When false, they are emitted as explicit open+close pairs (for example, <br></br>).

func (Writer) WriteTo

func (d Writer) WriteTo(out io.Writer, node Node) error

WriteTo serializes a node (document or element) to the given writer. When the node is a Document, document-level setup (encoding, XHTML detection, DTD filtering) is applied automatically.

func (Writer) XMLDeclaration

func (w Writer) XMLDeclaration(v bool) Writer

XMLDeclaration controls whether the XML declaration is emitted.

type XIncludeMarker

type XIncludeMarker struct {
	// contains filtered or unexported fields
}

XIncludeMarker is a marker node used to bracket XInclude-included content.

func NewXIncludeMarker

func NewXIncludeMarker(doc *Document, etype ElementType, name string) *XIncludeMarker

NewXIncludeMarker creates an XInclude marker node (start or end).

func (*XIncludeMarker) AddChild

func (n *XIncludeMarker) AddChild(cur Node) error

func (*XIncludeMarker) AddNamespaceDecl added in v0.3.0

func (n *XIncludeMarker) AddNamespaceDecl(ns *Namespace)

AddNamespaceDecl appends an existing Namespace to this node's declarations (nsDefs) without allocating a new one. Unlike DeclareNamespace it does not create a fresh Namespace, so a caller building a tree can reuse one Namespace object as both the declaration and an element's active namespace. The caller owns ns; it must not be shared as a declaration across nodes that could be mutated independently.

func (*XIncludeMarker) AddSibling

func (n *XIncludeMarker) AddSibling(cur Node) error

func (*XIncludeMarker) AppendText

func (n *XIncludeMarker) AppendText(b []byte) error

func (*XIncludeMarker) DeclareNamespace

func (n *XIncludeMarker) DeclareNamespace(prefix, uri string) error

DeclareNamespace declares a namespace on this node without making it the node's active namespace (libxml2: xmlNewNs).

func (*XIncludeMarker) Name

func (n *XIncludeMarker) Name() string

func (XIncludeMarker) Namespace

func (n XIncludeMarker) Namespace() *Namespace

func (XIncludeMarker) Namespaces

func (n XIncludeMarker) Namespaces() []*Namespace

func (XIncludeMarker) Prefix

func (n XIncludeMarker) Prefix() string

func (*XIncludeMarker) RemoveNamespaceByPrefix

func (n *XIncludeMarker) RemoveNamespaceByPrefix(prefix string) bool

RemoveNamespaceByPrefix removes a namespace declaration with the given prefix. Returns true if a declaration was removed.

func (*XIncludeMarker) Replace

func (n *XIncludeMarker) Replace(nodes ...Node) error

func (*XIncludeMarker) SetActiveNamespace

func (n *XIncludeMarker) SetActiveNamespace(prefix, uri string) error

SetActiveNamespace declares a namespace and sets it as this node's active namespace (libxml2: xmlSetNs).

func (*XIncludeMarker) SetNs

func (n *XIncludeMarker) SetNs(ns *Namespace)

SetNs sets the node's active namespace to an existing Namespace object without creating a new declaration.

func (*XIncludeMarker) SetTreeDoc

func (n *XIncludeMarker) SetTreeDoc(doc *Document)

func (XIncludeMarker) URI

func (n XIncludeMarker) URI() string

Directories

Path Synopsis
Package bench contains parse benchmarks for helium vs libxml2.
Package bench contains parse benchmarks for helium vs libxml2.
Package c14n implements XML canonicalization (C14N) as defined by the W3C specifications: Canonical XML 1.0, Exclusive Canonical XML 1.0, and Canonical XML 1.1.
Package c14n implements XML canonicalization (C14N) as defined by the W3C specifications: Canonical XML 1.0, Exclusive Canonical XML 1.0, and Canonical XML 1.1.
Package catalog implements OASIS XML Catalog resolution for public/system identifiers and URIs.
Package catalog implements OASIS XML Catalog resolution for public/system identifiers and URIs.
cmd
helium command
Package enum centralizes shared enumeration symbols used across packages.
Package enum centralizes shared enumeration symbols used across packages.
Package html implements an HTML parser compatible with libxml2's HTMLparser.
Package html implements an HTML parser compatible with libxml2's HTMLparser.
internal
catalog
Package catalog implements OASIS XML Catalog resolution for public/system identifiers and URIs.
Package catalog implements OASIS XML Catalog resolution for public/system identifiers and URIs.
domutil
Package domutil hosts small DOM/QName helpers shared by several helium processing packages (c14n, xmldsig1, xmlenc1, xpath1, xpath3, xslt3).
Package domutil hosts small DOM/QName helpers shared by several helium processing packages (c14n, xmldsig1, xmlenc1, xpath1, xpath3, xslt3).
encoding
Package encoding wraps around the various encoding stuff in golang.org/x/text/encoding.
Package encoding wraps around the various encoding stuff in golang.org/x/text/encoding.
heliumtest
Package heliumtest provides test helpers shared across helium packages.
Package heliumtest provides test helpers shared across helium packages.
icu
iofs
Package iofs provides the default fs.FS used by helium for opening external resources (DTDs, entities, schemas, XInclude targets).
Package iofs provides the default fs.FS used by helium for opening external resources (DTDs, entities, schemas, XInclude targets).
iolimit
Package iolimit provides a bounded read-all helper shared by the parser, xinclude, and xslt3 resource loaders.
Package iolimit provides a bounded read-all helper shared by the parser, xinclude, and xslt3 resource loaders.
lexicon
Package lexicon defines shared spec vocabulary reused across helium packages.
Package lexicon defines shared spec vocabulary reused across helium packages.
sequence
Package sequence provides generic sequence types for ordered collections with lazy evaluation support.
Package sequence provides generic sequence types for ordered collections with lazy evaluation support.
strcursor
Package strcursor provides buffered cursor types for reading runes and bytes from an io.Reader.
Package strcursor provides buffered cursor types for reading runes and bytes from an io.Reader.
unparsedtext
Package unparsedtext implements the text resource loading pipeline for XPath 3.1 fn:unparsed-text, fn:unparsed-text-available, fn:unparsed-text-lines, and the document retrieval that backs fn:doc / fn:json-doc.
Package unparsedtext implements the text resource loading pipeline for XPath 3.1 fn:unparsed-text, fn:unparsed-text-available, fn:unparsed-text-lines, and the document retrieval that backs fn:doc / fn:json-doc.
xmlbase64
Package xmlbase64 decodes xs:base64Binary lexical values that may contain interspersed XML whitespace (space, tab, CR, LF).
Package xmlbase64 decodes xs:base64Binary lexical values that may contain interspersed XML whitespace (space, tab, CR, LF).
xmlchar
Package xmlchar provides XML 1.0 NCName character classification functions.
Package xmlchar provides XML 1.0 NCName character classification functions.
xpathstream
Package xpathstream provides streamability analysis helpers for XPath 3.1 expressions.
Package xpathstream provides streamability analysis helpers for XPath 3.1 expressions.
Package push provides a generic push parser that accepts data in chunks and parses it in a background goroutine.
Package push provides a generic push parser that accepts data in chunks and parses it in a background goroutine.
Package relaxng implements RELAX NG (XML syntax) schema compilation and validation.
Package relaxng implements RELAX NG (XML syntax) schema compilation and validation.
Package sax defines the SAX2 event-driven XML parsing interface.
Package sax defines the SAX2 event-driven XML parsing interface.
Package schematron implements Schematron schema compilation and validation.
Package schematron implements Schematron schema compilation and validation.
Package shim provides a drop-in replacement for encoding/xml backed by the helium XML parser.
Package shim provides a drop-in replacement for encoding/xml backed by the helium XML parser.
Package sink provides a generic, channel-based asynchronous event sink.
Package sink provides a generic, channel-based asynchronous event sink.
Package stream implements a streaming XML writer that produces well-formed XML incrementally via method calls, writing directly to an io.Writer without building an in-memory DOM tree.
Package stream implements a streaming XML writer that produces well-formed XML incrementally via method calls, writing directly to an io.Writer without building an in-memory DOM tree.
tools
internal/gen
Package gen holds helpers shared by the helium code generators (tools/qt3gen and tools/xslt3gen).
Package gen holds helpers shared by the helium code generators (tools/qt3gen and tools/xslt3gen).
qt3gen command
Command qt3gen parses the W3C QT3 (XPath/XQuery Test Suite 3) catalog and generates a table-driven Go test file for the xpath3 package.
Command qt3gen parses the W3C QT3 (XPath/XQuery Test Suite 3) catalog and generates a table-driven Go test file for the xpath3 package.
xslt3gen command
Command xslt3gen parses the W3C XSLT 3.0 test catalog and generates table-driven Go test files for the xslt3 package.
Command xslt3gen parses the W3C XSLT 3.0 test catalog and generates table-driven Go test files for the xslt3 package.
Package xinclude implements XML Inclusion (XInclude) 1.0 processing.
Package xinclude implements XML Inclusion (XInclude) 1.0 processing.
Package xmldsig1 implements W3C XML Digital Signatures 1.1.
Package xmldsig1 implements W3C XML Digital Signatures 1.1.
Package xmlenc1 implements W3C XML Encryption 1.1.
Package xmlenc1 implements W3C XML Encryption 1.1.
Package xpath1 implements XPath 1.0 expression parsing and evaluation against helium XML document trees.
Package xpath1 implements XPath 1.0 expression parsing and evaluation against helium XML document trees.
Package xpath3 implements XPath 3.1 expression parsing and evaluation against helium XML document trees.
Package xpath3 implements XPath 3.1 expression parsing and evaluation against helium XML document trees.
Package xpointer implements XPointer framework and element() scheme resolution (libxml2: xpointer module / xmlXPtrEval).
Package xpointer implements XPointer framework and element() scheme resolution (libxml2: xpointer module / xmlXPtrEval).
Package xsd implements XML Schema (XSD) 1.0 compilation and validation.
Package xsd implements XML Schema (XSD) 1.0 compilation and validation.
Package xslt3 implements an XSLT 3.0 processor targeting Basic XSLT 3.0 conformance (W3C spec Section 27).
Package xslt3 implements an XSLT 3.0 processor targeting Basic XSLT 3.0 conformance (W3C spec Section 27).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL