Skip to content

speedata/risorxpath

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XPath module for risor

Assume you have this XML file:

<!-- a comment -->
<data type="greeting">
    <p>hello world!</p>
    <p>hello XML!</p>
</data>

Then you can use risorxpath to query this document. First load the XML file:

import xpath

xp := xpath.parse("data.xml")

or

import xpath

file := open("data.xml")
xp := xpath.parse(file)

Then you can use xp.evaluate() to query the document:

sequence := xp.evaluate("/data/p")
for i, itm := range sequence {
    print(i,itm)
}

prints

0 hello world!
1 hello XML!

API

xpath

  • sequence = xpath.evaluate(string) evaluates the XPath query with the current context.
  • ctx returns the current context.
  • set_variable(string, sequence) sets a variable to the given sequence.
  • xml_document returns the XML document of the current context.

Including in your Go program

import (
    "github.com/speedata/risorxpath"
)

...

result, err := risor.Eval(ctx,
		string(data),
		risor.WithLocalImporter(wd),
		risor.WithGlobals(map[string]any{
			"xpath": risorxpath.Module(),
		}))
...

Other

Contact: [email protected]

About

XPath module for the risor language

Topics

Resources

Stars

Watchers

Forks

Sponsor this project

 

Languages