Documentation ¶
Overview ¶
Package xpath contains tools to handle XPath evaluation.
Because of a very quirky dependency between this package and the github.com/lestrrat/libxml2/dom package, you MUST import both packages to properly use it.
import ( "github.com/lestrrat-go/libxml2/dom" "github.com/lestrrat-go/libxml2/xpath" )
Or, if you have no use for dom package in your program, and you don't want to use the magical "_" import, you can do the initialization yourself just to appease the compiler:
func init() { dom.SetupXPathCallback() }
Index ¶
- Constants
- Variables
- func Bool(r types.XPathResult, err error) bool
- func NodeIter(r types.XPathResult, err error) types.NodeIter
- func NodeList(r types.XPathResult, err error) types.NodeList
- func Number(r types.XPathResult, err error) float64
- func String(r types.XPathResult, err error) string
- type Context
- func (x *Context) Exists(xpath string) bool
- func (x *Context) Find(s string) (types.XPathResult, error)
- func (x *Context) FindExpr(expr types.XPathExpression) (types.XPathResult, error)
- func (x *Context) Free()
- func (x *Context) LookupNamespaceURI(prefix string) (string, error)
- func (x *Context) Pointer() uintptr
- func (x *Context) RegisterNS(name, nsuri string) error
- func (x *Context) SetContextNode(n types.Node) error
- type Expression
- type NodeIterator
- type Object
- type Result
Constants ¶
const ( UndefinedType = clib.XPathUndefinedType NodeSetType = clib.XPathNodeSetType BooleanType = clib.XPathBooleanType NumberType = clib.XPathNumberType StringType = clib.XPathStringType PointType = clib.XPathPointType RangeType = clib.XPathRangeType LocationSetType = clib.XPathLocationSetType UsersType = clib.XPathUsersType XSLTTreeType = clib.XPathXSLTTreeType )
Variables ¶
var WrapNodeFunc func(uintptr) (types.Node, error)
WrapNodeFunc is a function that gets called when Object.NodeList() is called. This is necessary because during the call to NodeList(), the underlying C pointers are materialized to objects in a different package ("github.com/lestrrat-go/libxml2/dom"), and said package uses this package... Yes, a circular dependency.
Normally this means that both pacckages should live under the same unified package, but in this case they are independent enough that we have decided they warrant to be separated.
So this WrapNodeFunc is our workaround for this problem: when github.com/lestrrat-go/libxml2/dom is loaded, it automatically initializes this function to an appropriate function on the fly.
Functions ¶
func Bool ¶
func Bool(r types.XPathResult, err error) bool
Bool returns the boolean component of the result, and as a side effect releases the Result by calling Free() on it. Use this if you do not really care about the error value from Find()
func NodeIter ¶
func NodeIter(r types.XPathResult, err error) types.NodeIter
NodeIter returns an iterator that will return the nodes assocaied with this reult, and as a side effect releases the result by calling Free() on it. Use this if you do not really care about the error value from Find().
func NodeList ¶
func NodeList(r types.XPathResult, err error) types.NodeList
NodeList returns the nodes associated with this result, and as a side effect releases the result by calling Free() on it. Use this if you do not really care about the error value from Find().
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context holds the current XPath context. You may register namespaces and context nodes to evaluate your XPath expressions with it.
func NewContext ¶
NewContext creates a new Context, optionally providing with a context node.
Note that although we are specifying `n... Node` for the argument, only the first, node is considered for the context node
func (*Context) Exists ¶
Exists compiles and evaluates the xpath expression, and returns true if a corresponding node exists
func (*Context) Find ¶
func (x *Context) Find(s string) (types.XPathResult, error)
Find evaluates the expression s against the nodes registered in x. It returns the resulting data evaluated to an Result.
You MUST call Free() on the Result, or you will leak memory If you don't really care for errors and just want to grab the value of Result, checkout xpath.String(), xpath.Number(), xpath.Bool() et al.
func (*Context) FindExpr ¶
func (x *Context) FindExpr(expr types.XPathExpression) (types.XPathResult, error)
FindExpr evaluates the given XPath expression and returns an Object. You must call `Free()` on this returned object
You MUST call Free() on the Result, or you will leak memory
func (*Context) LookupNamespaceURI ¶
LookupNamespaceURI looksup the namespace URI associated with prefix
func (*Context) RegisterNS ¶
RegisterNS registers a namespace so it can be used in an Expression
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
Expression is a compiled XPath expression
func NewExpression ¶
func NewExpression(s string) (*Expression, error)
NewExpression compiles the given XPath expression string
func (*Expression) Free ¶
func (x *Expression) Free()
Free releases the underlying C structs in the Expression
func (*Expression) Pointer ¶
func (x *Expression) Pointer() uintptr
Pointer returns the underlying C struct
func (Expression) String ¶
func (x Expression) String() string
String returns the expression as it was given to NewExpression
type NodeIterator ¶
type NodeIterator struct {
// contains filtered or unexported fields
}
NodeIterator is a way to get at a list of nodes returned by XPath et al in a lazy (and possibly more efficient) manner.
func NewNodeIterator ¶
func NewNodeIterator(nodes []uintptr) *NodeIterator
func (*NodeIterator) Next ¶
func (n *NodeIterator) Next() bool
Next returns true if there is at least one more node in the iterator.
func (*NodeIterator) Node ¶
func (n *NodeIterator) Node() types.Node
type Object ¶
type Object struct { // This flag controls if the StringValue should use the *contents* (literal value) // of the nodeset instead of stringifying the node ForceLiteral bool // contains filtered or unexported fields }
Object is the concrete implementatin of Result (types.XPathResult). This struct contains the result of evaluating an XPath expression.
func (Object) String ¶
String returns the stringified value of the nodes included in this Object. If the Object is anything other than a NodeSet, then we fallback to using fmt.Sprintf to generate some sort of readable output
func (Object) Type ¶
func (x Object) Type() clib.XPathObjectType
Type returns the clib.XPathObjectType