Skip to content

speedata/goxpath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Functions Related to QNames

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.