xpath: github.com/antchfx/xpath Index | Examples | Files

package xpath

import "github.com/antchfx/xpath"

XPath package example.

Code:

expr, err := xpath.Compile("count(//book)")
if err != nil {
    panic(err)
}
var root xpath.NodeNavigator
// using Evaluate() method
val := expr.Evaluate(root) // it returns float64 type
fmt.Println(val.(float64))

// using Evaluate() method
expr = xpath.MustCompile("//book")
val = expr.Evaluate(root) // it returns NodeIterator type.
iter := val.(*xpath.NodeIterator)
for iter.MoveNext() {
    fmt.Println(iter.Current().Value())
}

// using Select() method
iter = expr.Select(root) // it always returns NodeIterator object.
for iter.MoveNext() {
    fmt.Println(iter.Current().Value())
}

Index

Examples

Package Files

build.go func.go func_go110.go operator.go parse.go query.go xpath.go

type Expr Uses

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

Expr is an XPath expression for query.

func Compile Uses

func Compile(expr string) (*Expr, error)

Compile compiles an XPath expression string.

func MustCompile Uses

func MustCompile(expr string) *Expr

MustCompile compiles an XPath expression string and ignored error.

func (*Expr) Evaluate Uses

func (expr *Expr) Evaluate(root NodeNavigator) interface{}

Evaluate returns the result of the expression. The result type of the expression is one of the follow: bool,float64,string,NodeIterator).

func (*Expr) Select Uses

func (expr *Expr) Select(root NodeNavigator) *NodeIterator

Select selects a node set using the specified XPath expression.

func (*Expr) String Uses

func (expr *Expr) String() string

String returns XPath expression string.

type NodeIterator Uses

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

NodeIterator holds all matched Node object.

func Select Uses

func Select(root NodeNavigator, expr string) *NodeIterator

Select selects a node set using the specified XPath expression. This method is deprecated, recommend using Expr.Select() method instead.

func (*NodeIterator) Current Uses

func (t *NodeIterator) Current() NodeNavigator

Current returns current node which matched.

func (*NodeIterator) MoveNext Uses

func (t *NodeIterator) MoveNext() bool

MoveNext moves Navigator to the next match node.

type NodeNavigator Uses

type NodeNavigator interface {
    // NodeType returns the XPathNodeType of the current node.
    NodeType() NodeType

    // LocalName gets the Name of the current node.
    LocalName() string

    // Prefix returns namespace prefix associated with the current node.
    Prefix() string

    // Value gets the value of current node.
    Value() string

    // Copy does a deep copy of the NodeNavigator and all its components.
    Copy() NodeNavigator

    // MoveToRoot moves the NodeNavigator to the root node of the current node.
    MoveToRoot()

    // MoveToParent moves the NodeNavigator to the parent node of the current node.
    MoveToParent() bool

    // MoveToNextAttribute moves the NodeNavigator to the next attribute on current node.
    MoveToNextAttribute() bool

    // MoveToChild moves the NodeNavigator to the first child node of the current node.
    MoveToChild() bool

    // MoveToFirst moves the NodeNavigator to the first sibling node of the current node.
    MoveToFirst() bool

    // MoveToNext moves the NodeNavigator to the next sibling node of the current node.
    MoveToNext() bool

    // MoveToPrevious moves the NodeNavigator to the previous sibling node of the current node.
    MoveToPrevious() bool

    // MoveTo moves the NodeNavigator to the same position as the specified NodeNavigator.
    MoveTo(NodeNavigator) bool
}

NodeNavigator provides cursor model for navigating XML data.

type NodeType Uses

type NodeType int

NodeType represents a type of XPath node.

const (
    // RootNode is a root node of the XML document or node tree.
    RootNode NodeType = iota

    // ElementNode is an element, such as <element>.
    ElementNode

    // AttributeNode is an attribute, such as id='123'.
    AttributeNode

    // TextNode is the text content of a node.
    TextNode

    // CommentNode is a comment node, such as <!-- my comment -->
    CommentNode
)

Package xpath imports 10 packages (graph) and is imported by 20 packages. Updated 2019-08-02. Refresh now. Tools for package owners.