goxpath

package module
v0.0.0-...-744d7bd Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: BSD-3-Clause Imports: 11 Imported by: 1

README

Go reference documentation

XPath 2.0 package for Go.

License: BSD-3-Clause License

Usage

myfile.xml:

<data>
    <p>hello world!</p>
    <p>hello XML!</p>
</data>

main.go:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/speedata/goxpath"
)

func dothings() error {
	r, err := os.Open("myfile.xml")
	if err != nil {
		return err
	}
	xp, err := goxpath.NewParser(r)
	if err != nil {
		return err
	}
	seq, err := xp.Evaluate("for $i in /data/p return string($i)")
	if err != nil {
		return err
	}
	for _, itm := range seq {
		fmt.Println(itm)
	}
	return nil
}

func main() {
	if err := dothings(); err != nil {
		log.Fatal(err)
	}
}

Limitations

  • No schema types
  • No collations
  • Not all functions are implemented (see list below)

Implemented functions

This list is copied from XQuery 1.0 and XPath 2.0 Functions and Operators (Second Edition)

Accessors

Function Accessor Accepts Returns
string string-value an optional item or no argument xs:string

Functions on Numeric Values

Function Meaning
abs Returns the absolute value of the argument.
ceiling Returns the smallest number with no fractional part that is greater than or equal to the argument.
floor Returns the largest number with no fractional part that is less than or equal to the argument.
round Rounds to the nearest number with no fractional part.

Functions to Assemble and Disassemble Strings

Function Meaning
codepoints-to-string Creates an xs:string from a sequence of Unicode code points.
string-to-codepoints Returns the sequence of Unicode code points that constitute an xs:string.

Equality and Comparison of Strings

Function Meaning
compare Returns -1, 0, or 1, depending on whether the value of the first argument is respectively less than, equal to, or greater than the value of the second argument, according to the rules of the collation that is used.
codepoint-equal Returns true if the two arguments are equal using the Unicode code point collation.

Functions on String Values

Function Meaning
concat Concatenates two or more xs:anyAtomicType arguments cast to xs:string.
string-join Returns the xs:string produced by concatenating a sequence of xs:strings using an optional separator.
substring Returns the xs:string located at a specified place within an argument xs:string.
string-length Returns the length of the argument.
normalize-space Returns the whitespace-normalized value of the argument.
upper-case Returns the upper-cased value of the argument.
lower-case Returns the lower-cased value of the argument.
translate Returns the first xs:string argument with occurrences of characters contained in the second argument replaced by the character at the corresponding position in the third argument.

Functions Based on Substring Matching

Function Meaning
contains Indicates whether one xs:string contains another xs:string. A collation may be specified.
starts-with Indicates whether the value of one xs:string begins with the collation units of another xs:string. A collation may be specified.
ends-with Indicates whether the value of one xs:string ends with the collation units of another xs:string. A collation may be specified.
substring-before Returns the collation units of one xs:string that precede in that xs:string the collation units of another xs:string. A collation may be specified.
substring-after Returns the collation units of xs:string that follow in that xs:string the collation units of another xs:string. A collation may be specified.

String Functions that Use Pattern Matching

Function Meaning
matches Returns an xs:boolean value that indicates whether the value of the first argument is matched by the regular expression that is the value of the second argument.
replace Returns the value of the first argument with every substring matched by the regular expression that is the value of the second argument replaced by the replacement string that is the value of the third argument.
tokenize Returns a sequence of one or more xs:strings whose values are substrings of the value of the first argument separated by substrings that match the regular expression that is the value of the second argument.

Additional Boolean Constructor Functions

Function Meaning
true Constructs the xs:boolean value 'true'.
false Constructs the xs:boolean value 'false'.

Functions on Boolean Values

Function Meaning
not Inverts the xs:boolean value of the argument.

Component Extraction Functions on Durations, Dates and Times

Function Meaning
hours-from-time Returns the hours from an xs:time value.
minutes-from-time Returns the minutes from an xs:time value.
seconds-from-time Returns the seconds from an xs:time value.

Functions on Nodes

Function Meaning
local-name Returns the local name of the context node or the specified node as an xs:NCName.
number Returns the value of the context item after atomization or the specified argument converted to an xs:double.
Function Meaning
boolean Computes the effective boolean value of the argument sequence.
empty Indicates whether or not the provided sequence is empty.
reverse Reverses the order of items in a sequence.
Function Meaning
count Returns the number of items in a sequence.
max Returns the maximum value from a sequence of comparable values.
min Returns the minimum value from a sequence of comparable values.
Function Meaning
position Returns the position of the context item within the sequence of items currently being processed.
last Returns the number of items in the sequence of items currently being processed.

Context Functions

Function Meaning
current-dateTime Returns the current xs:dateTime.
current-date Returns the current xs:date.
current-time Returns the current xs:time.

Not yet implemented functions

Accessors

Function Accessor Accepts Returns
node-name node-name an optional node zero or one xs:QName
nilled nilled a node an optional xs:boolean
data typed-value zero or more items a sequence of atomic values
base-uri base-uri an optional node or no argument zero or one xs:anyURI
document-uri document-uri an optional node zero or one xs:anyURI

Functions on Numeric Values

Function Accessor Accepts Returns
round-half-to-even Takes a number and a precision and returns a number rounded to the given precision. If the fractional part is exactly half, the result is the number whose least significant digit is even.

Functions on String Values

Function Meaning
normalize-unicode Returns the normalized value of the first argument in the normalization form specified by the second argument.
encode-for-uri Returns the xs:string argument with certain characters escaped to enable the resulting string to be used as a path segment in a URI.
iri-to-uri Returns the xs:string argument with certain characters escaped to enable the resulting string to be used as (part of) a URI.
escape-html-uri Returns the xs:string argument with certain characters escaped in the manner that html user agents handle attribute values that expect URIs.

Functions on anyURI

Function Meaning
resolve-uri Returns an xs:anyURI representing an absolute xs:anyURI given a base URI and a relative URI.

Component Extraction Functions on Durations, Dates and Times

Function Meaning
years-from-duration Returns the year component of an xs:duration value.
months-from-duration Returns the months component of an xs:duration value.
days-from-duration Returns the days component of an xs:duration value.
hours-from-duration Returns the hours component of an xs:duration value.
minutes-from-duration Returns the minutes component of an xs:duration value.
seconds-from-duration Returns the seconds component of an xs:duration value.
year-from-dateTime Returns the year from an xs:dateTime value.
month-from-dateTime Returns the month from an xs:dateTime value.
day-from-dateTime Returns the day from an xs:dateTime value.
hours-from-dateTime Returns the hours from an xs:dateTime value.
minutes-from-dateTime Returns the minutes from an xs:dateTime value.
seconds-from-dateTime Returns the seconds from an xs:dateTime value.
timezone-from-dateTime Returns the timezone from an xs:dateTime value.
year-from-date Returns the year from an xs:date value.
month-from-date Returns the month from an xs:date value.
day-from-date Returns the day from an xs:date value.
timezone-from-date Returns the timezone from an xs:date value.
timezone-from-time Returns the timezone from an xs:time value.

Timezone Adjustment Functions on Dates and Time Values

Function Meaning
adjust-dateTime-to-timezone Adjusts an xs:dateTime value to a specific timezone, or to no timezone at all.
adjust-date-to-timezone Adjusts an xs:date value to a specific timezone, or to no timezone at all.
adjust-time-to-timezone Adjusts an xs:time value to a specific timezone, or to no timezone at all.

Additional Constructor Functions for QNames

Function Meaning
resolve-QName Returns an xs:QName with the lexical form given in the first argument. The prefix is resolved using the in-scope namespaces for a given element.
QName Returns an xs:QName with the namespace URI given in the first argument and the local name and prefix in the second argument.
Function Meaning
prefix-from-QName Returns an xs:NCName representing the prefix of the xs:QName argument.
local-name-from-QName Returns an xs:NCName representing the local name of the xs:QName argument.
namespace-uri-from-QName Returns the namespace URI for the xs:QName argument. If the xs:QName is in no namespace, the zero-length string is returned.
namespace-uri-for-prefix Returns the namespace URI of one of the in-scope namespaces for the given element, identified by its namespace prefix.
in-scope-prefixes Returns the prefixes of the in-scope namespaces for the given element.

Functions on Nodes

Function Meaning
name Returns the name of the context node or the specified node as an xs:string.
namespace-uri Returns the namespace URI as an xs:anyURI for the xs:QName of the argument node or the context node if the argument is omitted. This may be the URI corresponding to the zero-length string if the xs:QName is in no namespace.
lang Returns true or false, depending on whether the language of the given node or the context node, as defined using the xml:lang attribute, is the same as, or a sublanguage of, the language specified by the argument.
root Returns the root of the tree to which the node argument belongs.
Function Meaning
index-of Returns a sequence of xs:integers, each of which is the index of a member of the sequence specified as the first argument that is equal to the value of the second argument. If no members of the specified sequence are equal to the value of the second argument, the empty sequence is returned.
exists Indicates whether or not the provided sequence is not empty.
distinct-values Returns a sequence in which all but one of a set of duplicate values, based on value equality, have been deleted. The order in which the distinct values are returned is implementation dependent.
insert-before Inserts an item or sequence of items at a specified position in a sequence.
remove Removes an item from a specified position in a sequence.
subsequence Returns the subsequence of a given sequence, identified by location.
unordered Returns the items in the given sequence in a non-deterministic order.

Functions That Test the Cardinality of Sequences

Function Meaning
zero-or-one Returns the input sequence if it contains zero or one items. Raises an error otherwise.
one-or-more Returns the input sequence if it contains one or more items. Raises an error otherwise.
exactly-one Returns the input sequence if it contains exactly one item. Raises an error otherwise.

Equals, Union, Intersection and Except

Function Meaning
deep-equal Returns true if the two arguments have items that compare equal in corresponding positions.

Aggregate Functions

Function Meaning
avg Returns the average of a sequence of values.
sum Returns the sum of a sequence of values.

Functions and Operators that Generate Sequences

Function Meaning
id Returns the sequence of element nodes having an ID value matching the one or more of the supplied IDREF values.
idref Returns the sequence of element or attribute nodes with an IDREF value matching one or more of the supplied ID values.
doc Returns a document node retrieved using the specified URI.
doc-available Returns true if a document node can be retrieved using the specified URI.
collection Returns a sequence of nodes retrieved using the specified URI or the nodes in the default collection.

Context Functions

Function Meaning
implicit-timezone Returns the value of the implicit timezone property from the dynamic context.
default-collation Returns the value of the default collation property from the static context.
static-base-uri Returns the value of the Base URI property from the static context.

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.

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

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 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.

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.

type Tokenlist

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

Tokenlist represents units of XPath language elements.

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