Documentation
¶
Overview ¶
Package rexml is a pure-Go (no cgo) reimplementation of the core of Ruby's REXML XML library — the tree model, the parser, the serialisers (the default compact formatter and Formatters::Pretty), and a widely-used subset of XPath.
It mirrors REXML's observable behaviour rather than wrapping encoding/xml: REXML stores text in its raw, still-escaped form, defaults to single-quoted attributes, self-closes empty elements, and preserves comments / CDATA / processing instructions verbatim on a parse→serialise round trip. The Go tokenizer is used internally where convenient, but the output and value model match MRI's REXML, not Go's encoding/xml.
Index ¶
- Constants
- func AttrValue(n Node) string
- func PrettyString(d *Document, indent int) string
- func XPathEach(ctx Node, path string, fn func(Node))
- type Attribute
- type Attributes
- func (a *Attributes) Delete(qname string)
- func (a *Attributes) Each(fn func(*Attribute))
- func (a *Attributes) EachSorted(fn func(*Attribute))
- func (a *Attributes) Get(qname string) (string, bool)
- func (a *Attributes) GetAttr(qname string) *Attribute
- func (a *Attributes) Len() int
- func (a *Attributes) Set(qname, value string)
- type CData
- type Child
- type Comment
- type DocType
- type Document
- func (d *Document) Add(n Node) Node
- func (d *Document) AddElement(qname string) *Element
- func (d *Document) DocType() *DocType
- func (d *Document) Pretty(indent int) string
- func (d *Document) Root() *Element
- func (d *Document) RootNode() *Document
- func (d *Document) String() string
- func (d *Document) ToString() string
- func (d *Document) Write(opts WriteOptions) string
- func (d *Document) XMLDecl() *XMLDecl
- type Element
- func (e *Element) Add(n Node) Node
- func (e *Element) AddAttribute(qname, value string)
- func (e *Element) AddElement(qname string) *Element
- func (e *Element) AddText(s string)
- func (e *Element) Attr(qname string) (string, bool)
- func (e *Element) ChildElements() []*Element
- func (e *Element) EachElement(fn func(*Element))
- func (e *Element) ElementAt(n int) *Element
- func (e *Element) Elements(path string) []*Element
- func (e *Element) ExpandedName() string
- func (e *Element) FirstElement(path string) *Element
- func (e *Element) GetText() *Text
- func (e *Element) HasText() bool
- func (e *Element) NamespaceURI() string
- func (e *Element) Parent() *Element
- func (e *Element) QName() string
- func (e *Element) SetText(s string)
- func (e *Element) Text() string
- func (e *Element) Texts() []*Text
- type Instruction
- type Node
- type ParseError
- type PrettyFormatter
- type Text
- type WriteOptions
- type XMLDecl
- type XPath
Constants ¶
const VERSION = "3.4.4"
VERSION reports the REXML release whose observable behaviour this package targets (MRI 4.0.5 ships rexml 3.4.4).
Variables ¶
This section is empty.
Functions ¶
func AttrValue ¶
AttrValue returns the decoded value of a matched @attr node, the empty string for any other node — convenience for XPath @attr matches.
func PrettyString ¶
PrettyString returns the document rendered by the Pretty formatter with the given indent width (REXML's Formatters::Pretty.new(indent)).
Types ¶
type Attribute ¶
type Attribute struct {
Prefix string
Name string // local name
Value string
Raw bool // Value is already in normalized form (parsed); emit near-verbatim
}
Attribute is a single element attribute. Prefix is the namespace prefix (the part before ':' in the qualified name), empty for an unprefixed attribute. Value holds the normalized (still-escaped) form when Raw is set — the byte form a parser captured — and the plain unescaped form otherwise.
func (*Attribute) UnescapedValue ¶
UnescapedValue returns the attribute's decoded value — REXML's Attribute#value.
type Attributes ¶
type Attributes struct {
// contains filtered or unexported fields
}
Attributes is an ordered map of attributes keyed by qualified name, matching REXML's source order on serialisation.
func (*Attributes) Delete ¶
func (a *Attributes) Delete(qname string)
Delete removes an attribute by qualified name; it is a no-op if absent.
func (*Attributes) Each ¶
func (a *Attributes) Each(fn func(*Attribute))
Each calls fn for every attribute in source / insertion order — REXML's each_attribute, the order the Pretty formatter uses.
func (*Attributes) EachSorted ¶
func (a *Attributes) EachSorted(fn func(*Attribute))
EachSorted calls fn for every attribute ordered by local name with a stable sort — the order REXML's default (compact) formatter emits.
func (*Attributes) Get ¶
func (a *Attributes) Get(qname string) (string, bool)
Get returns the attribute's decoded value for a qualified name and whether it was set — REXML's Attributes#[] (which returns the unnormalized value).
func (*Attributes) GetAttr ¶
func (a *Attributes) GetAttr(qname string) *Attribute
GetAttr returns the *Attribute for a qualified name, or nil.
func (*Attributes) Set ¶
func (a *Attributes) Set(qname, value string)
Set inserts or replaces an attribute with an unescaped value (programmatic add_attribute), preserving first-seen order.
type CData ¶
type CData struct {
Value string
// contains filtered or unexported fields
}
CData is a CDATA section; its content is emitted verbatim between <![CDATA[ and ]]>.
type Child ¶
type Child = Node
Child is a node that can live inside an Element's child list and also at the Document's top level. Every Node type implements it.
type Comment ¶
type Comment struct {
Value string
// contains filtered or unexported fields
}
Comment is an XML comment; its content is emitted verbatim between <!-- -->.
type DocType ¶
type DocType struct {
// Body is the text between "<!DOCTYPE " and the closing ">".
Body string
// contains filtered or unexported fields
}
DocType is a document type declaration. The whole declaration is kept as its raw bytes (without the surrounding <!DOCTYPE ... >) so it round-trips.
type Document ¶
type Document struct {
Children []Node
// contains filtered or unexported fields
}
Document is the root of a REXML tree. Its Children hold the top-level nodes: an optional XMLDecl, DocType, comments / PIs, and exactly one root element.
func Parse ¶
Parse is an alias for ParseDocument, matching the common REXML idiom REXML::Document.new(xml).
func ParseDocument ¶
ParseDocument parses an XML string into a Document tree, mirroring REXML::Document.new. Text, comments, CDATA, PIs, the XML declaration and the DOCTYPE are preserved so a parse→serialise round trip matches MRI's REXML.
func (*Document) Add ¶
Add appends a top-level node. Element children get the document recorded as a nil parent (REXML root elements have no parent element).
func (*Document) AddElement ¶
AddElement creates the root element, appends it, and returns it.
func (*Document) Pretty ¶
Pretty renders the document with the Pretty formatter at the given indent — a convenience for REXML::Formatters::Pretty.new(indent).write(doc, out).
func (*Document) RootNode ¶
RootNode returns the document itself — REXML's root_node returns the containing Document for a Document.
func (*Document) ToString ¶
ToString returns the document's default (compact) serialisation — REXML's Document#to_s.
func (*Document) Write ¶
func (d *Document) Write(opts WriteOptions) string
Write serialises the document with the chosen formatter and returns the text.
type Element ¶
type Element struct {
Prefix string
Name string
Attributes *Attributes
Children []Node
// contains filtered or unexported fields
}
Element is an XML element node. Name is the local name; Prefix is the namespace prefix (empty when unprefixed). Attributes preserves source order; Children holds the ordered child nodes.
func NewElement ¶
NewElement builds a detached element from a qualified name (prefix:name).
func (*Element) AddAttribute ¶
AddAttribute sets an attribute by qualified name (REXML's add_attribute).
func (*Element) AddElement ¶
AddElement creates a child element with the given qualified name, appends it, and returns it (REXML's add_element).
func (*Element) AddText ¶
AddText appends an unescaped text node (REXML's add_text). Consecutive add_text calls in REXML coalesce; this mirrors that for raw==raw text nodes.
func (*Element) ChildElements ¶
ChildElements returns the direct child elements in order.
func (*Element) EachElement ¶
EachElement calls fn for every direct child element (REXML's each_element).
func (*Element) ElementAt ¶
ElementAt returns the element at the 1-based child-element index, or nil — REXML's elements[n] with an Integer.
func (*Element) Elements ¶
Elements resolves an XPath-style path against this element and returns the matching elements — REXML's Element#elements[path] / each. A bare positive integer selects the 1-based nth child element.
func (*Element) ExpandedName ¶
ExpandedName is REXML's expanded_name: prefix:name when prefixed, else name.
func (*Element) FirstElement ¶
FirstElement returns the first element matching the path, or nil.
func (*Element) NamespaceURI ¶
NamespaceURI resolves the element's own prefix to a namespace URI by walking up the xmlns declarations, mirroring REXML's Element#namespace.
func (*Element) SetText ¶
SetText replaces the element's first text node (or inserts one) with s, matching REXML's Element#text=.
type Instruction ¶
Instruction is a processing instruction (PI): <?target content?>.
type Node ¶
type Node interface {
// contains filtered or unexported methods
}
Node is any node in a REXML tree: Element, Text, Comment, CData, Instruction, DocType, XMLDecl, or the Document itself.
func XPathFirst ¶
XPathFirst returns the first node matching path relative to ctx, or nil — REXML::XPath.first.
func XPathMatch ¶
XPathMatch returns every node matching path — REXML::XPath.match.
type ParseError ¶
ParseError reports a parse failure with a byte offset, mirroring the fact that REXML::Document.new raises ParseException on malformed input.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type PrettyFormatter ¶
PrettyFormatter is REXML::Formatters::Pretty. It re-indents the tree, drops whitespace-only text nodes between elements, collapses runs of whitespace in text, and (when Compact) keeps short text-only elements on one line.
type Text ¶
type Text struct {
// Value is the raw stored string (escaped form when Raw, plain otherwise).
Value string
// Raw reports that Value already holds the serialised (escaped) bytes, so
// writeTo emits it verbatim. Parsed text sets this; NewText leaves it false.
Raw bool
// contains filtered or unexported fields
}
Text is character data inside an element. REXML keeps the text in its raw, still-escaped form: a parsed "A&" round-trips byte-for-byte, while text added programmatically (already-decoded) is escaped on output. The Raw flag distinguishes the two — parsed text is Raw, NewText escapes on write.
func NewText ¶
NewText builds a text node from an unescaped string (REXML's add_text). The string is escaped on serialisation.
type WriteOptions ¶
type WriteOptions struct {
// Indent selects the Pretty formatter with this indent width when > 0.
// REXML uses -1 to mean "no indentation" (the compact default).
Indent int
// Pretty forces the Pretty formatter even when Indent is 0 (REXML default
// indent of 2). Ignored when Indent > 0.
Pretty bool
}
WriteOptions configures Document.Write. The zero value selects the default compact formatter (REXML's Document#write with no indent).
type XMLDecl ¶
type XMLDecl struct {
Version string
Encoding string
Standalone string
// contains filtered or unexported fields
}
XMLDecl is the XML declaration: <?xml version='1.0' ...?>. REXML emits the version, and the encoding / standalone only when present.
type XPath ¶
type XPath struct{}
XPath implements the widely-used subset of XPath 1.0 that REXML's XPath.first / each / match cover. The supported grammar is documented on Match.
Boundary (out of scope): axes other than child/descendant (no ancestor, following-sibling, …), functions beyond text() (no count(), position(), last(), name(), contains(), …), arithmetic, and nested boolean predicates (and/or). Predicates support [n] (1-based position), [@attr], [@attr='value'] / [@attr="value"], and the boolean attribute existence test.
