Documentation
¶
Index ¶
- Constants
- Variables
- func BooleanValue(s Sequence) (bool, error)
- func FunctionExists(namespace, name string) bool
- func IntIsSubtypeOf(child, ancestor IntSubtype) bool
- func ItemStringvalue(itm Item) string
- func NumberValue(s Sequence) (float64, error)
- func RegisterCollation(uri string, factory CollationFactory)
- func RegisterFunction(f *Function)
- func StrIsSubtypeOf(child, ancestor StrSubtype) bool
- func StringValue(s Sequence) (string, error)
- func ToFloat64(itm any) (float64, bool)
- func ToXSInteger(itm Item) (int, error)
- func TypeIDOf(itm any) string
- func XPathErrorCode(err error) (string, bool)
- type Collation
- type CollationFactory
- type Context
- func (ctx *Context) Attributes(tf testfuncAttributes) (Sequence, error)
- func (ctx *Context) Collation() Collation
- func (ctx *Context) Current(tf testfuncChildren) (Sequence, error)
- func (ctx *Context) CurrentItem() Item
- func (ctx *Context) CurrentTime() time.Time
- func (ctx *Context) Document() goxml.XMLNode
- func (ctx *Context) Filter(filter EvalFunc) (Sequence, error)
- func (ctx *Context) GetContextSequence() Sequence
- func (ctx *Context) GetDecimalFormat(name string) (*DecimalFormat, error)
- func (ctx *Context) ResetFrom(src *Context)
- func (ctx *Context) Root() (Sequence, error)
- func (ctx *Context) SetContextSequence(seq Sequence) Sequence
- func (ctx *Context) SetCurrentItem(item Item)
- func (ctx *Context) SetDecimalFormat(name string, df *DecimalFormat)
- func (ctx *Context) SetSize(n int)
- func (ctx *Context) Size() int
- type DecimalFormat
- type EvalFunc
- type Function
- type IntSubtype
- type Item
- type MapEntry
- type NumericTypeID
- type Parser
- type Sequence
- type StrSubtype
- type Tokenlist
- type XPathArray
- type XPathError
- type XPathFunction
- type XPathMap
- type XSAnyURI
- type XSBase64Binary
- type XSDate
- type XSDateTime
- type XSDecimal
- type XSDouble
- type XSDuration
- type XSFloat
- type XSGDay
- type XSGMonth
- type XSGMonthDay
- type XSGYear
- type XSGYearMonth
- type XSHexBinary
- type XSInteger
- type XSQName
- type XSString
- type XSTime
- type XSUntypedAtomic
Constants ¶
const ( CodepointCollationURI = "http://www.w3.org/2005/xpath-functions/collation/codepoint" HTMLAsciiCaseInsensitiveURI = "http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive" UCACollationURI = "http://www.w3.org/2013/collation/UCA" UCACollationFallbackURI = "http://www.w3.org/2013/collation/UCA/" // tolerated alias )
Standard collation URIs defined by XPath 3.1 / XSLT 3.0.
Variables ¶
var ErrConversion = fmt.Errorf("conversion failed")
ErrConversion is returned in case of an unsuccessful cast.
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 ¶
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 FunctionExists ¶ added in v1.0.10
FunctionExists returns true if a function with the given namespace and local name is registered.
func IntIsSubtypeOf ¶ added in v1.0.10
func IntIsSubtypeOf(child, ancestor IntSubtype) bool
IntIsSubtypeOf returns true if child is the same as or a subtype of ancestor.
func ItemStringvalue ¶ added in v1.0.6
ItemStringvalue returns the string value of an individual item.
func NumberValue ¶
NumberValue returns the sequence converted to a float.
func RegisterCollation ¶ added in v1.0.12
func RegisterCollation(uri string, factory CollationFactory)
RegisterCollation registers a collation factory for a given URI prefix. The most specific (longest) match wins. Pass an exact URI for parameter-less collations such as the codepoint collation.
func RegisterFunction ¶
func RegisterFunction(f *Function)
RegisterFunction registers an XPath function
func StrIsSubtypeOf ¶ added in v1.0.10
func StrIsSubtypeOf(child, ancestor StrSubtype) bool
StrIsSubtypeOf returns true if child is the same as or a subtype of ancestor.
func StringValue ¶
StringValue returns the string value of the sequence by concatenating the string values of each item.
func ToFloat64 ¶ added in v1.0.10
ToFloat64 extracts the float64 value from any numeric item. Returns the value and true if the item is numeric, or 0 and false otherwise.
func ToXSInteger ¶
ToXSInteger converts the item to an xs:integer.
func XPathErrorCode ¶ added in v1.0.10
XPathErrorCode extracts the error code from an error, if it is an XPathError. Returns the code and true if successful, or empty string and false otherwise.
Types ¶
type Collation ¶ added in v1.0.12
type Collation interface {
// URI returns the canonical URI identifying this collation.
URI() string
// Compare returns -1, 0 or +1 like strings.Compare under this collation.
Compare(a, b string) int
// Equal reports whether the two strings are equal under this collation.
Equal(a, b string) bool
// Contains reports whether sub occurs as a (collation-equal) substring of s.
Contains(s, sub string) bool
// StartsWith reports whether s begins with prefix under this collation.
StartsWith(s, prefix string) bool
// EndsWith reports whether s ends with suffix under this collation.
EndsWith(s, suffix string) bool
// SubstringBefore returns the part of s before the first occurrence of sub,
// or "" if sub does not occur in s. The empty sub returns "".
SubstringBefore(s, sub string) string
// SubstringAfter returns the part of s after the first occurrence of sub,
// or "" if sub does not occur in s. The empty sub returns s.
SubstringAfter(s, sub string) string
// Key returns an opaque sort key suitable for use as a hash-map key.
// Two strings are Equal iff their Keys are equal.
Key(s string) string
}
Collation is a named string comparison rule used by XPath/XSLT functions such as fn:compare, fn:contains, xsl:sort etc.
All methods must be safe for concurrent use.
func CodepointCollation ¶ added in v1.0.12
func CodepointCollation() Collation
CodepointCollation returns the (cached) Unicode codepoint collation.
func ResolveCollation ¶ added in v1.0.12
ResolveCollation looks up a collation by URI, instantiating it on demand. Returns FOCH0002 if the URI cannot be resolved.
type CollationFactory ¶ added in v1.0.12
CollationFactory builds a Collation from URI parameters. The full URI is passed for round-tripping; params is the parsed query string.
type Context ¶
type Context struct {
Namespaces map[string]string // Storage for (private) name spaces
Store map[any]any // Store can be used for private variables accessible in functions
Pos int // Used to determine the position() in the sequence
// DefaultCollation is the static default collation, used by string operators
// and by string functions when no explicit collation argument is supplied.
// If nil, the Unicode codepoint collation is used.
DefaultCollation Collation
// contains filtered or unexported fields
}
Context is needed for variables, namespaces and XML navigation.
func CopyContext ¶
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 ¶
Attributes returns all attributes of the current node that satisfy the testfunc
func (*Context) Collation ¶ added in v1.0.12
Collation returns the static default collation, falling back to the Unicode codepoint collation if none is set.
func (*Context) CurrentItem ¶ added in v1.0.6
CurrentItem returns the XSLT current() item.
func (*Context) CurrentTime ¶ added in v1.0.10
CurrentTime returns the stable current time for this evaluation. Per XPath spec, current-dateTime() returns the same value within a single evaluation. The time is cached on first access and reused for all subsequent calls.
func (*Context) GetContextSequence ¶
GetContextSequence returns the current context.
func (*Context) GetDecimalFormat ¶ added in v1.0.10
func (ctx *Context) GetDecimalFormat(name string) (*DecimalFormat, error)
GetDecimalFormat returns the decimal format with the given name. Returns the default format for name "". Returns nil and an error for unknown named formats (FODF1280).
func (*Context) ResetFrom ¶ added in v1.0.7
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) SetContextSequence ¶
SetContextSequence sets the context sequence and returns the previous one.
func (*Context) SetCurrentItem ¶ added in v1.0.6
SetCurrentItem sets the XSLT current() item.
func (*Context) SetDecimalFormat ¶ added in v1.0.10
func (ctx *Context) SetDecimalFormat(name string, df *DecimalFormat)
SetDecimalFormat registers a named decimal format on the context. Use name "" for the default (unnamed) format. For qualified names, use "namespace-uri localname" as key.
type DecimalFormat ¶ added in v1.0.10
type DecimalFormat struct {
DecimalSeparator rune // default '.'
GroupingSeparator rune // default ','
MinusSign rune // default '-'
Percent rune // default '%'
PerMille rune // default '\u2030'
ZeroDigit rune // default '0'
Digit rune // default '#'
PatternSeparator rune // default ';'
Infinity string // default "Infinity"
NaN string // default "NaN"
ExponentSeparator rune // default 'e'
}
DecimalFormat defines the properties of a decimal format as per XPath 3.1 / XSLT 3.0 (Section 4.7.1). Use DefaultDecimalFormat() to create an instance with spec-default values.
func DefaultDecimalFormat ¶ added in v1.0.10
func DefaultDecimalFormat() *DecimalFormat
DefaultDecimalFormat returns a DecimalFormat with the XPath spec-default values.
type EvalFunc ¶
EvalFunc returns a sequence evaluating the XPath expression in the given context.
func ParseXPath ¶
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
DynamicCallError string // if non-empty, dynamic calls (via function reference) raise this error
}
Function represents an XPath function
type IntSubtype ¶ added in v1.0.10
type IntSubtype uint8
IntSubtype identifies the specific integer subtype in the XSD type hierarchy.
const ( IntInteger IntSubtype = iota // xs:integer IntLong // xs:long IntInt // xs:int IntShort // xs:short IntByte // xs:byte IntNonNegativeInteger // xs:nonNegativeInteger IntUnsignedLong // xs:unsignedLong IntUnsignedInt // xs:unsignedInt IntUnsignedShort // xs:unsignedShort IntUnsignedByte // xs:unsignedByte IntPositiveInteger // xs:positiveInteger IntNonPositiveInteger // xs:nonPositiveInteger IntNegativeInteger // xs:negativeInteger )
type Item ¶
type Item any
An Item can hold anything such as a number, a string or a node.
func WrapNumeric ¶ added in v1.0.10
func WrapNumeric(val float64, nt NumericTypeID) Item
WrapNumeric wraps a float64 result in the appropriate numeric type.
type NumericTypeID ¶ added in v1.0.10
type NumericTypeID int
NumericTypeID identifies the XPath numeric type of an item.
const ( NumInteger NumericTypeID = iota NumDecimal NumFloat NumDouble NumUnknown )
func NumericType ¶ added in v1.0.10
func NumericType(itm any) NumericTypeID
NumericType returns the numeric type ID for an item.
func PromoteNumeric ¶ added in v1.0.10
func PromoteNumeric(a, b NumericTypeID) NumericTypeID
PromoteNumeric returns the promoted type for a binary numeric operation. XPath type promotion: integer < decimal < float < double.
type Parser ¶
type Parser struct {
Ctx *Context
}
Parser contains all necessary references to the parser
func (*Parser) Evaluate ¶
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 ¶
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) Stringvalue ¶
Stringvalue returns the concatenation of the string value of each item.
func (Sequence) StringvalueJoin ¶ added in v1.0.5
StringvalueJoin returns the string values of all items joined by sep.
type StrSubtype ¶ added in v1.0.10
type StrSubtype uint8
StrSubtype identifies the specific string subtype in the XSD type hierarchy.
const ( StrString StrSubtype = iota // xs:string StrNormalizedString // xs:normalizedString StrToken // xs:token StrLanguage // xs:language StrNMTOKEN // xs:NMTOKEN StrName // xs:Name StrNCName // xs:NCName StrID // xs:ID StrIDREF // xs:IDREF StrENTITY // xs:ENTITY )
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 XPathError ¶ added in v1.0.10
type XPathError struct {
Code string // e.g. "XPTY0004", "FORG0001", "FOER0000"
Description string // human-readable description
Value Sequence // optional error value (for fn:error)
}
XPathError represents a structured XPath error with an error code, description, and optional error value. This implements the error interface and is used by try/catch expressions to match error codes.
func NewXPathError ¶ added in v1.0.10
func NewXPathError(code, description string) *XPathError
NewXPathError creates a new XPathError with the given code and description.
func (*XPathError) Error ¶ added in v1.0.10
func (e *XPathError) Error() string
Error implements the error interface.
type XPathFunction ¶ added in v1.0.10
type XPathFunction struct {
Name string
Namespace string
Arity int
Fn func(*Context, []Sequence) (Sequence, error)
DynamicCallError string // if non-empty, calling this function reference raises this error
}
XPathFunction represents a callable function reference (XPath 3.1). Created by named function references (fn#arity) or inline functions.
type XPathMap ¶ added in v1.0.5
type XPathMap struct {
Entries []MapEntry
}
XPathMap represents an XPath 3.1 map.
type XSBase64Binary ¶ added in v1.0.10
type XSBase64Binary string
XSBase64Binary represents an xs:base64Binary value.
type XSDateTime ¶
XSDateTime is a date time instance
func (XSDateTime) String ¶
func (d XSDateTime) String() string
type XSDecimal ¶ added in v1.0.10
type XSDecimal float64
XSDecimal represents an xs:decimal value (no INF, no NaN).
type XSDouble ¶ added in v1.0.10
type XSDouble float64
XSDouble represents an xs:double value (IEEE 754 double precision).
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 XSFloat ¶ added in v1.0.10
type XSFloat float64
XSFloat represents an xs:float value (IEEE 754 single precision, stored as float64).
type XSGYearMonth ¶ added in v1.0.10
type XSGYearMonth string
XSGYearMonth represents xs:gYearMonth.
type XSHexBinary ¶ added in v1.0.10
type XSHexBinary string
XSHexBinary represents an xs:hexBinary value (uppercase hex string).
type XSInteger ¶ added in v1.0.10
type XSInteger struct {
V int
Subtype IntSubtype
}
XSInteger represents an xs:integer value with subtype information.
type XSQName ¶ added in v1.0.5
XSQName represents an xs:QName value with namespace URI, prefix, and local name.
type XSString ¶ added in v1.0.10
type XSString struct {
V string
Subtype StrSubtype
}
XSString represents an xs:string value with subtype information.
type XSUntypedAtomic ¶ added in v1.0.10
type XSUntypedAtomic string
XSUntypedAtomic represents an xs:untypedAtomic value.