xpath

package
v0.0.0-...-e6d9de6 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: MIT Imports: 4 Imported by: 0

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

View Source
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

View Source
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().

func Number

func Number(r types.XPathResult, err error) float64

Number returns the numeric 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 String

func String(r types.XPathResult, err error) string

String returns the string 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()

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

func NewContext(n ...types.Node) (*Context, error)

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

func (x *Context) Exists(xpath string) bool

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) Free

func (x *Context) Free()

Free releases the underlying C structs in the XPath

func (*Context) LookupNamespaceURI

func (x *Context) LookupNamespaceURI(prefix string) (string, error)

LookupNamespaceURI looksup the namespace URI associated with prefix

func (*Context) Pointer

func (x *Context) Pointer() uintptr

Pointer returns a pointer to the underlying C struct

func (*Context) RegisterNS

func (x *Context) RegisterNS(name, nsuri string) error

RegisterNS registers a namespace so it can be used in an Expression

func (*Context) SetContextNode

func (x *Context) SetContextNode(n types.Node) error

SetContextNode sets or resets the context node which XPath expressions will be evaluated against.

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) Bool

func (x Object) Bool() bool

Bool returns the boolval component of the Object

func (*Object) Free

func (x *Object) Free()

Free releases the underlying C structs

func (Object) NodeIter

func (x Object) NodeIter() types.NodeIter

func (Object) NodeList

func (x Object) NodeList() types.NodeList

NodeList returns the list of nodes included in this Object

func (Object) Number

func (x Object) Number() float64

Number returns the floatval component of the Object as float64

func (Object) Pointer

func (x Object) Pointer() uintptr

Pointer returns the underlying C struct

func (Object) String

func (x Object) String() 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

type Result

type Result types.XPathResult

Result is an alias to types.XPathResult

Jump to

Keyboard shortcuts

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