|
From: Julius M. <ju...@mu...> - 2006-11-13 16:38:23
|
Hi all,
i would propose to add the attached function to xmlpp::Node.
This function tests whether the node or any subnode match with the =20
given xpath-expression.
( NodeSet find(const Glib::ustring& xpath); is not suitable for that =20
purpose, because some expressions don't return an NodeSet).
I would ask you to add this code snippet to the cvs-repository.
Best regards
/Julius
-----------------------------
libxml++/nodes/node.h:
-----------------------------
/** Test nodes with XPath expression
* @param xpath The XPath of the nodes.
* @return True if any node matches the XPath expression, false =20
otherwise.
*/
bool test(const Glib::ustring& xpath) const;
/** Test nodes with XPath expression
* @param xpath The XPath of the nodes.
* @param namespaces A map of namespace prefixes to namespace URIs =20=
to be used while finding.
* @return True if any node matches the XPath expression, false =20
otherwise.
*/
bool test(const Glib::ustring& xpath, const PrefixNsMap& =20
namespaces) const;
-----------------------------
libxml++/nodes/node.cc:
-----------------------------
static bool test_impl(xmlXPathContext* ctxt, const Glib::ustring& xpath)
{
bool ret;
xmlXPathObject* result =3D xmlXPathEval((const xmlChar*)xpath.c_str=20=
(), ctxt);
if(!result)
{
xmlXPathFreeContext(ctxt);
throw exception("Invalid XPath: " + xpath);
}
ret =3D xmlXPathCastToBoolean(result);
xmlXPathFreeObject(result);
xmlXPathFreeContext(ctxt);
return ret;
}
bool Node::test(const Glib::ustring& xpath) const
{
xmlXPathContext* ctxt =3D xmlXPathNewContext(impl_->doc);
ctxt->node =3D impl_;
return test_impl(ctxt, xpath);
}
bool Node::test(const Glib::ustring& xpath,
const PrefixNsMap& namespaces) const
{
xmlXPathContext* ctxt =3D xmlXPathNewContext(impl_->doc);
ctxt->node =3D impl_;
for (PrefixNsMap::const_iterator it=3Dnamespaces.begin();
it !=3D namespaces.end(); it++)
xmlXPathRegisterNs(ctxt,
reinterpret_cast<const xmlChar*>(it-=20
>first.c_str()),
reinterpret_cast<const xmlChar*>(it-=20
>second.c_str()));
return test_impl(ctxt, xpath);
}
-----------------------------
--=20
GRIN Verlag GbR
Dr. Julius Mittenzwei
Marienstr. 17
80331 M=FCnchen
http://www.grin.com/
E-Mail: jm...@gr...
Tel: +49-(0)89-550559-12
Fax: +49-(0)89-550559-10
|