XPath support in Xerces-C
Asked Answered
C

3

13

I am supporting a legacy C++ application which uses Xerces-C for XML parsing. I've been spoiled by .Net and am used to using XPath to select nodes from a DOM tree.

Is there any way to get access some limited XPath functionality in Xerces-C? I'm looking for something like selectNodes("/for/bar/baz"). I could do this manually, but XPath is so nice by comparison.

Catherine answered 9/7, 2009 at 19:46 Comment(0)
P
7

See the xerces faq.

http://xerces.apache.org/xerces-c/faq-other-2.html#faq-9

Does Xerces-C++ support XPath? No.Xerces-C++ 2.8.0 and Xerces-C++ 3.0.1 only have partial XPath implementation for the purposes of handling Schema identity constraints. For full XPath support, you can refer Apache Xalan C++ or other Open Source Projects like Pathan.

It's fairly easy to do what you want using xalan however.

Phenobarbital answered 9/7, 2009 at 23:59 Comment(0)
S
7

Here is a working example of XPath evaluation with Xerces 3.1.2.

Sample XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <ApplicationSettings>hello world</ApplicationSettings>
</root>

C++

#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/util/TransService.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>

using namespace xercesc;
using namespace std;

int main()
{
  XMLPlatformUtils::Initialize();
  // create the DOM parser
  XercesDOMParser *parser = new XercesDOMParser;
  parser->setValidationScheme(XercesDOMParser::Val_Never);
  parser->parse("sample.xml");
  // get the DOM representation
  DOMDocument *doc = parser->getDocument();
  // get the root element
  DOMElement* root = doc->getDocumentElement();

  // evaluate the xpath
  DOMXPathResult* result=doc->evaluate(
      XMLString::transcode("/root/ApplicationSettings"),
      root,
      NULL,
      DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
      NULL);

  if (result->getNodeValue() == NULL)
  {
    cout << "There is no result for the provided XPath " << endl;
  }
  else
  {
    cout<<TranscodeToStr(result->getNodeValue()->getFirstChild()->getNodeValue(),"ascii").str()<<endl;
  }

  XMLPlatformUtils::Terminate();
  return 0;
}

Compile and run (assumes standard xerces library installation and C++ file named xpath.cpp)

g++ -g -Wall -pedantic -L/opt/lib -I/opt/include -DMAIN_TEST xpath.cpp -o xpath -lxerces-c
./xpath

Result

hello world
Spunk answered 14/10, 2015 at 18:20 Comment(1)
Try changing the query to something like: count(//somenode/subnode/[@someattribute='somevalue']) and it throws an DOMXPathExceptionHooghly
T
2

According to the FAQ, Xerces-C supports partial XPath 1 implementation:

The same engine is made available through the DOMDocument::evaluate API to let the user perform simple XPath queries involving DOMElement nodes only, with no predicate testing and allowing the "//" operator only as the initial step.

You use DOMDocument::evaluate() to evaluate the expression, which then returns a DOMXPathResult.

Tarlatan answered 9/7, 2009 at 19:59 Comment(2)
Has anyone used this functionality? Has it worked? If so, for what versions of Xerces-C?Catherine
@AdamTegen Yes, I know it's 6 years later but Xerces is still popular. I provided an example of how to accomplish XPath evaluation with Xerces 3.1.2.Spunk

© 2022 - 2024 — McMap. All rights reserved.