Documentation
¶
Index ¶
- func ExecAsNumber(cursor Cursor, expr *Grammar, settings ...ContextApply) (float64, error)
- func ExecAsString(cursor Cursor, expr *Grammar, settings ...ContextApply) (string, error)
- func GetCursorString(c Cursor) string
- func Unmarshal(result Result, value any, settings ...ContextApply) error
- func WithFunction(local string, fn Function) func(c *ContextSettings)
- func WithFunctionNS(space, local string, fn Function) func(c *ContextSettings)
- func WithFunctionName(name XmlName, fn Function) func(c *ContextSettings)
- func WithNS(name, url string) func(c *ContextSettings)
- func WithVariable(local string, value Result) func(c *ContextSettings)
- func WithVariableNS(space, local string, value Result) func(c *ContextSettings)
- func WithVariableName(name XmlName, value Result) func(c *ContextSettings)
- type Attribute
- type Bool
- type CharData
- type Comment
- type Context
- type ContextApply
- type ContextSettings
- type Cursor
- type Element
- type Function
- type Grammar
- type NamedNode
- type Namespace
- type Node
- type NodeSet
- type Number
- type Parser
- type ProcInst
- type Result
- type Root
- type String
- type XmlName
- type XmlParseOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecAsNumber ¶ added in v0.9.16
func ExecAsNumber(cursor Cursor, expr *Grammar, settings ...ContextApply) (float64, error)
Like Exec, except it returns the query as a number.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <root> <a>3.14</a> <a>9001</a> </root> ` xpath := xsel.MustBuildExpr(`/root/a`) cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) result, _ := xsel.ExecAsNumber(cursor, &xpath) // Be careful when returning the number result of NodeSet's. // Only the first node's value value will be returned. fmt.Println(result) }
Output: 3.14
func ExecAsString ¶ added in v0.9.16
func ExecAsString(cursor Cursor, expr *Grammar, settings ...ContextApply) (string, error)
Like Exec, except it returns the string result of the query.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <root> <a>This is the first node.</a> <a>This is the second node.</a> </root> ` xpath := xsel.MustBuildExpr(`/root/a`) cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) result, _ := xsel.ExecAsString(cursor, &xpath) // Be careful when returning the string result of NodeSet's. // Only the first node's string value will be returned. // If you want the string value of all node's, use ExecAsNodeset. fmt.Println(result) }
Output: This is the first node.
func GetCursorString ¶ added in v0.9.16
GetCursorString is a convenience method to return the string value of an individual Node.
func Unmarshal ¶ added in v0.9.13
func Unmarshal(result Result, value any, settings ...ContextApply) error
Unmarshal maps a XPath result to a struct or slice. When unmarshaling a slice, the result must be a NodeSet. When unmarshaling a struct, the result must be a NodeSet with one result. To unmarshal a value to a struct field, give it a "xsel" tag name, and a XPath expression for its value (e.g. `xsel:"//my-struct[@my-id = 'my-value']"`).
For struct fields, Unmarshal can set fields that are ints and uints, bools, strings, slices, and nested structs.
For slice elements, Unmarshal can set ints and uints, bools, strings, and structs. It cannot Unmarshal multidimensional slices.
Arrays, maps, and channels are not supported.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <Root xmlns="http://www.adventure-works.com"> <Customers> <Customer CustomerID="GREAL"> <CompanyName>Great Lakes Food Market</CompanyName> <ContactName>Howard Snyder</ContactName> <ContactTitle>Marketing Manager</ContactTitle> <FullAddress> <Address>2732 Baker Blvd.</Address> <City>Eugene</City> <Region>OR</Region> </FullAddress> </Customer> <Customer CustomerID="HUNGC"> <CompanyName>Hungry Coyote Import Store</CompanyName> <ContactName>Yoshi Latimer</ContactName> <FullAddress> <Address>City Center Plaza 516 Main St.</Address> <City>Walla Walla</City> <Region>WA</Region> </FullAddress> </Customer> </Customers> </Root> ` type Address struct { Address string `xsel:"NS:Address"` City string `xsel:"NS:City"` Region string `xsel:"NS:Region"` } type Customer struct { Id string `xsel:"@CustomerID"` Name string `xsel:"NS:CompanyName"` ContactName string `xsel:"NS:ContactName"` Address Address `xsel:"NS:FullAddress"` } type Customers struct { Customers []Customer `xsel:"NS:Customers/NS:Customer"` } contextSettings := xsel.WithNS("NS", "http://www.adventure-works.com") xpath := xsel.MustBuildExpr(`/NS:Root`) cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) result, _ := xsel.Exec(cursor, &xpath, contextSettings) customers := Customers{} _ = xsel.Unmarshal(result, &customers, contextSettings) // Remember to check for errors fmt.Printf("%+v\n", customers) }
Output: {Customers:[{Id:GREAL Name:Great Lakes Food Market ContactName:Howard Snyder Address:{Address:2732 Baker Blvd. City:Eugene Region:OR}} {Id:HUNGC Name:Hungry Coyote Import Store ContactName:Yoshi Latimer Address:{Address:City Center Plaza 516 Main St. City:Walla Walla Region:WA}}]}
func WithFunction ¶ added in v0.9.13
func WithFunction(local string, fn Function) func(c *ContextSettings)
WithFunction binds a custom function name with no namespace to a XPath query.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <root> <a>This is an element.</a> <!-- This is a comment. --> </root> ` isComment := func(context xsel.Context, args ...xsel.Result) (xsel.Result, error) { nodeSet, isNodeSet := context.Result().(xsel.NodeSet) if !isNodeSet || len(nodeSet) == 0 { return xsel.Bool(false), nil } _, isComment := nodeSet[0].Node().(xsel.Comment) return xsel.Bool(isComment), nil } xpath := xsel.MustBuildExpr(`//node()[is-comment()]`) cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) result, _ := xsel.Exec(cursor, &xpath, xsel.WithFunction("is-comment", isComment)) fmt.Println(result) }
Output: This is a comment.
func WithFunctionNS ¶ added in v0.9.13
func WithFunctionNS(space, local string, fn Function) func(c *ContextSettings)
WithFunctionNS binds a custom function name with a namespace to a XPath query.
func WithFunctionName ¶ added in v0.9.13
func WithFunctionName(name XmlName, fn Function) func(c *ContextSettings)
WithFunctionName binds a custom function name with a namespace to a XPath query.
func WithNS ¶ added in v0.9.13
func WithNS(name, url string) func(c *ContextSettings)
WithNS binds a namespace name to a XPath query.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <root xmlns="http://some.namespace.com"> <a xmlns="http://some.namespace.com">This is an XML node with a namespace prefix.</a> </root> ` xpath := xsel.MustBuildExpr(`/ns:root/ns:a`) cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) result, _ := xsel.Exec(cursor, &xpath, xsel.WithNS("ns", "http://some.namespace.com")) fmt.Println(result) }
Output: This is an XML node with a namespace prefix.
func WithVariable ¶ added in v0.9.13
func WithVariable(local string, value Result) func(c *ContextSettings)
WithVariable binds a variable name with no namespace to a XPath query.
func WithVariableNS ¶ added in v0.9.13
func WithVariableNS(space, local string, value Result) func(c *ContextSettings)
WithVariableNS binds a variable name with a namespace to a XPath query.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <root> <node>2.50</node> <node>3.14</node> <node>0.30</node> </root> ` const NS = "http://some.namespace.com" xpath := xsel.MustBuildExpr(`//node()[. = $ns:mynum]`) cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) result, _ := xsel.Exec(cursor, &xpath, xsel.WithNS("ns", NS), xsel.WithVariableNS(NS, "mynum", xsel.Number(3.14))) fmt.Println(result) }
Output: 3.14
func WithVariableName ¶ added in v0.9.13
func WithVariableName(name XmlName, value Result) func(c *ContextSettings)
WithVariableName binds a variable name with a namespace to a XPath query.
Types ¶
type ContextApply ¶ added in v0.9.13
type ContextApply = exec.ContextApply
type ContextSettings ¶ added in v0.9.13
type ContextSettings = exec.ContextSettings
type Cursor ¶
func ReadHtml ¶ added in v0.9.13
ReadHtml parses the given HTML document and stores the node in memory.
func ReadJson ¶ added in v0.9.13
ReadJson parses the given JSON document and stores the node in memory.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { json := ` { "states": ["AK", ["MD", "FL"] ] } ` xpath := xsel.MustBuildExpr(`/#obj/states/#arr/text()`) cursor, _ := xsel.ReadJson(bytes.NewBufferString(json)) result, _ := xsel.Exec(cursor, &xpath) fmt.Println(result) xpath = xsel.MustBuildExpr(`/#obj/states/#arr/#arr/text()[2]`) result, _ = xsel.Exec(cursor, &xpath) fmt.Println(result) }
Output: AK FL
type Grammar ¶ added in v0.9.13
func MustBuildExpr ¶ added in v0.9.13
MustBuildExpr is like BuildExpr, but panics if an error is thrown.
type NodeSet ¶
func ExecAsNodeset ¶ added in v0.9.16
func ExecAsNodeset(cursor Cursor, expr *Grammar, settings ...ContextApply) (NodeSet, error)
Like Exec, except it returns the query as a NodeSet.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <root> <a>This is the first node.</a> <a>This is the second node.</a> </root> ` xpath := xsel.MustBuildExpr(`/root/a`) cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) result, _ := xsel.ExecAsNodeset(cursor, &xpath) for _, i := range result { fmt.Println(xsel.GetCursorString(i)) } }
Output: This is the first node. This is the second node.
Example (Subqueries) ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <root> <a><b>Some text inbetween b and c. <c>A descendant c element.</c></b></a> <a><d>A d element.</d><c>A c element.</c></a> </root> ` aQuery := xsel.MustBuildExpr(`/root/a`) rootCursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) aElements, _ := xsel.ExecAsNodeset(rootCursor, &aQuery) cQuery := xsel.MustBuildExpr(`.//c`) for i, a := range aElements { cElements, _ := xsel.ExecAsNodeset(a, &cQuery) fmt.Printf("%d: %s\n", i, cElements.String()) } }
Output: 0: A descendant c element. 1: A c element.
type Result ¶ added in v0.9.13
func Exec ¶ added in v0.9.13
func Exec(cursor Cursor, expr *Grammar, settings ...ContextApply) (Result, error)
Exec executes an XPath query against the given Cursor and returns the result.
Example ¶
package main import ( "bytes" "fmt" "github.com/ChrisTrenkamp/xsel" ) func main() { xml := ` <root> <a>This is an XML node.</a> </root> ` xpath := xsel.MustBuildExpr(`/root/a`) cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml)) result, _ := xsel.Exec(cursor, &xpath) fmt.Println(result) }
Output: This is an XML node.
type XmlParseOptions ¶ added in v0.9.13
type XmlParseOptions = parser.XmlParseOptions
Directories
¶
Path | Synopsis |
---|---|
lexer
Package lexer is generated by GoGLL.
|
Package lexer is generated by GoGLL. |
parser
Package parser is generated by gogll.
|
Package parser is generated by gogll. |
parser/bsr
Package bsr implements a Binary Subtree Representation set as defined in
|
Package bsr implements a Binary Subtree Representation set as defined in |
parser/slot
Package slot is generated by gogll.
|
Package slot is generated by gogll. |
parser/symbols
Package symbols is generated by gogll.
|
Package symbols is generated by gogll. |
sppf
Package sppf implements a Shared Packed Parse Forest as defined in:
|
Package sppf implements a Shared Packed Parse Forest as defined in: |
token
Package token is generated by GoGLL.
|
Package token is generated by GoGLL. |