Re: [libxml++] ignore empty text nodes



* Andrej van der Zee

I am a happy user of libxmlpp for some time now. One thing I could not
find out yet. How can I ignore empty text nodes?
[...]

Hi.

I have used the following XPath-based technique to remove such nodes in order to "normalize" XML-documents that are stored in pretty-printed format. The reason I had them stored pretty-printed, was to improve readability in a test suite. I would not use pretty-printing on XML that is going directly between machines.

void remove_whitespace_nodes(xmlpp::Document & doc)
{
xmlpp::NodeSet whitespace_nodes = doc.get_root_node()->find("//text()[normalize-space()='']");
    for (xmlpp::NodeSet::const_iterator it = whitespace_nodes.begin();
         it != whitespace_nodes.end();
         ++it)
    {
        (*it)->get_parent()->remove_child(*it);
    }
}

Make sure the XPath above does not identify nodes that carries meaning to your application, otherwise you would have to refine it somehow.

--
Sincerely,
Knut Aksel Røysland




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]