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 Ben Griffin <be...@redsnapper.net> on 2009/03/13 13:47:26 UTC
attribute 'xmlns' is not declared for element 'tx' - huh?
The following program outputs ( on cerr )
>DOM Error:no declaration found for element 'tx'
>DOM Error:attribute 'xmlns' is not declared for element 'tx'
I don't understand why.
What I would expect is for the namespace to be found, and then an
error regarding that.
Instead, there's a problem identifying that the xmlns declaration is a
document identifier.
What am I doing wrong?
-------------------------------------------------
#include <iostream>
#include <xercesc/util/XercesDefs.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationLS.hpp>
#include <xercesc/dom/DOMLSParser.hpp>
#include <xercesc/dom/DOMLSInput.hpp>
using namespace xercesc;
class Err: public DOMErrorHandler {
public:
bool Err::handleError(const xercesc::DOMError& domError) {
std::cerr << "DOM Error:" << (char *)
(XMLString::transcode(domError.getMessage())) << "\r";
return true;
}
};
int main (int argc, char * const argv[]) {
XMLPlatformUtils::Initialize();
const XMLCh gLS[] = { 'L', 'S', chNull };
// <tx xmlns="http://bgriffin.net/tx" /> but with no transocoder.
const XMLCh file[] = {'<','t','x','
','x
','m
','l
','n
','s
','=
','"','h
','t
','t
','p
',':','/','/','b
','g','r','i','f','f','i','n','.','n','e','t','/','t','x','"','
','/','>',chNull};
DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(gLS);
DOMLSParser* parser = ((DOMImplementationLS*)impl)-
>createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
DOMConfiguration* dc = parser->getDomConfig();
Err* errHandler = new Err();
dc->setParameter(XMLUni::fgDOMErrorHandler,errHandler);
dc->setParameter(XMLUni::fgDOMValidate, true);
DOMLSInput* input = ((DOMImplementationLS*)impl)->createLSInput();
input->setStringData(file);
input->setEncoding(XMLUni::fgXMLChEncodingString); //Alberto
Massari's idea.
DOMDocument* doc = parser->parse(input);
XMLPlatformUtils::Terminate();
return 0;
}
Re: attribute 'xmlns' is not declared for element 'tx' - huh?
Posted by Ben Griffin <be...@redsnapper.net>.
Thanks Dave, that's very helpful.
So what I had thought was that the namespaceUri represented an
unresolved resource, just as the systemId does for DTDs.
Even though I am aware the different function of the namespaceUri (in
terms of XML).
Does this mean that any schemas that is to be used must be preloaded
for the parser to load documents without error?
Is there a way of dynamically allocating a grammar to a document based
on it's namespace at load time via something similar to
DOMLSResourceResolver -- eg some sort of "DOMLSNamespaceResolver" ?
On 16 Mar 2009, at 16:13, David Bertoni wrote:
> Ben Griffin wrote:
>> Dave, once again, thank-you for your response and the support.
>> Yes, making the two changes that you suggest gets the document to
>> load without error.
>> What is it I need to do to get the
>> DOMLSResourceResolver::resolveResource() triggered?
> You need a reference to an external entity in your XML document.
> Otherwise, there's no reason for the parser to call it.
>
> You also have a few memory leaks in your code. The return value
> from XMLString::transcode() must be freed by calling
> XMLString::release(). Also, the casts to char* are unnecessary, and
> the line-termination character should be "\n" and not "\r".
>
> Dave
Here is the revised code, with an additional Writer put in also.
As Alberto mentioned, using setStringData, it's necessary to also
setEncoding (unless there is an xml declaration).
What that means is that I can use UTF-8 directly, and let the parser
do the transcoding, as it appears to want to generate the file as if
it were a stream anyway.
Does this mean that there's a xerces-c 'bug' - in that the parameter
to setStringData (and the relevant structure) should be type XMLByte*
rather than XMLCh* , in that it appears to be treated as XMLByte
internally?
Are there any reasons why I shouldn't do the following in general?
#include <iostream>
#include <xercesc/util/XercesDefs.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationLS.hpp>
#include <xercesc/dom/DOMLSParser.hpp>
#include <xercesc/dom/DOMLSSerializer.hpp>
#include <xercesc/dom/DOMLSInput.hpp>
#include <xercesc/dom/DOMLSOutput.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/framework/MemBufFormatTarget.hpp>
using namespace xercesc;
class Err: public DOMErrorHandler {
public:
bool Err::handleError(const xercesc::DOMError& domError) {
char *msg = XMLString::transcode(domError.getMessage());
std::cerr << "DOM Error:" << msg << "\n";
XMLString::release(&msg);
return true;
}
};
int main (int argc, char * const argv[]) {
XMLPlatformUtils::Initialize();
const XMLCh gLS[] = { 'L', 'S', chNull };
std::string grammar = "<schema xmlns='http://www.w3.org/2001/
XMLSchema' targetNamespace='anyXml'><element name='xml' type='anyType'/
></schema>";
std::string example = "<xml xmlns='anyXml'><foo bar='bim'><baz /></
foo></xml>";
Err* err = new Err();
DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(gLS);
DOMLSParser* parser = ((DOMImplementationLS*)impl)-
>createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
DOMLSSerializer* serializer = ((DOMImplementationLS*)impl)-
>createLSSerializer();
MemBufFormatTarget* mbft = new MemBufFormatTarget();
DOMLSInput* ginput = ((DOMImplementationLS*)impl)->createLSInput();
DOMLSInput* input = ((DOMImplementationLS*)impl)->createLSInput();
DOMLSOutput* output = ((DOMImplementationLS*)impl)->createLSOutput();
ginput->setEncoding(XMLUni::fgUTF8EncodingString);
input->setEncoding(XMLUni::fgUTF8EncodingString);
output->setEncoding(XMLUni::fgUTF8EncodingString);
output->setByteStream(mbft);
DOMConfiguration* dc = parser->getDomConfig();
dc->setParameter(XMLUni::fgXercesSchema, true);
dc->setParameter(XMLUni::fgXercesSchemaFullChecking, true);
dc->setParameter(XMLUni::fgXercesUseCachedGrammarInParse, true);
dc->setParameter(XMLUni::fgDOMDatatypeNormalization, true);
dc->setParameter(XMLUni::fgDOMNamespaces, true);
dc->setParameter(XMLUni::fgDOMValidate, true);
dc->setParameter(XMLUni::fgDOMErrorHandler,err);
DOMConfiguration* wdc = serializer->getDomConfig();
wdc->setParameter(XMLUni::fgDOMErrorHandler,err);
wdc->setParameter(XMLUni::fgDOMXMLDeclaration,false);
wdc->setParameter(XMLUni::fgDOMWRTBOM,false);
ginput->setStringData((XMLCh*)grammar.c_str());
parser->loadGrammar(ginput, Grammar::SchemaGrammarType, true);
input->setStringData((XMLCh*)example.c_str());
DOMDocument* doc = parser->parse(input);
serializer->write(doc,output);
std::cout << (const char *)mbft->getRawBuffer() << "\n";
delete mbft;
output->release();
input->release();
ginput->release();
serializer->release();
parser->release();
delete err;
XMLPlatformUtils::Terminate();
int x;
std::cin >> x;
return 0;
}
Re: attribute 'xmlns' is not declared for element 'tx' - huh?
Posted by David Bertoni <db...@apache.org>.
Ben Griffin wrote:
> Dave, once again, thank-you for your response and the support.
>
> Yes, making the two changes that you suggest gets the document to load
> without error.
> What is it I need to do to get the
> DOMLSResourceResolver::resolveResource() triggered?
You need a reference to an external entity in your XML document.
Otherwise, there's no reason for the parser to call it.
You also have a few memory leaks in your code. The return value from
XMLString::transcode() must be freed by calling XMLString::release().
Also, the casts to char* are unnecessary, and the line-termination
character should be "\n" and not "\r".
Dave
Re: attribute 'xmlns' is not declared for element 'tx' - huh?
Posted by Ben Griffin <be...@redsnapper.net>.
Dave, once again, thank-you for your response and the support.
Yes, making the two changes that you suggest gets the document to load
without error.
What is it I need to do to get the
DOMLSResourceResolver::resolveResource() triggered?
Best regards
Ben
On 16 Mar 2009, at 06:59, David Bertoni wrote:
> Ben Griffin wrote:
>> Dave thanks for your suggestion,
>> But it doesn't appear to be right - let me show you:
>> I add a resource handler, and also a grammar in the following code.
>> However, the output remains the same
> There are two problems here. The first is the target namespace in
> your schema doesn't match the namespace of the document.
>
> The other is you need to tell the DOMBuilder to use cached grammars
> when parsing:
>
> dc->setParameter(XMLUni::fgXercesUseCachedGrammarInParse, true);
>
> Dave
Re: attribute 'xmlns' is not declared for element 'tx' - huh?
Posted by David Bertoni <db...@apache.org>.
Ben Griffin wrote:
> Dave thanks for your suggestion,
>
> But it doesn't appear to be right - let me show you:
>
> I add a resource handler, and also a grammar in the following code.
> However, the output remains the same
There are two problems here. The first is the target namespace in your
schema doesn't match the namespace of the document.
The other is you need to tell the DOMBuilder to use cached grammars when
parsing:
dc->setParameter(XMLUni::fgXercesUseCachedGrammarInParse, true);
Dave
Re: attribute 'xmlns' is not declared for element 'tx' - huh?
Posted by Ben Griffin <be...@redsnapper.net>.
Dave thanks for your suggestion,
But it doesn't appear to be right - let me show you:
I add a resource handler, and also a grammar in the following code.
However, the output remains the same
> DOM Error:no declaration found for element 'tx'
> DOM Error:attribute 'xmlns' is not declared for element 'tx'
Best regards (and truly confused!)
Ben.
On 13 Mar 2009, at 17:29, David Bertoni wrote:
>> You're forcing the parser to validate your document, even though it
>> has no grammar to use. As a result, it will report a validation
>> error for every element in the document.
>
> Dave
#include <iostream>
#include <xercesc/util/XercesDefs.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationLS.hpp>
#include <xercesc/dom/DOMLSParser.hpp>
#include <xercesc/dom/DOMLSInput.hpp>
#include <xercesc/dom/DOMLSResourceResolver.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/parsers/AbstractDOMParser.hpp>
using namespace xercesc;
class Rsr : public DOMLSResourceResolver {
public:
DOMLSInput* Rsr::resolveResource(
const XMLCh* const resourceType,
const XMLCh* const namespaceUri,
const XMLCh* const publicId,
const XMLCh* const systemId,
const XMLCh* const baseURI ) {
DOMLSInput* retval=NULL;
if (namespaceUri) {
std::cerr << "Not found:" << (char *)
(XMLString::transcode(namespaceUri)) << "\r";
}
return retval;
}
};
class Err: public DOMErrorHandler {
public:
bool Err::handleError(const xercesc::DOMError& domError) {
std::cerr << "DOM Error:" << (char *)
(XMLString::transcode(domError.getMessage())) << "\r";
return true;
}
};
int main (int argc, char * const argv[]) {
XMLPlatformUtils::Initialize();
const XMLCh gLS[] = { 'L', 'S', chNull };
const XMLCh xfil[] = {'<','t','x','
','x
','m
','l
','n
','s
','=
','"','h
','t
','t
','p
',':','/','/','b
','g','r','i','f','f','i','n','.','n','e','t','/','t','x','"','
','/','>',chNull};
const XMLCh grammar[] ={'<','s','c','h','e','m','a','
','x
','m
','l
','n
','s
','=
','"','h
','t
','t
','p
',':','/','/','w
','w
','w
','.','w
','3
','.','o
','r
','g','/','2','0','0','1','/','X','M','L','S','c','h','e','m','a','"',
'
','t
','a
','r
','g
','e
','t
','N
','a
','m
','e
','s
','p
','a
','c
','e
','=
','"','h
','t
','t
','p
',':','/','/','b','g','r','i','f','f','i','n','.','n','e','t','"','>',
'<','e','l','e','m','e','n','t','
','n','a','m','e','=','"','t','x','"','
','t
','y
','p
','e
','=
','"','a
','n
','y
','T
','y','p','e','"','/','>','<','/','s','c','h','e','m','a','>',chNull};
DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(gLS);
DOMLSParser* parser = ((DOMImplementationLS*)impl)-
>createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
DOMConfiguration* dc = parser->getDomConfig();
Err* errHandler = new Err();
Rsr* rsrHandler = new Rsr();
dc->setParameter(XMLUni::fgXercesSchema, true);
dc->setParameter(XMLUni::fgXercesSchemaFullChecking, true);
dc->setParameter(XMLUni::fgDOMDatatypeNormalization, true);
dc->setParameter(XMLUni::fgDOMNamespaces, true);
dc->setParameter(XMLUni::fgDOMValidate, true);
dc->setParameter(XMLUni::fgDOMResourceResolver,rsrHandler);
dc->setParameter(XMLUni::fgDOMErrorHandler,errHandler);
DOMLSInput* ginput = ((DOMImplementationLS*)impl)->createLSInput();
DOMLSInput* input = ((DOMImplementationLS*)impl)->createLSInput();
ginput->setStringData(grammar);
ginput->setEncoding(XMLUni::fgXMLChEncodingString); //Alberto
Massari's idea.
input->setStringData((XMLCh*)xfil);
input->setEncoding(XMLUni::fgXMLChEncodingString); //Alberto
Massari's idea.
parser->loadGrammar(ginput, Grammar::SchemaGrammarType, true);
DOMDocument* doc = parser->parse(input);
input->release();
ginput->release();
parser->release();
delete errHandler;
delete rsrHandler;
XMLPlatformUtils::Terminate();
return 0;
}
Re: attribute 'xmlns' is not declared for element 'tx' - huh?
Posted by David Bertoni <db...@apache.org>.
Ben Griffin wrote:
> The following program outputs ( on cerr )
>
> >DOM Error:no declaration found for element 'tx'
> >DOM Error:attribute 'xmlns' is not declared for element 'tx'
>
> I don't understand why.
> What I would expect is for the namespace to be found, and then an error
> regarding that.
> Instead, there's a problem identifying that the xmlns declaration is a
> document identifier.
>
> What am I doing wrong?
>
>
> -------------------------------------------------
> #include <iostream>
>
> #include <xercesc/util/XercesDefs.hpp>
> #include <xercesc/util/PlatformUtils.hpp>
> #include <xercesc/util/XMLString.hpp>
>
> #include <xercesc/dom/DOMErrorHandler.hpp>
> #include <xercesc/dom/DOMImplementationRegistry.hpp>
> #include <xercesc/dom/DOMImplementation.hpp>
> #include <xercesc/dom/DOMImplementationLS.hpp>
> #include <xercesc/dom/DOMLSParser.hpp>
> #include <xercesc/dom/DOMLSInput.hpp>
>
>
> using namespace xercesc;
>
> class Err: public DOMErrorHandler {
> public:
> bool Err::handleError(const xercesc::DOMError& domError) {
> std::cerr << "DOM Error:" << (char
> *)(XMLString::transcode(domError.getMessage())) << "\r";
> return true;
> }
> };
>
> int main (int argc, char * const argv[]) {
> XMLPlatformUtils::Initialize();
> const XMLCh gLS[] = { 'L', 'S', chNull };
> // <tx xmlns="http://bgriffin.net/tx" /> but with no transocoder.
> const XMLCh file[] = {'<','t','x','
> ','x','m','l','n','s','=','"','h','t','t','p',':','/','/','b','g','r','i','f','f','i','n','.','n','e','t','/','t','x','"','
> ','/','>',chNull};
>
> DOMImplementation* impl =
> DOMImplementationRegistry::getDOMImplementation(gLS);
> DOMLSParser* parser =
> ((DOMImplementationLS*)impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS,
> 0);
>
> DOMConfiguration* dc = parser->getDomConfig();
> Err* errHandler = new Err();
> dc->setParameter(XMLUni::fgDOMErrorHandler,errHandler);
> dc->setParameter(XMLUni::fgDOMValidate, true);
You're forcing the parser to validate your document, even though it has
no grammar to use. As a result, it will report a validation error for
every element in the document.
Dave
Re: attribute 'xmlns' is not declared for element 'tx' - huh?
Posted by Ben Griffin <be...@redsnapper.net>.
Addendum:: I have added all the following to the sample -and it still
fails with the same error.
dc->setParameter(XMLUni::fgXercesSchema, true);
dc->setParameter(XMLUni::fgXercesSchemaFullChecking, true);
dc->setParameter(XMLUni::fgDOMDatatypeNormalization, true);
dc->setParameter(XMLUni::fgDOMNamespaces, true);
On 13 Mar 2009, at 12:47, Ben Griffin wrote:
> The following program outputs ( on cerr )
>
> >DOM Error:no declaration found for element 'tx'
> >DOM Error:attribute 'xmlns' is not declared for element 'tx'
> [...]