xerces-c-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Boris Kolpackov <bo...@codesynthesis.com>
Subject Re: XPath from DOMNode ?
Date Thu, 03 Sep 2009 07:33:01 GMT
Hi Ben,

Ben Griffin <ben@redsnapper.net> writes:

> Given a specific DOMNode, is there a method for finding (or deriving) a 
> valid XPath that uniquely identifies that DOMNode?

You could go up the document tree (by calling getParentNode) and construct
the XPath path by prefixing the element names, something along these
lines (pseudo-code):

string xpath (DOMElement* n)
{
  if (n == 0)
    return "/";
  else
    return xpath (n->getParentNode ()) + "/" + n->getName ();
}

This will work for documents that don't contain sequences of elements
on the same lavel. If you have to handle this situation then you will
need to extend this logic to also include the element indexes, e.g.,
//foo/bar[2].

Boris

-- 
Boris Kolpackov, Code Synthesis Tools  http://codesynthesis.com/~boris/blog
Open-source XML data binding for C++:  http://codesynthesis.com/products/xsd
XML data binding for embedded systems: http://codesynthesis.com/products/xsde

Mime
View raw message