SSD
SSD

Reputation: 41

Xercesc XPath functionalities

I have tired DOMDocument::evaluate in xercesc 3.1.1 to select nodes from a DOM tree. It works for some of the xpath expression. But to select nodes by attribute value like "//Project[@index=\"1\"]" is not supported. Can anyone confirm this?

Thanks!

Upvotes: 2

Views: 2410

Answers (2)

ViRuSTriNiTy
ViRuSTriNiTy

Reputation: 5155

Yes i can confirm that this kind of xpath expression is not supported in xerces 3.1.1.

As an example, lets say your XML looks like...

<Root><Item>ABCD</Item><Item>EFGH</Item></Root>

...then the following code prints out the value of the Item nodes:

DOMElement * lXMLDocumentElement(lXMLDocument->getDocumentElement());
if ( lXMLDocumentElement )
{
  try
  {
    DOMXPathResult * r(
      lXMLDocument->evaluate(L"Item", lXMLDocumentElement, NULL, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL));

    if ( r )
    {
      for ( unsigned c(0); c < r->getSnapshotLength(); ++c )
      {
        r->snapshotItem(c);
        DOMNode * n(r->getNodeValue());

        if ( n->getNodeType() == DOMNode::ELEMENT_NODE )
        {
          DOMElement * e(static_cast<DOMElement *>(n));

          std::wcout << e->getTextContent() << std::endl;
        }
      }
    }
  }
  catch ( const DOMXPathException & e )
  {
    // handle exception
  }
}

however, when the XML looks like

<Root><Project index="1">ABCD</Project><Project>EFGH</Project></Root>

and the xpath expression

//Project[@index="1"]

is used, an exception with code INVALID_EXPRESSION_ERR is thrown, thus the expression is not supported.

Its also worth mentioning that the evaluate() method only supports the following values for parameter type (see DOMXPathResultImpl.cpp)

ANY_UNORDERED_NODE_TYPE
FIRST_ORDERED_NODE_TYPE
UNORDERED_NODE_SNAPSHOT_TYPE
ORDERED_NODE_SNAPSHOT_TYPE

Upvotes: 2

LarsH
LarsH

Reputation: 27994

When I look at the DOMDocument class docs, I don't see an evaluate() method. Do you mean DOMXPathEvaluator::evaluate?

In general, DOMXPathEvaluator::evaluate() is supposed to support XPath (presumably at least 1.0), so selecting nodes by attribute value should not be a problem.

When you say "is not supported", do you mean that you tried an XPath expression like "//Project[@index=\"1\"]" and it didn't work? If so, what did your code look like, and what was the result?

What ResultType did you ask for? How did you use the returned results? (Sometimes the correct results are returned, but they aren't accessed correctly.)

Upvotes: 0

Related Questions