Search code examples
xpathlibxml2vala

Removing xml node from document


I am trying to find certain nodes using xpath in an html-document. And then remove them. In this example I am looking for a node with the id "atoc_next". The correct node is found and I unlink it. Then I search for a node "article" and write it to a file. However the node that was supposed to be unlinked earlier, which was a child of the article-node, is still present. Any idea whats wrong? Btw: if I delete or free_list() the node after unlinking, the code sefaults =/

var html_cntx = new Html.ParserCtxt();
html_cntx.use_options(Html.ParserOption.NOWARNING);
html_cntx.use_options(Html.ParserOption.NOERROR);
var doc = html_cntx.read_file("document.txt");

Xml.XPath.Context cntx = new Xml.XPath.Context(doc);
Xml.XPath.Object* res = cntx.eval_expression("//a[@id='atoc_next']");

assert (res != null);
assert (res->type == Xml.XPath.ObjectType.NODESET);
assert (res->nodesetval != null);

for(int i = 0; i < res->nodesetval->length(); i++)
{
    Xml.Node* node = res->nodesetval->item(i);
    node->unlink;
}

delete res;
res = cntx.eval_expression("//article");
assert (res != null);
assert (res->type == Xml.XPath.ObjectType.NODESET);
assert (res->nodesetval != null);

FileStream stream = FileStream.open("article.html", "w");
assert (stream != null);

for(int i = 0; i < res->nodesetval->length(); i++)
{
    Xml.Node* node = res->nodesetval->item(i);
    doc->node_dump_file(stream, node);
}

thx for any advice in advance :)


Solution

  • If you do node->unlink;, this has no effect. It simply gets a function pointer to the unlink function, then discards it. Do node->unlink(); instead.