XPath 2.0+ package for Go, with XPath 3.1 maps and arrays.
License: BSD-3-Clause License
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 )
}
}
When working with namespaces, you need to have them predefined in your query:
myfile.xml:
<a : root xmlns : a =" anamespace" >
<a : sub >text</a : sub >
</a : root >
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
}
xp .Ctx .Namespaces ["a" ] = "anamespace"
seq , err := xp .Evaluate ("/a:root/a:sub/text()" )
if err != nil {
return err
}
fmt .Println (seq .Stringvalue ())
return nil
}
func main () {
if err := dothings (); err != nil {
log .Fatal (err )
}
}
No schema types
No collations
Not all functions are implemented (see list below)
This list is copied from XQuery 1.0 and XPath 2.0 Functions and Operators (Second Edition)
Function
Accessor
Accepts
Returns
string
string-value
an optional item or no argument
xs:string
data
typed-value
zero or more items
a sequence of atomic values
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.
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 to Assemble and Disassemble Strings
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.
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 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
resolve-uri
Returns an xs:anyURI representing an absolute xs:anyURI given a base URI and a relative URI.
Timezone Adjustment Functions on Dates and Time Values
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.
local-name
Returns the local name of the context node or the specified node as an xs:NCName.
lang
Returns true or false, depending on whether the language of the given node, as defined using the xml:lang attribute, is the same as, or a sublanguage of, the language specified by the argument.
number
Returns the value of the context item after atomization or the specified argument converted to an xs:double.
root
Returns the root of the tree to which the node argument belongs.
Function
Meaning
boolean
Computes the effective boolean value of the argument sequence.
empty
Indicates whether or not the provided sequence is empty.
exists
Indicates whether or not the provided sequence is not empty.
reverse
Reverses the order of items in a sequence.
Function
Meaning
avg
Returns the average of a sequence of values.
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.
sum
Returns the sum of a sequence of 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.
Function
Meaning
distinct-values
Returns a sequence with duplicate values removed.
index-of
Returns the positions of items in a sequence that match a given value.
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.
unordered
Returns the items in the given sequence in a non-deterministic order.
subsequence
Returns the subsequence of a given sequence, identified by location.
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.
Functions and Operators that Generate Sequences
Function
Meaning
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.
Formatting Functions (XPath 3.0)
Function
Meaning
format-date
Formats an xs:date value using a picture string.
format-number
Formats a number as a string using a picture string.
Function
Meaning
sort
Returns a sorted copy of a sequence.
Namespace: http://www.w3.org/2005/xpath-functions/map
Function
Meaning
map:get
Returns the value associated with a key in a map.
map:size
Returns the number of entries in a map.
map:keys
Returns the keys of a map as a sequence.
map:contains
Returns true if the map contains an entry with the given key.
map:put
Returns a map with an added or replaced entry.
map:merge
Returns a map that combines the entries from multiple maps.
XPath 3.1 Array Functions
Namespace: http://www.w3.org/2005/xpath-functions/array
Function
Meaning
array:get
Returns the member at a given position in an array.
array:size
Returns the number of members in an array.
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.
XML Schema Type Constructors
Function
Meaning
xs:integer
Cast to xs:integer.
xs:double
Cast to xs:double.
xs:string
Cast to xs:string.
xs:date
Cast to xs:date.
xs:dateTime
Cast to xs:dateTime.
xs:duration
Cast to xs:duration.
xs:time
Cast to xs:time.
Not yet implemented functions
Function
Accessor
Accepts
Returns
node-name
node-name
an optional node
zero or one xs:QName
nilled
nilled
a node
an optional xs:boolean
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 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.
collection
Returns a sequence of nodes retrieved using the specified URI or the nodes in the default collection.
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.