xmlread

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package xmlread turns the scanner's tokens into element events for a consumer that builds blocks, and keeps what every consumer needs: the stack of open elements with their names, namespaces, attributes, xml:space and offsets; the document type declaration and its entities; and the counts of what the file holds that is reported rather than read (external references, processing instructions, repairs, cuts).

The generic reader is one consumer (package generic); a profile-driven one implements the same Handler and hands the elements it has no rule for to the generic builder.

Index

Constants

View Source
const (
	NamespaceXSI      = "http://www.w3.org/2001/XMLSchema-instance"
	NamespaceXInclude = "http://www.w3.org/2001/XInclude"
	// NamespaceXInclude2003 is the namespace of the 2003 draft that some
	// files still use.
	NamespaceXInclude2003 = "http://www.w3.org/2003/XInclude"
)

Namespaces the reader recognises.

View Source
const (
	RefExternalSubset = iota
	RefExternalEntity
	RefStylesheet
	RefXInclude
)

Kinds of external reference, in the order they are reported.

Variables

View Source
var RefKindNames = [refKinds]string{
	"external DTD subset", "external entity reference", "xml-stylesheet instruction", "XInclude element",
}

RefKindNames names the kinds for messages.

Functions

This section is empty.

Types

type Attr

type Attr struct {
	Name  string // qualified, as written
	URI   string // namespace; empty for none
	Local string
	Value string // decoded and normalized as XML requires
}

Attr is an attribute of an element, as strings that stay valid.

type Element

type Element struct {
	Name  string // qualified, as written, prefix included
	Local string
	URI   string // namespace; empty for none
	// Attrs are the element's attributes, valid during the handler's
	// StartElement only: they are released when it returns, so that
	// open elements hold little whatever the file. Names are cut to
	// xmlscan.MaxNameKept bytes, values to xmlscan's MaxValueBytes.
	Attrs []Attr
	// Depth is 1 for the root.
	Depth int
	// Preserve reports that xml:space="preserve" is in effect.
	Preserve bool
	// Start and End are the offsets of the start tag in the decoded text.
	Start, End int64
	// Data is for the consumer: anything it keeps per element.
	Data any
}

Element is an open element.

func (*Element) Attr

func (e *Element) Attr(uri, local string) (string, bool)

Attr returns the value of the attribute with the given namespace and local name.

type Handler

type Handler interface {
	// StartElement is called when e opens; e is the top of the stack.
	StartElement(r *Reader, e *Element)
	// EndElement is called when e closes, before it leaves the stack;
	// start and end are the offsets of its end tag (equal when it has
	// none).
	EndElement(r *Reader, e *Element, start, end int64)
	// Text is character data, CDATA and entity replacements, in pieces.
	Text(r *Reader, text []byte, start, end int64)
	// Comment is a piece of a comment; final marks its last piece.
	Comment(r *Reader, text []byte, final bool, start, end int64)
	// Stop reports that the consumer wants no more events.
	Stop() bool
}

Handler consumes the events of one reading.

type Options

type Options struct {
	Scan xmlscan.Options
}

Options configures a reading.

type Reader

type Reader struct {
	Report Report
	// contains filtered or unexported fields
}

Reader reads one document.

func New

func New(ctx context.Context, r io.Reader, opts Options) *Reader

New returns a Reader of the decoded text r. ExpansionLimit in opts bounds entity expansion.

func NewBytes

func NewBytes(ctx context.Context, data []byte, opts Options) *Reader

NewBytes is New for text in memory.

func (*Reader) Offset

func (r *Reader) Offset() int64

Offset returns the offset in the decoded text the reading has reached.

func (*Reader) Run

func (r *Reader) Run(h Handler) error

Run reads the document to its end, or until the handler stops or the context is done, and returns the scanner's error or the context's.

func (*Reader) Scanner

func (r *Reader) Scanner() *xmlscan.Scanner

Scanner returns the scanner, for its Stats and Err.

func (*Reader) Stack

func (r *Reader) Stack() []*Element

Stack returns the open elements, the root first.

func (*Reader) Top

func (r *Reader) Top() *Element

Top returns the innermost open element, or nil.

type Report

type Report struct {
	// External counts references a conforming processor would load, by
	// kind (Ref*). Nothing is loaded.
	External [refKinds]int
	// PIs counts processing instructions other than the XML declaration
	// and xml-stylesheet.
	PIs int
	// Assumed counts references to undeclared entities read as the
	// character HTML 4 gives that name, in a file with an
	// external DTD subset; Undeclared those kept as written.
	Assumed, Undeclared int
	// Repairs counts places where damaged markup was read leniently. It
	// leaves out what CharRefsReplaced counts.
	Repairs int
	// CharRefsReplaced counts character references to no character XML
	// allows (�, a surrogate, a number past U+10FFFF), each read as
	// U+FFFD: those of the document, and those in the values of the entity
	// declarations read, whether or not the document uses the entity.
	CharRefsReplaced int
	// Elements counts elements read.
	Elements int
	// Root is the root element, Doctype the declaration, if any.
	RootName, RootURI string
	RootLang          string
	SchemaLocation    string
	NoNSSchemaLoc     string
	Doctype           *dtd.Doctype
}

Report counts what the reading found besides the events.

Jump to

Keyboard shortcuts

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