First, you should to check that the returned object is a NodeSet by examining object->type == Xml.ObjectType.NODESET
.
Next, you are only returning the first item in the resulting nodeset. There are nodeset->length()
items.
void main() {
var document = Xml.Parser.parse_file ("file.xml");
run_query("//album", document);
run_query("/library/artist[@name=\"b\"]/album", document);
}
void run_query(string query, Xml.Doc* document) {
stdout.printf("Query: %s\n", query);
Xml.XPath.Context context;
unowned Xml.XPath.Object *object;
context = new Xml.XPath.Context(document);
object = context.eval(query);
assert(object->type == Xml.XPath.ObjectType.NODESET);
if(object->nodesetval->is_empty()) {
stdout.printf("empty");
return;
}
for (var i = 0; i < object->nodesetval->length(); i++) {
var node = object->nodesetval->item(i);
stdout.printf("node %s name=\"%s\"\n", node->name, node->get_prop("name"));
}
}
Produces:
Query: //album
node album name="1"
node album name="2"
node album name="8"
node album name="9"
Query: /library/artist[@name="b"]/album
node album name="8"
node album name="9"