You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Ramon F Herrera <ra...@patriot.net> on 2012/09/11 23:49:54 UTC
What is a pre-compiled XPath expression and does Xerces support it???
I have an educated guess about the nature of a pre-compiled XPath
expression, but would like to know more details.
I am facing a serious performance problem.
TIA,
-Ramon
Re: How to maintain implicit namespaces across importNode() over WSDL/XSD
Posted by Ben Griffin <be...@redsnapper.net>.
> The schema_document is then installed as a grammar using DOMLSParser* parser->loadGrammar().
> The PROBLEM is that the "s3:ErrorInfo" value loses the scope of the namespace prefix found in the original WSDL document.
> What is the solution to this? Should I itererate through the full list of xmlns:* attributes of the WSDL and insert them into the root element of the new schema_doc? Or am I missing something?
This is what I have done so far - which is indeed iterate through all document element attributes, looking for a match with xmlns:
Is there a more efficient way of doing this? Am I missing something?
DOMElement* schema_node; //points to the schema element in the WSDL document..
DOMDocument* schema_doc = impl->createDocument();
DOMNode* imported_node = schema_doc->importNode(schema_node,true);
schema_doc->appendChild(imported_node);
[...]
const DOMDocument* schema_node_doc = schema_node->getOwnerDocument();
if ( schema_node_doc != NULL) {
const DOMElement* wsdl_de = schema_node_doc->getDocumentElement();
if (wsdl_de != NULL) {
const DOMNamedNodeMap* wsdl_de_attr= wsdl_de->getAttributes();
DOMElement* schema_de = schema_doc->getDocumentElement();
if (schema_de != NULL && wsdl_de_attr != NULL ) {
size_t n = wsdl_de_attr->getLength();
for(size_t i=0; i<n; i++) {
DOMAttr* ai = (DOMAttr*)(wsdl_de_attr->item(i));
const u_str ain(ai->getName()); //u_str = std::basic_string<XMLCh>
if (ain.compare(0,6,X("xmlns:")) == 0) {
DOMAttr* iattr = (DOMAttr*)(schema_doc->importNode(ai,true));
DOMAttr* oattr = schema_de->setAttributeNode(iattr);
if ( oattr!= NULL ) { delete oattr; } //This attribute was already there.
}
}
}
}
}
How to maintain implicit namespaces across importNode() over WSDL/XSD
Posted by Ben Griffin <be...@redsnapper.net>.
I am currently using XercesC to extract and recompose the grammars embedded within an WSDL document.
At the DocumentElement level of the WSDL document (the wsdl:definitions element) there are (as is normal) all the namespace declarations used for the document. So, for instance,
xmlns:s3="http://foobar.com/mySOAP/commons"
As you probably know, typically, a sequence of schemas are defined under the "wsdl:types" element.
I am extracting these, so that I can subsequently load them as grammars and then validate / process the instance documents that they define.
For instance:
[...]
<s:schema elementFormDefault="qualified" targetNamespace="http://foobar.com/mySOAP/Responder">
<s:import namespace="http://foobar.com/mySOAP/commons"/>
<s:complexType abstract="true" name="Responder">
<s:complexContent mixed="false">
<s:extension base="s3:ErrorInfo">
<s:sequence>
<s:element maxOccurs="1" minOccurs="1" name="responsetime" type="s:double"/>
</s:sequence>
</s:extension>
</s:complexContent>
</s:complexType>
</s:schema>
[...]
So - during the process of extracting the schemas from the WSDL document, I am using importNode() as follows:
DOMElement* schema_node; //points to the schema element in the WSDL document..
DOMDocument* schema_doc = impl->createDocument();
DOMNode* imported_node = schema_doc->importNode(schema_node,true);
schema_doc->appendChild(imported_node);
The schema_document is then installed as a grammar using DOMLSParser* parser->loadGrammar().
The PROBLEM is that the "s3:ErrorInfo" value loses the scope of the namespace prefix found in the original WSDL document.
What is the solution to this? Should I itererate through the full list of xmlns:* attributes of the WSDL and insert them into the root element of the new schema_doc? Or am I missing something?
Re: What is a pre-compiled XPath expression and does Xerces support it???
Posted by Ben Griffin <be...@redsnapper.net>.
I'm not sure if you are using DOM - but if you are, I recently wrote a small example/test that includes pre-compile XPaths.
This depends heavily on XQilla - which I am sure most XercesC developers/users recommend if you are planning on using XPaths.
I am using the XQilla "SimpleAPI" - which allows for more optimal approaches to applications that heavily depend upon XPaths.
Anyway, I include the code - hoping it may help you with this sort of stuff.
#include <iostream>
#include <fstream>
#include <xqilla/xqilla-dom3.hpp>
#include <xqilla/xqilla-simple.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XercesDefs.hpp>
#include <xercesc/dom/DOM.hpp>
/*
File foo.xml is as follows.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE div><div xmlns="http://www.w3.org/1999/xhtml">
<h5>Foo</h5><div><span class="phoo">wibble</span></div>
</div>
File bar.xml is as follows.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE div><div xmlns="http://www.w3.org/1999/xhtml">
<h5>Bar</h5><div><span class="bah">wobble</span></div>
</div>
*/
using namespace xercesc;
using namespace std;
class errhandler : public DOMErrorHandler {
public:
bool handleError(const DOMError& domError) {
cout << UTF8(domError.getMessage()); return true;
}
};
errhandler* err = NULL;
//dom
DOMImplementation* impl = NULL;
DOMLSParser* parser = NULL;
DOMDocument* d1 = NULL;
DOMDocument* d2 = NULL;
//xqilla
DynamicContext* central = NULL;
XQilla* xqilla = NULL;
XercesConfiguration* xc = NULL;
XQQuery* q1 = NULL;
XQQuery* q2 = NULL;
DOMDocument* DOMDocFromFile(const XMLCh* const uri) {
DOMDocument* rslt = NULL;
try {
rslt = parser->parseURI(uri);
} catch (...) {
std::cout << "DOM Parse error" << std::endl;
}
return rslt;
}
void doDocQuery(DOMDocument* d,XQQuery* q) {
DOMElement* el = d->getDocumentElement();
if (el != NULL) {
AutoDelete<DynamicContext> c(q->createDynamicContext());
c->setContextItem(((XercesConfiguration*)c->getConfiguration())->createNode(el,c));
Result result = q->execute(c);
Item::Ptr item;
while((item = result->next(c))) {
std::cout << UTF8(item->asString(c)) << ";";
}
}
}
int main(int argc, char *argv[]) {
err = new errhandler();
XQillaPlatformUtils::initialize();
xqilla = new XQilla();
xc = new XercesConfiguration();
impl = DOMImplementationRegistry::getDOMImplementation(X("XPath2 3.0"));
parser = ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS,NULL);
DOMConfiguration* dc = parser->getDomConfig();
dc->setParameter(XMLUni::fgDOMErrorHandler,err);
dc->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument,true);
central = xqilla->createContext(XQilla::XPATH3,xc);
central->setNamespaceBinding(X("h"),X("http://www.w3.org/1999/xhtml"));
q1 = xqilla->parse(X("//h:span[@class]/@class"),central,NULL,0x12);
q2 = xqilla->parse(X("//h:h5/text()"),central,NULL,0x12);
//initialise process is done
for (int i = 1; i <= 100; i++) {
cout << "iteration " << i << ":";
d1 = DOMDocFromFile(X("foo.xml"));
d2 = DOMDocFromFile(X("bar.xml"));
doDocQuery(d1,q1);
doDocQuery(d1,q2);
doDocQuery(d2,q1);
doDocQuery(d2,q2);
d1->release();
d2->release();
cout << endl;
}
//now teardown variables
delete q1;
delete q2;
delete central;
parser->release();
delete xc;
delete xqilla;
delete err;
return 0;
}
Re: What is a pre-compiled XPath expression and does Xerces support
it???
Posted by Alberto Massari <Al...@progress.com>.
Hi Ramon,
a pre-compiled XPath expression is a way to convert the query from a
string into an object that is able to run the query, and it is useful
when the same query is applied several times against different target nodes.
In Xerces you can create one by using DOMDocument::createExpression, and
you can execute it by invoking evaluate() on the returned object.
In any case, if you are experiencing performance problems, you should be
aware that the XPath engine in Xerces is a very simple one that doesn't
perform any optimization.
Alberto
Il 11/09/2012 23:49, Ramon F Herrera ha scritto:
> I have an educated guess about the nature of a pre-compiled XPath
> expression, but would like to know more details.
>
> I am facing a serious performance problem.
>
> TIA,
>
> -Ramon
>
>
>