Documentation ¶
Index ¶
- Constants
- Variables
- type AttributeNode
- type CDataNode
- type CommentNode
- type Document
- type DocumentFragment
- type ElementNode
- type NamespaceDeclaration
- type Node
- type NodeType
- type Nodeset
- type ParseOption
- type ProcessingInstructionNode
- type SerializationOption
- type TextNode
- type WriteBuffer
- type XmlDocument
- func CreateEmptyDocument(inEncoding, outEncoding []byte) (doc *XmlDocument)
- func NewDocument(p unsafe.Pointer, contentLen int, inEncoding, outEncoding []byte) (doc *XmlDocument)
- func Parse(content, inEncoding, url []byte, options ParseOption, outEncoding []byte) (doc *XmlDocument, err error)
- func ReadFile(filename string, options ParseOption) (doc *XmlDocument, err error)
- func (document *XmlDocument) AddUnlinkedNode(nodePtr unsafe.Pointer)
- func (document *XmlDocument) BookkeepFragment(fragment *DocumentFragment)
- func (document *XmlDocument) CreateCDataNode(data string) (cdata *CDataNode)
- func (document *XmlDocument) CreateCommentNode(data string) (comment *CommentNode)
- func (document *XmlDocument) CreateElementNode(tag string) (element *ElementNode)
- func (document *XmlDocument) CreatePINode(name, data string) (pi *ProcessingInstructionNode)
- func (document *XmlDocument) CreateTextNode(data string) (text *TextNode)
- func (document *XmlDocument) DocPtr() (ptr unsafe.Pointer)
- func (document *XmlDocument) DocRef() (d Document)
- func (document *XmlDocument) DocType() (t NodeType)
- func (document *XmlDocument) DocXPathCtx() (ctx *xpath.XPath)
- func (document *XmlDocument) Free()
- func (document *XmlDocument) InputEncoding() (encoding []byte)
- func (document *XmlDocument) NodeById(id string) (element *ElementNode)
- func (document *XmlDocument) OutputEncoding() (encoding []byte)
- func (document *XmlDocument) ParseFragment(input, url []byte, options ParseOption) (fragment *DocumentFragment, err error)
- func (document *XmlDocument) RemoveUnlinkedNode(nodePtr unsafe.Pointer) bool
- func (document *XmlDocument) Root() (element *ElementNode)
- func (document *XmlDocument) UnparsedEntityURI(name string) (val string)
- func (document *XmlDocument) Uri() (val string)
- type XmlNode
- func (xmlNode *XmlNode) AddChild(data interface{}) (err error)
- func (xmlNode *XmlNode) AddNextSibling(data interface{}) (err error)
- func (xmlNode *XmlNode) AddPreviousSibling(data interface{}) (err error)
- func (xmlNode *XmlNode) Attr(name string) (val string)
- func (xmlNode *XmlNode) Attribute(name string) (attribute *AttributeNode)
- func (xmlNode *XmlNode) AttributeList() (attributes []*AttributeNode)
- func (xmlNode *XmlNode) Attributes() (attributes map[string]*AttributeNode)
- func (xmlNode *XmlNode) Coerce(data interface{}) (nodes []Node, err error)
- func (xmlNode *XmlNode) Content() string
- func (xmlNode *XmlNode) CountChildren() int
- func (xmlNode *XmlNode) DeclareNamespace(prefix, href string)
- func (xmlNode *XmlNode) DeclaredNamespaces() (result []NamespaceDeclaration)
- func (xmlNode *XmlNode) Duplicate(level int) Node
- func (xmlNode *XmlNode) DuplicateTo(doc Document, level int) (dup Node)
- func (xmlNode *XmlNode) EvalXPath(data interface{}, v xpath.VariableScope) (result interface{}, err error)
- func (xmlNode *XmlNode) EvalXPathAsBoolean(data interface{}, v xpath.VariableScope) (result bool)
- func (xmlNode *XmlNode) FirstChild() Node
- func (xmlNode *XmlNode) InnerHtml() string
- func (xmlNode *XmlNode) InsertAfter(data interface{}) (err error)
- func (xmlNode *XmlNode) InsertBefore(data interface{}) (err error)
- func (xmlNode *XmlNode) InsertBegin(data interface{}) (err error)
- func (xmlNode *XmlNode) InsertEnd(data interface{}) (err error)
- func (xmlNode *XmlNode) IsValid() bool
- func (xmlNode *XmlNode) LastChild() Node
- func (xmlNode *XmlNode) LineNumber() int
- func (xmlNode *XmlNode) MyDocument() (document Document)
- func (xmlNode *XmlNode) Name() (name string)
- func (xmlNode *XmlNode) Namespace() (href string)
- func (xmlNode *XmlNode) NextSibling() Node
- func (xmlNode *XmlNode) NodePtr() (p unsafe.Pointer)
- func (xmlNode *XmlNode) NodeType() (nodeType NodeType)
- func (xmlNode *XmlNode) Parent() Node
- func (xmlNode *XmlNode) ParseFragment(input, url []byte, options ParseOption) (fragment *DocumentFragment, err error)
- func (xmlNode *XmlNode) Path() (path string)
- func (xmlNode *XmlNode) PreviousSibling() Node
- func (xmlNode *XmlNode) RecursivelyRemoveNamespaces() (err error)
- func (xmlNode *XmlNode) Remove()
- func (xmlNode *XmlNode) RemoveDefaultNamespace()
- func (xmlNode *XmlNode) Replace(data interface{}) (err error)
- func (xmlNode *XmlNode) ResetChildren()
- func (xmlNode *XmlNode) ResetNodePtr()
- func (xmlNode *XmlNode) Search(data interface{}) (result []Node, err error)
- func (xmlNode *XmlNode) SearchWithVariables(data interface{}, v xpath.VariableScope) (result []Node, err error)
- func (xmlNode *XmlNode) SerializeWithFormat(format SerializationOption, encoding, outputBuffer []byte) ([]byte, int)
- func (xmlNode *XmlNode) SetAttr(name, value string) (val string)
- func (xmlNode *XmlNode) SetChildren(data interface{}) (err error)
- func (xmlNode *XmlNode) SetContent(content interface{}) (err error)
- func (xmlNode *XmlNode) SetInnerHtml(data interface{}) (err error)
- func (xmlNode *XmlNode) SetName(name string)
- func (xmlNode *XmlNode) SetNamespace(prefix, href string)
- func (xmlNode *XmlNode) SetNsAttr(href, name, value string) (val string)
- func (xmlNode *XmlNode) String() string
- func (xmlNode *XmlNode) ToBuffer(outputBuffer []byte) []byte
- func (xmlNode *XmlNode) ToHtml(encoding, outputBuffer []byte) ([]byte, int)
- func (xmlNode *XmlNode) ToUnformattedXml() string
- func (xmlNode *XmlNode) ToXml(encoding, outputBuffer []byte) ([]byte, int)
- func (xmlNode *XmlNode) Unlink()
- func (xmlNode *XmlNode) Wrap(data string) (err error)
Constants ¶
const DefaultEncoding = "utf-8"
DefaultEncoding is UTF-8, which is also the default for both libxml2 and Go strings.
Variables ¶
var ( ERR_UNDEFINED_COERCE_PARAM = errors.New("unexpected parameter type in coerce") ERR_UNDEFINED_SET_CONTENT_PARAM = errors.New("unexpected parameter type in SetContent") ERR_UNDEFINED_SEARCH_PARAM = errors.New("unexpected parameter type in Search") ERR_CANNOT_MAKE_DUCMENT_AS_CHILD = errors.New("cannot add a document node as a child") ERR_CANNOT_COPY_TEXT_NODE_WHEN_ADD_CHILD = errors.New("cannot copy a text node when adding it") )
var DefaultEncodingBytes = []byte(DefaultEncoding)
DefaultEncodingBytes allows us to conveniently pass the DefaultEncoding to various functions that expect the encoding as a byte array.
var ERR_FAILED_TO_PARSE_XML = errors.New("failed to parse xml input")
var ErrEmptyFragment = errors.New("empty xml fragment")
var ErrFailParseFragment = errors.New("failed to parse xml fragment")
var ErrTooLarge = errors.New("Output buffer too large")
run out of memory
Functions ¶
This section is empty.
Types ¶
type AttributeNode ¶
type AttributeNode struct {
*XmlNode
}
AttributeNode represents an attribute, which has a name and a value.
AttributeNodes are created by calling SetAttr or SetNsAttr on an element node, and retrieved by the Attribute and Attributes functions on an element node.
Note that while mamespace declarations resemble attributes, they are a distinct node type and cannot be used or retrieved as an AttributeNode.
func (*AttributeNode) SetValue ¶
func (attrNode *AttributeNode) SetValue(val interface{})
SetValue sets the value of the attribute. Note that the argument will be converted to a string, and automatically XML-escaped when the document is serialized.
func (*AttributeNode) String ¶
func (attrNode *AttributeNode) String() string
String returns the value of the attribute.
func (*AttributeNode) Value ¶
func (attrNode *AttributeNode) Value() string
Value returns the value of the attribute.
type CDataNode ¶
type CDataNode struct {
*XmlNode
}
CDataNode represents a CDATA section. This XML node type allows the embedding of unescaped, verbatim text within an XML document.
It is otherwise identical to a TextNode. It is most often used to wrap content that is whitespace-sensitive or likely to contain large numbers of less-than or greater-than signs (such as code snippets or example documents).
If you use the XML_PARSE_NOCDATA parsing option, the parser will always present the CDATA sections as TextNodes.
type CommentNode ¶
type CommentNode struct {
*XmlNode
}
type Document ¶
type Document interface { /* Nokogiri APIs */ CreateElementNode(string) *ElementNode CreateCDataNode(string) *CDataNode CreateTextNode(string) *TextNode CreateCommentNode(string) *CommentNode CreatePINode(string, string) *ProcessingInstructionNode ParseFragment([]byte, []byte, ParseOption) (*DocumentFragment, error) DocPtr() unsafe.Pointer DocType() NodeType DocRef() Document InputEncoding() []byte OutputEncoding() []byte DocXPathCtx() *xpath.XPath AddUnlinkedNode(unsafe.Pointer) RemoveUnlinkedNode(unsafe.Pointer) bool Free() String() string Root() *ElementNode NodeById(string) *ElementNode BookkeepFragment(*DocumentFragment) RecursivelyRemoveNamespaces() error UnparsedEntityURI(string) string Uri() string }
type DocumentFragment ¶
func ParseFragment ¶
func ParseFragment(content, inEncoding, url []byte, options ParseOption, outEncoding []byte) (fragment *DocumentFragment, err error)
func (*DocumentFragment) Children ¶
func (fragment *DocumentFragment) Children() []Node
func (*DocumentFragment) Remove ¶
func (fragment *DocumentFragment) Remove()
func (*DocumentFragment) String ¶
func (fragment *DocumentFragment) String() string
func (*DocumentFragment) ToBuffer ¶
func (fragment *DocumentFragment) ToBuffer(outputBuffer []byte) []byte
type ElementNode ¶
type ElementNode struct {
*XmlNode
}
type NamespaceDeclaration ¶
NamespaceDeclaration represents a namespace declaration, providing both the prefix and the URI of the namespace. It is returned by the DeclaredNamespaces function.
type Node ¶
type Node interface { NodePtr() unsafe.Pointer ResetNodePtr() MyDocument() Document IsValid() bool ParseFragment([]byte, []byte, ParseOption) (*DocumentFragment, error) LineNumber() int // NodeType() NodeType NextSibling() Node PreviousSibling() Node Parent() Node FirstChild() Node LastChild() Node CountChildren() int Attributes() map[string]*AttributeNode AttributeList() []*AttributeNode Coerce(interface{}) ([]Node, error) AddChild(interface{}) error AddPreviousSibling(interface{}) error AddNextSibling(interface{}) error InsertBefore(interface{}) error InsertAfter(interface{}) error InsertBegin(interface{}) error InsertEnd(interface{}) error SetInnerHtml(interface{}) error SetChildren(interface{}) error Replace(interface{}) error Wrap(string) error SetContent(interface{}) error Name() string SetName(string) Attr(string) string SetAttr(string, string) string SetNsAttr(string, string, string) string Attribute(string) *AttributeNode Path() string Duplicate(int) Node DuplicateTo(Document, int) Node Search(interface{}) ([]Node, error) SearchWithVariables(interface{}, xpath.VariableScope) ([]Node, error) EvalXPath(interface{}, xpath.VariableScope) (interface{}, error) EvalXPathAsBoolean(interface{}, xpath.VariableScope) bool Unlink() Remove() ResetChildren() SerializeWithFormat(SerializationOption, []byte, []byte) ([]byte, int) ToXml([]byte, []byte) ([]byte, int) ToUnformattedXml() string ToHtml([]byte, []byte) ([]byte, int) ToBuffer([]byte) []byte String() string Content() string InnerHtml() string RecursivelyRemoveNamespaces() error Namespace() string SetNamespace(string, string) DeclareNamespace(string, string) RemoveDefaultNamespace() DeclaredNamespaces() []NamespaceDeclaration }
type NodeType ¶
type NodeType int
NodeType is an enumeration that indicates the type of XmlNode.
const ( XML_ELEMENT_NODE NodeType = iota + 1 XML_ATTRIBUTE_NODE XML_TEXT_NODE XML_CDATA_SECTION_NODE XML_ENTITY_REF_NODE XML_ENTITY_NODE XML_PI_NODE XML_COMMENT_NODE XML_DOCUMENT_NODE XML_DOCUMENT_TYPE_NODE XML_DOCUMENT_FRAG_NODE XML_NOTATION_NODE XML_HTML_DOCUMENT_NODE XML_DTD_NODE XML_ELEMENT_DECL XML_ATTRIBUTE_DECL XML_ENTITY_DECL XML_NAMESPACE_DECL XML_XINCLUDE_START XML_XINCLUDE_END XML_DOCB_DOCUMENT_NODE )
type Nodeset ¶
type Nodeset []Node
func (Nodeset) ToPointers ¶
Produce a slice of unsafe.Pointer objects, suitable for passing to a C function
func (Nodeset) ToXPathNodeset ¶
func (n Nodeset) ToXPathNodeset() (ret C.xmlXPathObjectPtr)
Produce a C.xmlXPathObjectPtr suitable for passing to libxml2
func (Nodeset) ToXPathValueTree ¶
func (n Nodeset) ToXPathValueTree() (ret C.xmlXPathObjectPtr)
Produce a C.xmlXPathObjectPtr marked as a ResultValueTree, suitable for passing to libxml2
type ParseOption ¶
type ParseOption int
ParseOption values allow you to tune the behaviour of the parsing engine.
const ( XML_PARSE_RECOVER ParseOption = 1 << iota // recover on errors XML_PARSE_NOENT // substitute entities XML_PARSE_DTDLOAD // load the external subset XML_PARSE_DTDATTR // default DTD attributes XML_PARSE_DTDVALID // validate with the DTD XML_PARSE_NOERROR // suppress error reports XML_PARSE_NOWARNING // suppress warning reports XML_PARSE_PEDANTIC // pedantic error reporting XML_PARSE_NOBLANKS // remove blank nodes XML_PARSE_SAX1 // use the SAX1 interface internally XML_PARSE_XINCLUDE // Implement XInclude substitition XML_PARSE_NONET // Forbid network access XML_PARSE_NODICT // Do not reuse the context dictionnary XML_PARSE_NSCLEAN // remove redundant namespaces declarations XML_PARSE_NOCDATA // merge CDATA as text nodes XML_PARSE_NOXINCNODE // do not generate XINCLUDE START/END nodes XML_PARSE_COMPACT // compact small text nodes; makes tree read-only XML_PARSE_OLD10 // parse using XML-1.0 before update 5 XML_PARSE_NOBASEFIX // do not fixup XINCLUDE xml//base uris XML_PARSE_HUGE // relax any hardcoded limit from the parser XML_PARSE_OLDSAX // parse using SAX2 interface before 2.7.0 XML_PARSE_IGNORE_ENC // ignore internal document encoding hint XML_PARSE_BIG_LINES // Store big lines numbers in text PSVI field )
const DefaultParseOption ParseOption = XML_PARSE_RECOVER | XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING
DefaultParseOption provides liberal parsing highly tolerant of invalid documents. Errors and warnings are suppressed and the DTD is not processed.
const StrictParseOption ParseOption = XML_PARSE_NOENT | XML_PARSE_DTDLOAD | XML_PARSE_DTDATTR | XML_PARSE_NOCDATA
StrictParseOption provides standard-compliant parsing. The DTD is processed, entity substitions are made, and errors and warnings are reported back.
type ProcessingInstructionNode ¶
type ProcessingInstructionNode struct {
*XmlNode
}
type SerializationOption ¶
type SerializationOption int
SerializationOption is a set of flags used to control how a node is written out.
const ( XML_SAVE_FORMAT SerializationOption = 1 << iota // format save output XML_SAVE_NO_DECL //drop the xml declaration XML_SAVE_NO_EMPTY //no empty tags XML_SAVE_NO_XHTML //disable XHTML1 specific rules XML_SAVE_XHTML //force XHTML1 specific rules XML_SAVE_AS_XML //force XML serialization on HTML doc XML_SAVE_AS_HTML //force HTML serialization on XML doc XML_SAVE_WSNONSIG //format with non-significant whitespace )
type TextNode ¶
type TextNode struct {
*XmlNode
}
func (*TextNode) DisableOutputEscaping ¶
func (node *TextNode) DisableOutputEscaping()
DisableOutputEscaping disables the usual safeguards against creating invalid XML and allows the characters '<', '>', and '&' to be written out verbatim. Normally they are safely escaped as entities.
This API is intended to provide support for XSLT processors and similar XML manipulation libraries that may need to output unsupported entity references or use the XML API for non-XML output. It should never be used in the normal course of XML processing.
type WriteBuffer ¶
type XmlDocument ¶
type XmlDocument struct { Ptr *C.xmlDoc Me Document Node InEncoding []byte OutEncoding []byte UnlinkedNodes map[*C.xmlNode]bool XPathCtx *xpath.XPath Type NodeType InputLen int // contains filtered or unexported fields }
XmlDocument is the primary interface for working with XML documents.
func CreateEmptyDocument ¶
func CreateEmptyDocument(inEncoding, outEncoding []byte) (doc *XmlDocument)
Create an empty XML document and return an XmlDocument. The root element, along with any top-level comments or processing instructions, can be added by calling AddChild() on the document itself.
func NewDocument ¶
func NewDocument(p unsafe.Pointer, contentLen int, inEncoding, outEncoding []byte) (doc *XmlDocument)
NewDocument wraps the pointer to the C struct.
TODO: this should probably not be exported.
func Parse ¶
func Parse(content, inEncoding, url []byte, options ParseOption, outEncoding []byte) (doc *XmlDocument, err error)
Parse creates an XmlDocument from some pre-existing content where the input encoding is known. Byte arrays created from a Go string are utf-8 encoded (you can pass DefaultEncodingBytes in this scenario).
If you want to build up a document programmatically, calling CreateEmptyDocument and building it up using the xml.Node interface is a better approach than building a string and calling Parse.
If you have an XML file, then ReadFile will automatically determine the encoding according to the XML specification.
func ReadFile ¶
func ReadFile(filename string, options ParseOption) (doc *XmlDocument, err error)
ReadFile loads an XmlDocument from a filename. The encoding declared in the document will be used as the input encoding. If no encoding is declared, the library will use the alogrithm in the XML standard to determine if the document is encoded with UTF-8 or UTF-16.
func (*XmlDocument) AddUnlinkedNode ¶
func (document *XmlDocument) AddUnlinkedNode(nodePtr unsafe.Pointer)
func (*XmlDocument) BookkeepFragment ¶
func (document *XmlDocument) BookkeepFragment(fragment *DocumentFragment)
func (*XmlDocument) CreateCDataNode ¶
func (document *XmlDocument) CreateCDataNode(data string) (cdata *CDataNode)
CreateCDataNode creates a CDATA node. CDATA nodes can only be children of an element.
The data argument will become the content of the newly created node.
func (*XmlDocument) CreateCommentNode ¶
func (document *XmlDocument) CreateCommentNode(data string) (comment *CommentNode)
CreateCommentNode creates a comment node. Comment nodes can be children of an element or of the document itself.
The data argument will become the content of the comment.
func (*XmlDocument) CreateElementNode ¶
func (document *XmlDocument) CreateElementNode(tag string) (element *ElementNode)
CreateElementNode creates an element node with the specified tag name. It can be added as a child of any other element, or as a child of the document itself.
Use SetNamespace if the element node needs to be in a namespace.
Note that valid documents have only one child element, referred to as the root node.
func (*XmlDocument) CreatePINode ¶
func (document *XmlDocument) CreatePINode(name, data string) (pi *ProcessingInstructionNode)
CreatePINode creates a processing instruction node with the specified name and data. Processing instruction nodes can be children of an element or of the document itself.
While it's common to use an attribute-like syntax for processing instructions, the data is actually an arbitrary string that you will need to generate or parse yourself.
func (*XmlDocument) CreateTextNode ¶
func (document *XmlDocument) CreateTextNode(data string) (text *TextNode)
CreateTextNode creates a text node. It can be added as a child of an element.
The data argument is XML-escaped and used as the content of the node.
func (*XmlDocument) DocPtr ¶
func (document *XmlDocument) DocPtr() (ptr unsafe.Pointer)
DocPtr provides access to the libxml2 structure underlying the document.
func (*XmlDocument) DocRef ¶
func (document *XmlDocument) DocRef() (d Document)
DocRef returns the embedded Document interface.
func (*XmlDocument) DocType ¶
func (document *XmlDocument) DocType() (t NodeType)
DocType returns one of the node type constants, usually XML_DOCUMENT_NODE. This may be of use if you are working with the C API.
func (*XmlDocument) DocXPathCtx ¶
func (document *XmlDocument) DocXPathCtx() (ctx *xpath.XPath)
Returns an XPath context that can be used to compile and evaluate XPath expressions.
In most cases, you should call the Search or EvalXPath functions instead of handling the context directly.
func (*XmlDocument) Free ¶
func (document *XmlDocument) Free()
Free the C structures associated with this document.
func (*XmlDocument) InputEncoding ¶
func (document *XmlDocument) InputEncoding() (encoding []byte)
InputEncoding is the original encoding of the document.
func (*XmlDocument) NodeById ¶
func (document *XmlDocument) NodeById(id string) (element *ElementNode)
Get an element node by the value of its ID attribute. By convention this attribute is named id, but the actual name of the attribute is set by the document's DTD or schema.
The value for an ID attribute is guaranteed to be unique within a valid document.
func (*XmlDocument) OutputEncoding ¶
func (document *XmlDocument) OutputEncoding() (encoding []byte)
OutputEncoding is the encoding that will be used when the document is written out. This can be overridden by explicitly specifying an encoding as an argument to any of the output functions.
func (*XmlDocument) ParseFragment ¶
func (document *XmlDocument) ParseFragment(input, url []byte, options ParseOption) (fragment *DocumentFragment, err error)
func (*XmlDocument) RemoveUnlinkedNode ¶
func (document *XmlDocument) RemoveUnlinkedNode(nodePtr unsafe.Pointer) bool
func (*XmlDocument) Root ¶
func (document *XmlDocument) Root() (element *ElementNode)
Root returns the root node of the document. Newly created documents do not have a root node until an element node is added a child of the document.
Documents that have multiple root nodes are invalid and the behaviour is not well defined.
func (*XmlDocument) UnparsedEntityURI ¶
func (document *XmlDocument) UnparsedEntityURI(name string) (val string)
Return the value of an NDATA entity declared in the DTD. If there is no such entity or the value cannot be encoded as a valid URI, an empty string is returned.
Note that this library assumes you already know the name of entity and does not expose any way of getting the list of entities.
func (*XmlDocument) Uri ¶
func (document *XmlDocument) Uri() (val string)
Uri returns the URI of the document - typically this is the filename if ReadFile was used to parse
the document.
type XmlNode ¶
XmlNode implements the Node interface, and as such is the heart of the API.
func (*XmlNode) AddChild ¶
Add a node as a child of the current node. Passing in a nodeset will add all the nodes as children of the current node.
func (*XmlNode) AddNextSibling ¶
Insert a node immediately after this node in the document. Passing in a nodeset will add all the nodes, in order.
func (*XmlNode) AddPreviousSibling ¶
Insert a node immediately before this node in the document. Passing in a nodeset will add all the nodes, in order.
func (*XmlNode) Attr ¶
Attr returns the value of an attribute.
If you need to check for the existence of an attribute, use Attribute.
func (*XmlNode) Attribute ¶
func (xmlNode *XmlNode) Attribute(name string) (attribute *AttributeNode)
Return the attribute node, or nil if the attribute does not exist.
func (*XmlNode) AttributeList ¶
func (xmlNode *XmlNode) AttributeList() (attributes []*AttributeNode)
Return a document-ordered list of attribute nodes.
func (*XmlNode) Attributes ¶
func (xmlNode *XmlNode) Attributes() (attributes map[string]*AttributeNode)
Return the attribute nodes indexed by name.
func (*XmlNode) CountChildren ¶
CountChildren returns the number of child nodes.
func (*XmlNode) DeclareNamespace ¶
Add a namespace declaration to an element.
This is typically done on the root element or node high up in the tree to avoid duplication. The declaration is not created if the namespace is already declared in this scope with the same prefix.
func (*XmlNode) DeclaredNamespaces ¶
func (xmlNode *XmlNode) DeclaredNamespaces() (result []NamespaceDeclaration)
Returns a list of all the namespace declarations that exist on this node.
You can add a namespace declaration by calling DeclareNamespace. Calling SetNamespace will automatically add a declaration if required.
Calling SetNsAttr does *not* automatically create a declaration. This will fixed in a future version.
func (*XmlNode) DuplicateTo ¶
func (*XmlNode) EvalXPath ¶
func (xmlNode *XmlNode) EvalXPath(data interface{}, v xpath.VariableScope) (result interface{}, err error)
Evaluate an XPath and return a result of the appropriate type. If a non-nil VariableScope is provided, any variables or functions present in the xpath will be resolved.
If the result is a nodeset (or the empty nodeset), a nodeset will be returned.
If the result is a number, a float64 will be returned.
If the result is a boolean, a bool will be returned.
In any other cases, the result will be coerced to a string.
func (*XmlNode) EvalXPathAsBoolean ¶
func (xmlNode *XmlNode) EvalXPathAsBoolean(data interface{}, v xpath.VariableScope) (result bool)
Evaluate an XPath and coerce the result to a boolean according to the XPath rules. In the presence of an error, this function will return false even if the expression cannot actually be evaluated.
In most cases you are better advised to call EvalXPath; this function is intended for packages that implement XML standards and that are fully aware of the consequences of suppressing a compilation error.
If a non-nil VariableScope is provided, any variables or registered functions present in the xpath will be resolved.
func (*XmlNode) FirstChild ¶
func (*XmlNode) InsertAfter ¶
func (*XmlNode) InsertBefore ¶
func (*XmlNode) InsertBegin ¶
func (*XmlNode) IsValid ¶
Returns true if the node is valid. Nodes become invalid when Remove() is called.
func (*XmlNode) LineNumber ¶
Returns the line number on which the node appears, or a -1 if the line number cannot be determined.
func (*XmlNode) MyDocument ¶
Return the document containing this node. Removed or unlinked nodes still have a document associated with them.
func (*XmlNode) Namespace ¶
The namespace of the node. This is the empty string if there no associated namespace.
func (*XmlNode) NextSibling ¶
NextSibling returns the next sibling (if any) of the current node. It is often used when iterating over the children of a node.
func (*XmlNode) Parent ¶
Parent returns the parent of the current node (or nil if there isn't one). This will always be an element or document node, as those are the only node types that can have children.
func (*XmlNode) ParseFragment ¶
func (xmlNode *XmlNode) ParseFragment(input, url []byte, options ParseOption) (fragment *DocumentFragment, err error)
func (*XmlNode) Path ¶
Path returns an XPath expression that can be used to select this node in the document.
func (*XmlNode) PreviousSibling ¶
PreviousSibling returns the previous sibling (if any) of the current node. It is often used when iterating over the children of a node in reverse.
func (*XmlNode) RecursivelyRemoveNamespaces ¶
func (*XmlNode) RemoveDefaultNamespace ¶
func (xmlNode *XmlNode) RemoveDefaultNamespace()
func (*XmlNode) ResetChildren ¶
func (xmlNode *XmlNode) ResetChildren()
func (*XmlNode) ResetNodePtr ¶
func (xmlNode *XmlNode) ResetNodePtr()
func (*XmlNode) Search ¶
Search for nodes that match an XPath. This is the simplest way to look for nodes.
func (*XmlNode) SearchWithVariables ¶
func (xmlNode *XmlNode) SearchWithVariables(data interface{}, v xpath.VariableScope) (result []Node, err error)
As the Search function, but passing a VariableScope that can be used to reolve variable names or registered function references in the XPath being evaluated.
func (*XmlNode) SerializeWithFormat ¶
func (xmlNode *XmlNode) SerializeWithFormat(format SerializationOption, encoding, outputBuffer []byte) ([]byte, int)
SerializeWithFormat allows you to control the serialization flags passed to libxml. In most cases ToXml() and ToHtml() provide sensible defaults and should be preferred.
The format parameter should be a set of SerializationOption constants or'd together. If encoding is nil, the document's output encoding is used - this defaults to UTF-8. If outputBuffer is nil, one will be created for you.
func (*XmlNode) SetAttr ¶
SetAttr sets the value of an attribute. If the attribute is in a namespace, use SetNsAttr instead.
While this call accepts QNames for the name parameter, it does not check their validity.
Attributes such as "xml:lang" or "xml:space" are not is a formal namespace and should be set by calling SetAttr with the prefix as part of the name.
func (*XmlNode) SetChildren ¶
func (*XmlNode) SetContent ¶
func (*XmlNode) SetInnerHtml ¶
func (*XmlNode) SetNamespace ¶
Set the namespace of an element.
func (*XmlNode) SetNsAttr ¶
SetNsAttr sets the value of a namespaced attribute.
Attributes such as "xml:lang" or "xml:space" are not is a formal namespace and should be set by calling SetAttr with the xml prefix as part of the name.
The namespace should already be declared and in-scope when SetNsAttr is called. This restriction will be lifted in a future version.
func (*XmlNode) ToHtml ¶
ToHtml generates an indented XML document that conforms to HTML 4.0 rules; meaning that some elements may be unclosed or forced to use end tags even when empty.
If you want to output XHTML, call SerializeWithFormat and enable the XML_SAVE_XHTML flag as part of the format.
If encoding is nil, the document's output encoding is used - this defaults to UTF-8. If outputBuffer is nil, one will be created for you.
func (*XmlNode) ToUnformattedXml ¶
ToUnformattedXml generates an unformatted XML document without an XML declaration. This is useful for conforming to various standards and for unit testing, although the output is not guaranteed to be well formed unless xmlNode is an element node.
func (*XmlNode) ToXml ¶
ToXml generates an indented XML document with an XML declaration. It is not guaranteed to be well formed unless xmlNode is an element node, or a document node with only one element child.
If you need finer control over the formatting, call SerializeWithFormat.
If encoding is nil, the document's output encoding is used - this defaults to UTF-8. If outputBuffer is nil, one will be created for you.