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 ¶
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.
const ( RefExternalSubset = iota RefExternalEntity RefStylesheet RefXInclude )
Kinds of external reference, in the order they are reported.
Variables ¶
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.
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 Reader ¶
type Reader struct {
Report Report
// contains filtered or unexported fields
}
Reader reads one document.
func New ¶
New returns a Reader of the decoded text r. ExpansionLimit in opts bounds entity expansion.
func (*Reader) Run ¶
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.
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.