How do I parse xml with a namespace?
Opened this issue · 2 comments
Read through everything but still not understanding how to parse xml with a namespace. var b parses correctly but I can't figure out how to parse var a below:
package main
import (
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/xpath"
"log"
)
func main() {
log.SetFlags(log.Lshortfile)
doc, _ := gokogiri.ParseXml([]byte(a))
defer doc.Free()
doc.SetNamespace("", "http://example.com/this")
x := xpath.Compile(".//NodeA/NodeB")
groups, err := doc.Search(x)
log.Println(groups)
if err != nil {
log.Println(err)
}
for i, group := range groups {
log.Println(i, group)
}
}
var a = <?xml version="1.0" ?><NodeA xmlns="http://example.com/this"><NodeB>thisthat</NodeB></NodeA>
var b = <?xml version="1.0" ?><NodeA><NodeB>thisthat</NodeB></NodeA>
Try using doc.EvalXPath(x, nil)
instead of Search. The SetNamespace
call is for when you're building documents, not for querying. I think but am not completely sure that EvalXPath automatically registers any namespaces that occur in the document.
Alternatively you can get the XPath context directly (via doc.DocXPathCtx
) and call RegisterNamespace
followed by EvaluateAsNodeset
on the context object ( this is what EvalXPath does in the background, see https://github.com/moovweb/gokogiri/blob/master/xml/node.go#L650 )
I apologize for the poor state of the documentation around namespaces; for personal reasons I've been unable to spend much time on my contributions since February and one of my big outstanding patches is a proper user's guide.
Still not documented, even after moving across to the jbowtie fork.