I am parsing a xml document using gopkg.in/xmlpath.v2, and I am finding a trouble... I have no problem to get info from a single node, or get a iterator and loop over its items getting their info. But, the case where I am blocked is when I try to get the info from the same node on which I am iterating. I think that an example will be illuminating.
This is the XML:
<Warnings>
<Warning Type="309" ShortText="Unfulfilled Paid Service 1">Unable to book seat 1</Warning>
<Warning Type="309" ShortText="Unfulfilled Paid Service 2">Unable to book seat 2</Warning>
<Warning Type="309" ShortText="Unfulfilled Paid Service 3">Unable to book seat 3</Warning>
</Warnings>
These are the xpath that I am usin:
xpath := xPathWarning{
WarningsBase: "Warnings/Warning",
Warning: "",
WarningAttr: "@ShortText",
}
And this is the way that I am trying to get the value and attribute:
func getWarnings(root *xmlpath.Node, xpath xpath_OC) []Warning {
warnings := []Warning{}
v, _ := xmlpath.Compile(xpath.WarningsBase)
WarningsBaseIter := v.Iter(root)
for WarningsBaseIter.Next() {
rawOffer := WarningsBaseIter.Node()
warning := Warning{
Value: GetString("", xpath.Warning, rawOffer),
Attr: GetString("", xpath.WarningAttr, rawOffer),
}
warnings = append(warnings, warning)
}
return warnings
}
func GetString(ExpressionPrefix string, XPathExpression string, Node *xmlpath.Node) string {
Expr := []string{ExpressionPrefix, XPathExpression}
pathString := strings.Join(Expr, "")
if pathString != "BLANK" {
Path, err := xmlpath.Compile(pathString)
if err == nil {
Value, _ := Path.String(Node)
return Value
}
}
return ""
}
I am able to get the ShortText, but not the value. I had been checking the error, showing on terminal the result of Path, err := xmlpath.Compile(pathString)
, and the err shown is compiling xml path "":0: empty path
.
Any solution for this??
Thanks.