2

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!

SSD
  • 41
  • 4

2 Answers2

2

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
SeniorJD
  • 6,946
  • 4
  • 36
  • 53
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
  • 1
    Yes! And I got the same error in xerces-c 3.2.3. The xercesc::DOMException::getMessage() got a message "expression has incorrect syntax or contains XPath features not supported by the XML Schema XPath subset". I want this to be fixed! xerces-c does not support XPath 1.0 and lies! – Harutaka Matsumoto Jun 21 '22 at 08:35
0

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.)

LarsH
  • 27,481
  • 8
  • 94
  • 152
  • 1
    DOMXPathEvaluator is an interface. DOMDocument inherits that interface. http://xerces.apache.org/xerces-c/apiDocs-3/classDOMDocument.html evaluate is implemented in DOMDocumentImpl. Yes, I tried that expression. – SSD Apr 02 '12 at 13:32
  • DOMXPathEvaluator is an interface. DOMDocument inherits that interface. evaluate is implemented in DOMDocumentImpl. Yes, I tried that expression. DOMNode* node = parser->getDocument(); DOMDocument* document = dynamic_cast(node); DOMElement* root = document->getDocumentElement(); try { DOMXPathNSResolver* resolver=document->createNSResolver(root); DOMXPathResult* result=document->evaluate( xpathStr, root, resolver, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL); The exception was triggered for that expression. – SSD Apr 02 '12 at 13:41
  • @SSD: sorry for my confusion about the interface. Since DOMDocument is supposed to implement the evaluate() method of the DOMXPathEvaluator interface, I'm puzzled not to see evaluate() on the class doc for DOMDocument. And no mention of DOMDocumentImpl. I guess there are some differences between C# and Java here that I'm not familiar with. – LarsH Apr 02 '12 at 17:21
  • @SSD, regarding your code, please edit the question to post your code there (http://sssce.org). The formatting in a comment makes it really hard to read. Thanks. Also please tell us what exception occurred. – LarsH Apr 02 '12 at 17:23
  • I've got the same problem, and I've found that xerces is throwing a Token Not Found exception when it parses the '['. Have you got any news regarding this issue? – rturrado Jan 13 '14 at 13:33
  • @rturrado: Sorry, no news. I don't work in the C++ world, and I won't be investigating this further unless the OP provides the requested information. But you could post a new question. – LarsH Jan 13 '14 at 15:37