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!
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.
import (
"github.com/speedata/risorxpath"
)
...
result, err := risor.Eval(ctx,
string(data),
risor.WithLocalImporter(wd),
risor.WithGlobals(map[string]any{
"xpath": risorxpath.Module(),
}))
...
Contact: [email protected]