goxpath

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: BSD-3-Clause Imports: 20 Imported by: 5

README

goxpath

An XPath 2.0+ evaluator written in Go with selected XPath 3.1 functions (maps, arrays, JSON).

Built on goxml for the XML tree model.

Usage

doc, _ := goxml.Parse(strings.NewReader(`<root><item id="1">Hello</item></root>`))
xp := &goxpath.Parser{Ctx: goxpath.NewContext(doc)}
result, _ := xp.Evaluate("//item[@id='1']")
fmt.Println(result) // Hello

See pkg.go.dev for the full Go API.

Documentation

The full XPath function reference is at:

https://doc.speedata.de/goxml/

Supported Functions

String, numeric, date/time, sequence, node, QName, URI, regex, JSON (json-to-xml, xml-to-json), map and array functions. See the documentation for the complete list.

License

MIT — see LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConversion is returned in case of an unsuccessful cast.
	ErrConversion = fmt.Errorf("conversion failed")
)
View Source
var ErrSequence = fmt.Errorf("a sequence with more than one item is not allowed here")

ErrSequence is raised when a sequence of items is not allowed as an argument.

Functions

func BooleanValue

func BooleanValue(s Sequence) (bool, error)

BooleanValue returns the effective boolean value of the sequence. XPath 2.0 §2.4.3: if the first item is a node, returns true; a single atomic value is converted to boolean; otherwise FORG0006.

func ItemStringvalue added in v1.0.6

func ItemStringvalue(itm Item) string

ItemStringvalue returns the string value of an individual item.

func NumberValue

func NumberValue(s Sequence) (float64, error)

NumberValue returns the sequence converted to a float.

func RegisterFunction

func RegisterFunction(f *Function)

RegisterFunction registers an XPath function

func StringValue

func StringValue(s Sequence) (string, error)

StringValue returns the string value of the sequence by concatenating the string values of each item.

func ToXSInteger

func ToXSInteger(itm Item) (int, error)

ToXSInteger converts the item to an xs:integer.

Types

type Context

type Context struct {
	Namespaces map[string]string           // Storage for (private) name spaces
	Store      map[interface{}]interface{} // Store can be used for private variables accessible in functions
	Pos        int                         // Used to determine the position() in the sequence
	// contains filtered or unexported fields
}

Context is needed for variables, namespaces and XML navigation.

func CopyContext

func CopyContext(cur *Context) *Context

CopyContext creates a new context with the underlying xml document but can be changed without changing the original context.

func NewContext

func NewContext(doc *goxml.XMLDocument) *Context

NewContext returns a context from the xml document

func (*Context) Attributes

func (ctx *Context) Attributes(tf testfuncAttributes) (Sequence, error)

Attributes returns all attributes of the current node that satisfy the testfunc

func (*Context) Current

func (ctx *Context) Current(tf testfuncChildren) (Sequence, error)

Current returns all elements in the context that satisfy the testfunc.

func (*Context) CurrentItem added in v1.0.6

func (ctx *Context) CurrentItem() Item

CurrentItem returns the XSLT current() item.

func (*Context) Document

func (ctx *Context) Document() goxml.XMLNode

Document moves the node navigator to the document and retuns it

func (*Context) Filter

func (ctx *Context) Filter(filter EvalFunc) (Sequence, error)

Filter applies predicates to the context

func (*Context) GetContextSequence

func (ctx *Context) GetContextSequence() Sequence

GetContextSequence returns the current context.

func (*Context) ResetFrom added in v1.0.7

func (ctx *Context) ResetFrom(src *Context)

ResetFrom reuses an existing context by copying state from src. Unlike CopyContext, it reuses the existing map allocations instead of creating new maps, which reduces GC pressure in tight loops.

func (*Context) Root

func (ctx *Context) Root() (Sequence, error)

Root moves the node navigator to the root node of the document.

func (*Context) SetContextSequence

func (ctx *Context) SetContextSequence(seq Sequence) Sequence

SetContextSequence sets the context sequence and returns the previous one.

func (*Context) SetCurrentItem added in v1.0.6

func (ctx *Context) SetCurrentItem(item Item)

SetCurrentItem sets the XSLT current() item.

func (*Context) SetSize added in v1.0.6

func (ctx *Context) SetSize(n int)

SetSize sets the context size used by last().

func (*Context) Size added in v1.0.6

func (ctx *Context) Size() int

Size returns the context size used by last().

type EvalFunc

type EvalFunc func(*Context) (Sequence, error)

EvalFunc returns a sequence evaluating the XPath expression in the given context.

func ParseXPath

func ParseXPath(tl *Tokenlist) (EvalFunc, error)

ParseXPath takes a previously created token list and returns a function that can be used to evaluate the XPath expression in different contexts.

type Function

type Function struct {
	Name      string
	Namespace string
	F         func(*Context, []Sequence) (Sequence, error)
	MinArg    int
	MaxArg    int
}

Function represents an XPath function

type Item

type Item interface{}

An Item can hold anything such as a number, a string or a node.

type MapEntry added in v1.0.5

type MapEntry struct {
	Key   Item
	Value Sequence
}

MapEntry represents a key-value pair in an XPath map.

type Parser

type Parser struct {
	Ctx *Context
}

Parser contains all necessary references to the parser

func NewParser

func NewParser(r io.Reader) (*Parser, error)

NewParser returns a context to be filled

func (*Parser) Evaluate

func (xp *Parser) Evaluate(xpath string) (Sequence, error)

Evaluate reads an XPath expression and evaluates it in the given context. Parsed expressions are cached so that repeated evaluation of the same XPath string avoids re-tokenizing and re-parsing.

func (*Parser) SetVariable

func (xp *Parser) SetVariable(name string, value Sequence)

SetVariable is used to set a variable name.

func (*Parser) XMLDocument

func (xp *Parser) XMLDocument() *goxml.XMLDocument

XMLDocument returns the underlying XML document

type Sequence

type Sequence []Item

A Sequence is a list of Items

func (Sequence) IntValue

func (s Sequence) IntValue() (int, error)

IntValue returns the sequence value as an integer.

func (Sequence) String

func (s Sequence) String() string

func (Sequence) Stringvalue

func (s Sequence) Stringvalue() string

Stringvalue returns the concatenation of the string value of each item.

func (Sequence) StringvalueJoin added in v1.0.5

func (s Sequence) StringvalueJoin(sep string) string

StringvalueJoin returns the string values of all items joined by sep.

type Tokenlist

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

Tokenlist represents units of XPath language elements.

type XPathArray added in v1.0.5

type XPathArray struct {
	Members []Sequence
}

XPathArray represents an XPath 3.1 array.

func (*XPathArray) Get added in v1.0.5

func (a *XPathArray) Get(pos int) (Sequence, error)

Get returns the member at the given 1-based index.

func (*XPathArray) Size added in v1.0.5

func (a *XPathArray) Size() int

Size returns the number of members in the array.

type XPathMap added in v1.0.5

type XPathMap struct {
	Entries []MapEntry
}

XPathMap represents an XPath 3.1 map.

func (*XPathMap) Contains added in v1.0.5

func (m *XPathMap) Contains(key Item) bool

Contains checks if a key exists in the map.

func (*XPathMap) Get added in v1.0.5

func (m *XPathMap) Get(key Item) (Sequence, bool)

Get looks up a key in the map by comparing string values.

func (*XPathMap) Keys added in v1.0.5

func (m *XPathMap) Keys() Sequence

Keys returns all keys in the map as a Sequence.

func (*XPathMap) Size added in v1.0.5

func (m *XPathMap) Size() int

Size returns the number of entries in the map.

type XSDate

type XSDate time.Time

XSDate is a date instance

func (XSDate) String

func (d XSDate) String() string

type XSDateTime

type XSDateTime time.Time

XSDateTime is a date time instance

func (XSDateTime) String

func (d XSDateTime) String() string

type XSDuration added in v1.0.5

type XSDuration struct {
	Negative bool
	Years    int
	Months   int
	Days     int
	Hours    int
	Minutes  int
	Seconds  float64
}

XSDuration represents an xs:duration value with separate date and time components.

func ParseXSDuration added in v1.0.5

func ParseXSDuration(s string) (XSDuration, error)

ParseXSDuration parses an ISO 8601 duration string (e.g. "P1Y2M3DT4H5M6.5S", "-P1Y").

func (XSDuration) String added in v1.0.5

func (d XSDuration) String() string

type XSQName added in v1.0.5

type XSQName struct {
	Namespace string
	Prefix    string
	Localname string
}

XSQName represents an xs:QName value with namespace URI, prefix, and local name.

func (XSQName) String added in v1.0.5

func (q XSQName) String() string

type XSTime

type XSTime time.Time

XSTime is a time instance

func (XSTime) String

func (d XSTime) String() string

Jump to

Keyboard shortcuts

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