Whenever I have a default namespace in my xml file, xpath expression is
not returning any nodeset.
I have my xml file as follows.
Sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="HelpRequestMsg">
<part name="body" element="Help"/>
</message>
<message name="HelpResponseMsg">
<part name="body" element="HelpResponse"/>
</message>
<message name="ItemSearchRequestMsg">
<part name="body" element="ItemSearch"/>
</message>
</definitions>
I am trying to use xpath expression as "//message". My code is as
follows. I am using xerces as well as xalan in my application.
XMLPlatformUtils::Initialize();
XPathEvaluator::initialize();
XercesDOMParser *parser = new XercesDOMParser;
parser->setDoNamespaces(true);
parser->setDoSchema(true);
parser->setValidationSchemaFullChecking(true);
parser->setCreateEntityReferenceNodes(true);
XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
parser->parse(m_xmlfile);
doc = parser->getDocument();
std::string xpath = "//message"; //xpath expression
XercesDOMSupport dom_support;
XercesParserLiaison parser_liaison;
XalanDocument *xalan_document = parser_liaison.createDocument(doc,
false, true);
XalanNode * root_context_node = xalan_document;
XalanElement *namespace_node = xalan_document->getDocumentElement();
const XalanDOMString expression(xpath.c_str());
XPathEvaluator evaluator;
NodeRefList nodelist;
evaluator.selectNodeList( nodelist,
dom_support,
root_context_node,
expression.c_str(),
namespace_node);
NodeRefList::size_type length = nodelist.getLength();
Since I have default namespace xmlns=http://schemas.xmlsoap.org/wsdl/ in
my input xml file I'm getting length as zero.
If I remove the default namespace them I am getting length as 3 which is
correct for my xpath expression.
Please help me how xpath expression needs to be modified whenever we
have default namespace.
Any help regarding xpath expressions whenever there are namespaces in
the xml file are appreciated.
Thanks & regards,
Jaya Nageswar.