xmldoc

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package xmldoc reads XML documents into trees that keep them as written, and navigates them for XPath.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ElementXML

func ElementXML(e *Node) string

ElementXML returns the XML of an element, with the namespace declarations its ancestors make that it doesn't make itself, the innermost of each prefix, so its names mean in it what they mean in the document.

func TrimSpace

func TrimSpace(s string) string

TrimSpace trims the white space of XML off text.

Types

type Attr

type Attr struct {
	Name  Name
	Value string
	Decl  bool // whether it declares a namespace, unless namespaces are ignored
}

Attr is an attribute of an element.

type Kind

type Kind int

Kind is the kind of a node of an XML document.

const (
	Document Kind = iota + 1
	Element
	Text
	Comment
	ProcInst
)

The kinds of nodes.

type Name

type Name struct {
	Space  string
	Prefix string
	Local  string
}

Name is the name of an element or an attribute: its namespace URI, its prefix and its local name. When namespaces are ignored, Local is the name as written, with its prefix, and the others are empty.

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

Nav navigates the tree of an XML document for XPath, as XPath 1.0 sees it: the document, its elements, their attributes, without namespace declarations, its text, white space included, and its comments. The xpath module has no node type for processing instructions, so it doesn't see them.

func NewNav

func NewNav(doc *Node) *Nav

NewNav returns a navigator on the document node of a tree.

func (x *Nav) Copy() xpath.NodeNavigator

Copy implements xpath.NodeNavigator.

func (x *Nav) LocalName() string

LocalName implements xpath.NodeNavigator.

func (x *Nav) MoveTo(other xpath.NodeNavigator) bool

MoveTo implements xpath.NodeNavigator: to where other is.

func (x *Nav) MoveToChild() bool

MoveToChild implements xpath.NodeNavigator.

func (x *Nav) MoveToFirst() bool

MoveToFirst implements xpath.NodeNavigator: to the first sibling.

func (x *Nav) MoveToNext() bool

MoveToNext implements xpath.NodeNavigator: to the next sibling.

func (x *Nav) MoveToNextAttribute() bool

MoveToNextAttribute implements xpath.NodeNavigator.

func (x *Nav) MoveToParent() bool

MoveToParent implements xpath.NodeNavigator: an attribute's parent is its element.

func (x *Nav) MoveToPrevious() bool

MoveToPrevious implements xpath.NodeNavigator: to the previous sibling.

func (x *Nav) MoveToRoot()

MoveToRoot implements xpath.NodeNavigator.

func (x *Nav) NamespaceURL() string

NamespaceURL returns the namespace URI of the name, which a prefix that XPathNamespace binds selects by.

func (x *Nav) Node() *Node

Node returns the node the navigator is on, or the element of the attribute it is on.

func (x *Nav) NodeType() xpath.NodeType

NodeType implements xpath.NodeNavigator.

func (x *Nav) Prefix() string

Prefix implements xpath.NodeNavigator: the prefix as written.

func (x *Nav) Value() string

Value implements xpath.NodeNavigator: the string-value of XPath 1.0. An element's and the document's is the text they hold, at any depth.

type Node

type Node struct {
	Kind     Kind
	Name     Name // an element's; a processing instruction's target, as local
	Attrs    []Attr
	Children []*Node
	Parent   *Node
	Index    int    // the node's place among its parent's children
	Text     string // the data of text, a comment or a processing instruction
}

Node is a node of an XML document, as written: its text keeps its white space, its names their prefixes.

func Parse

func Parse(data []byte, ignoreNamespaces bool) (*Node, error)

Parse reads an XML document into its tree, whose root is the document node. Unless ignoreNamespaces is set, each prefix must be bound by a declaration.

func (*Node) Root

func (n *Node) Root() *Node

Root returns the root element of a document.

Jump to

Keyboard shortcuts

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