
Jose wrote:
On Sun, Mar 21, 2010 at 12:00 AM, Phil Endecott <spam_from_boost_dev@chezphil.org> wrote:
In fact, something "XPath-like" but also more "C++-like" would be the next step to improve the "user" code in my application. ?Currently I have too much verbose iteration looking for the elements that I want. ?It would be great to have a XPath-like DSL for finding these elements. ?(An application for Proto?)
Have you tried pugixml?
Thanks; I have seen it, but I hadn't noticed that it has an XPath implementation. (In other respects, it is similar in concept to RapidXML i.e. it needs O(N) memory.) From its examples: // You can use a sometimes convenient path function cout << doc.first_element_by_path("bookstore/book/price").child_value() << endl; // And you can use powerful XPath expressions cout << doc.select_single_node("/bookstore/book[@title = 'ShaderX']/price").node().child_value() << endl; // Compile query that prints total price of all Gems book in store xpath_query query("sum(/bookstore/book[contains(@title, 'Gems')]/price)"); cout << query.evaluate_number(doc) << endl; I'm not convinced that a full XPath implementation is really useful (e.g. that final example using the XPath sum() function and the contains() test; I think it would be better to do those in C++). But some subset, implemented as a DSL, would be good: element e = doc.first_element_matching("bookstore" / "book" / "price"); That could expand to something that assumed doc modelled an 'XML container' concept, as in: element_iterator i = find(doc.child_elements().begin(),doc.child_elements().end(),"bookstore"); if (i==doc.end()) throw NotFound(); element_iterator j = find(i->child_elements().begin(),i->child_elements().end(),"book"); if (j==i->end()) throw NotFound(); element_iterator k = find(j->child_elements().begin(),j->echild_elements().nd(),"price"); if (k==j->end()) throw NotFound(); element e = *k; I guess that if there's something I want to propose, it's that we consider what this XML container concept should look like. In particular, I think that DOM is not what we want because it's too dissimilar to other C++ concepts (i.e. iterators). (An adaptor for DOM would be possible, however.) Regards, Phil.