[libxml++] proposed function to xmlpp::Node



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 given xpath-expression. ( NodeSet find(const Glib::ustring& xpath); is not suitable for that 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 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 to be used while finding. * @return True if any node matches the XPath expression, false otherwise.
   */
bool test(const Glib::ustring& xpath, const PrefixNsMap& namespaces) const;

-----------------------------
libxml++/nodes/node.cc:
-----------------------------

static bool test_impl(xmlXPathContext* ctxt, const Glib::ustring& xpath)
{
  bool ret;
xmlXPathObject* result = xmlXPathEval((const xmlChar*)xpath.c_str (), ctxt);

  if(!result)
  {
    xmlXPathFreeContext(ctxt);
    throw exception("Invalid XPath: " + xpath);
  }

  ret = xmlXPathCastToBoolean(result);

  xmlXPathFreeObject(result);
  xmlXPathFreeContext(ctxt);

  return ret;
}

bool Node::test(const Glib::ustring& xpath) const
{
  xmlXPathContext* ctxt = xmlXPathNewContext(impl_->doc);
  ctxt->node = impl_;

  return test_impl(ctxt, xpath);
}

bool Node::test(const Glib::ustring& xpath,
                   const PrefixNsMap& namespaces) const
{
  xmlXPathContext* ctxt = xmlXPathNewContext(impl_->doc);
  ctxt->node = impl_;

  for (PrefixNsMap::const_iterator it=namespaces.begin();
       it != namespaces.end(); it++)
    xmlXPathRegisterNs(ctxt,
reinterpret_cast<const xmlChar*>(it- >first.c_str()), reinterpret_cast<const xmlChar*>(it- >second.c_str()));

  return test_impl(ctxt, xpath);
}

-----------------------------



--
GRIN Verlag GbR
Dr. Julius Mittenzwei
Marienstr. 17
80331 München

http://www.grin.com/
E-Mail: jm grin com
Tel: +49-(0)89-550559-12
Fax: +49-(0)89-550559-10







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