cxpath

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2026 License: BSD-3-Clause Imports: 5 Imported by: 2

README

Go Reference

cxpath - a programmer's friendly interface to XPath

With cxpath you can access XML files via XPath 2.0 in a Go friendly matter.

<a:root xmlns:a="anamespace">
  <a:sub>text</a:sub>
</a:root>
package main

import (
	"fmt"
	"log"

	"github.com/speedata/cxpath"
)

func dothings() error {
	ctx, err := cxpath.NewFromFile("myfile.xml")
	if err != nil {
		return err
	}
	// for XPath queries
	ctx.SetNamespace("a", "anamespace")
	root := ctx.Root()
	// prints 'root'
	fmt.Println(root.Eval("local-name()"))
	// prints sub
	fmt.Println(root.Eval("local-name(a:sub)"))
	// prints anamespace
	fmt.Println(root.Eval("namespace-uri(a:sub)"))
	sub := root.Eval("a:sub")
	for cp := range sub.Each("string-to-codepoints(.)") {
		// prints 116, 101, 120, 116 - the codepoints for 'text'
		fmt.Println(cp)
	}
	return nil
}

func main() {
	if err := dothings(); err != nil {
		log.Fatal(err)
	}
}

Error handling

The constructors NewFromFile and NewFromReader return errors the usual Go way. For all other methods, errors are stored in the Error field of the returned Context so that method chaining stays clean:

root := ctx.Root()
result := root.Eval("some/xpath")
if result.Error != nil {
    log.Fatal(result.Error)
}
fmt.Println(result.String())

The same applies to the Each iterator. If the XPath expression is invalid, the iterator yields a single Context with the Error field set:

for item := range root.Each("some/xpath") {
    if item.Error != nil {
        log.Fatal(item.Error)
    }
    fmt.Println(item.Int())
}

The value accessors Int() and Bool() also store conversion errors in Context.Error rather than returning them directly.

Installation

go get github.com/speedata/cxpath

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	P     *goxpath.Parser
	Seq   goxpath.Sequence
	Error error
}

A Context stores the current state of the XPath parser.

func NewFromFile

func NewFromFile(filename string) (*Context, error)

NewFromFile returns a new context from a file name.

func NewFromReader

func NewFromReader(r io.Reader) (*Context, error)

NewFromReader returns a new context from a reader.

func (*Context) Bool added in v0.0.2

func (ctx *Context) Bool() bool

Bool returns the boolean value of the sequence.

func (*Context) Each

func (ctx *Context) Each(eval string) iter.Seq[*Context]

Each can be used as an iterator which returns a Context for each item in the resulting sequence.

func (*Context) Eval

func (ctx *Context) Eval(eval string) *Context

Eval executes the given XPath expression relative to the current context. It returns a new Context, so the old one is still available for further use.

func (*Context) Int

func (ctx *Context) Int() int

Int returns the number value as an integer

func (*Context) Root

func (ctx *Context) Root() *Context

Root returns the top most element of the XML file.

func (*Context) SetNamespace

func (ctx *Context) SetNamespace(prefix, uri string) *Context

SetNamespace sets a prefix/URI pair for XPath queries.

func (*Context) String

func (ctx *Context) String() string

String returns the string value of the current sequence.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL