You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Milan Tomic <mi...@setcce.org> on 2004/12/30 18:00:02 UTC

Schema validation

Hello,

	If I try to validate (with Xerces 2.5) my.xml file with some
schema I got this error:

Fatal Error at file 
C:\my.xml
, line 
1
column 
67
Message: 
The schemaLocation attribute does not contain pairs of values.

	I'm attaching my.xml and schema. This is my code:

parser->setDoNamespaces(true);
parser->setCreateEntityReferenceNodes(true);
parser->setDoSchema(true);
parser->setValidationSchemaFullChecking(false);
parser->setExternalSchemaLocation("C:\\xmldsig-core-schema.xsd");
parser->parse(my_xml_file);

	Btw, is there a way to give schema as a input stream, and not to
have to save it to the file? And if it is in the file, will Xerces be
multithread safe?

Thank you,
Milan


  

RE: Schema validation

Posted by Alberto Massari <am...@progress.com>.
At 16.46 31/12/2004 +0100, Milan Tomic wrote:
>Hi,
>
>         In this case, the best way for me is not extract <Signature>
>node and then validate it only. Which is the best way to do it? I have
>DOMElement* pointer to the Signature node.

As I wrote to another user, Xerces-C cannot validate a DOMElement*; you 
will have to serialize it to a string (using DOMWriter), wrap it inside a 
MemBufferInputSource and parse it.

Alberto


>Thank you,
>Milan
>
>
> > -----Original Message-----
> > From: Alberto Massari [mailto:amassari@progress.com]
> > Sent: Friday, December 31, 2004 4:10 PM
> > To: xerces-c-dev@xml.apache.org
> > Subject: RE: Schema validation
> >
> >
> > Hi Milan,
> >
> > At 16.00 31/12/2004 +0100, Milan Tomic wrote:
> > >Hi Albero,
> > >
> > >         I've tried that and got the error as it is in the
> > attachment.
> >
> > you are validating the XML file against the schema for the digital
> > signatures, but your file also contains elements that are in
> > the empty
> > namespace. As you have not loaded a schema for these
> > elements, you will be
> > getting validation errors for all of them.
> > If you want to have zero validation errors, you need to
> > create (or find)
> > the schema for the "note" element and load it using loadGrammar.
> >
> > >[...]
> > >         Btw, do I have to set useCachedGrammarInParse()? I'm
> > >validating with schema only once and never again.
> >
> > useCachedGrammarInParse() is needed, otherwise parse() will
> > not look at the
> > schema that loadGrammar() has previously loaded.
> >
> > Alberto
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> >
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-c-dev-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Schema validation

Posted by Milan Tomic <mi...@setcce.org>.
Hi,

	In this case, the best way for me is not extract <Signature>
node and then validate it only. Which is the best way to do it? I have
DOMElement* pointer to the Signature node.

Thank you,
Milan


> -----Original Message-----
> From: Alberto Massari [mailto:amassari@progress.com] 
> Sent: Friday, December 31, 2004 4:10 PM
> To: xerces-c-dev@xml.apache.org
> Subject: RE: Schema validation
> 
> 
> Hi Milan,
> 
> At 16.00 31/12/2004 +0100, Milan Tomic wrote:
> >Hi Albero,
> >
> >         I've tried that and got the error as it is in the 
> attachment.
> 
> you are validating the XML file against the schema for the digital 
> signatures, but your file also contains elements that are in 
> the empty 
> namespace. As you have not loaded a schema for these 
> elements, you will be 
> getting validation errors for all of them.
> If you want to have zero validation errors, you need to 
> create (or find) 
> the schema for the "note" element and load it using loadGrammar.
> 
> >[...]
> >         Btw, do I have to set useCachedGrammarInParse()? I'm 
> >validating with schema only once and never again.
> 
> useCachedGrammarInParse() is needed, otherwise parse() will 
> not look at the 
> schema that loadGrammar() has previously loaded.
> 
> Alberto 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Schema validation

Posted by Alberto Massari <am...@progress.com>.
Hi Milan,

At 16.00 31/12/2004 +0100, Milan Tomic wrote:
>Hi Albero,
>
>         I've tried that and got the error as it is in the attachment.

you are validating the XML file against the schema for the digital 
signatures, but your file also contains elements that are in the empty 
namespace. As you have not loaded a schema for these elements, you will be 
getting validation errors for all of them.
If you want to have zero validation errors, you need to create (or find) 
the schema for the "note" element and load it using loadGrammar.

>[...]
>         Btw, do I have to set useCachedGrammarInParse()? I'm validating
>with schema only once and never again.

useCachedGrammarInParse() is needed, otherwise parse() will not look at the 
schema that loadGrammar() has previously loaded.

Alberto 



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Schema validation

Posted by Milan Tomic <mi...@setcce.org>.
Hi Albero,

	I've tried that and got the error as it is in the attachment.
This is my code:

XercesDOMParser * parser = new XercesDOMParser;
Janitor<XercesDOMParser> j_parser(parser);

parser->setDoNamespaces(true);
parser->setCreateEntityReferenceNodes(true);  

if (doCheckSchema)
{

  HINSTANCE insApp   = AfxGetInstanceHandle();
  HRSRC     hResInfo =
FindResource(insApp,(LPCSTR)IDR_SCHEMA_XMLSIG,"SCHEMAS");
  HGLOBAL   hRes     = LoadResource(insApp,hResInfo );

  DWORD dwFileLength = SizeofResource( insApp, hResInfo );

  if (dwFileLength == 0) {
    return 0;
  }

  MemBufInputSource * is = new MemBufInputSource((const unsigned char
*const)hRes, dwFileLength, "XYZ");
  Janitor<MemBufInputSource> j_is(is);  

  parser->loadGrammar(*is, Grammar::SchemaGrammarType, true);

  parser->setDoSchema(true);
  parser->setValidationSchemaFullChecking(true);
  parser->setValidationScheme(XercesDOMParser::Val_Always);
  parser->useCachedGrammarInParse(true);

}

	Btw, do I have to set useCachedGrammarInParse()? I'm validating
with schema only once and never again.

Thank you very much,
Milan

RE: Schema validation

Posted by Alberto Massari <am...@progress.com>.
Hi Milan,
if I recall it correctly, that mail was addressing a different problem 
(forcing validation against a DTD); and this required some carefully coding.
If you need to validate against one or more XMLSchemas, this should work:

XercesDOMParser * parser = new XercesDOMParser;

MemBufInputSource is("<xs:schema....", dwTextLength, "");
parser->loadGrammar(is, Grammar::SchemaGrammarType, true);

parser->setDoSchema(true);
parser->setValidationSchemaFullChecking(true);
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->useCachedGrammarInParse(true);
parser->parse(...);

delete parser;

in short, you invoke loadGrammar for every schema you want to load, 
specifying the third argument (toCache) to true; this way all the schemas 
will be kept inside the GrammarResolver of the parser. By calling 
setValidationScheme(XercesDOMParser::Val_Always) you tell the parser to 
always validate the data being parsed (even if it doesn't have a reference 
to a schema), and by calling useCachedGrammarInParse() you tell him to use 
this cache of schemas.

Hope this helps,
Alberto

At 11.08 31/12/2004 +0100, Milan Tomic wrote:
>Hi Alberto,
>
>         I've succeeded to manage setExternalSchemaLocation() to work OK,
>but I would much raither like to use loadGrammar(). I've found in mail
>archives (
>http://www.mail-archive.com/xerces-c-dev@xml.apache.org/msg13604.html )
>your response on this topic and I did the same using this code:
>
>MemBufInputSource * is = new MemBufInputSource((const unsigned char
>*const)hRes, dwFileLength, "Some text...");
>
>XercesDOMParser * schema = new XercesDOMParser;
>Grammar * pSchema = schema->loadGrammar(*is,
>Grammar::SchemaGrammarType);
>schemaValidator = new SchemaValidator();
>schemaValidator->setGrammar(pSchema);
>
>XercesDOMParser * parser = new XercesDOMParser(schemaValidator);
>parser->setDoNamespaces(true);
>parser->setCreateEntityReferenceNodes(true);
>
>DOMTreeErrorReporter * errorHandler = new DOMTreeErrorReporter();
>parser->setErrorHandler(errorHandler);
>
>parser->setDoSchema(true);
>parser->setValidationSchemaFullChecking(true);
>parser->setValidationScheme(XercesDOMParser::Val_Always);
>
>parser->parse("C:\\my.xml");
>
>         And when using files I have attached with my previous post, I
>got this error:
>
>Fatal Error at file
>C:\my.xml
>, line
>1
>column
>116
>Message:
>An exception occurred! Type:RuntimeException, Message:A DOCTYPE was seen
>but the installed validator does not understand DTDs
>
>         What is it? Btw, does input stream ( for loadGrammar() ) have to
>end with 0 byte? If not, Is it error if it does?
>
>Thank you,
>Milan
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xerces-c-dev-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Schema validation

Posted by Milan Tomic <mi...@setcce.org>.
P.S. If I wish to check some XML file with more than 1 schema in the
same time, should I use XMLGrammarPool or something else?



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: Schema validation

Posted by Milan Tomic <mi...@setcce.org>.
Hi Alberto,

	I've succeeded to manage setExternalSchemaLocation() to work OK,
but I would much raither like to use loadGrammar(). I've found in mail
archives (
http://www.mail-archive.com/xerces-c-dev@xml.apache.org/msg13604.html )
your response on this topic and I did the same using this code:

MemBufInputSource * is = new MemBufInputSource((const unsigned char
*const)hRes, dwFileLength, "Some text...");

XercesDOMParser * schema = new XercesDOMParser;
Grammar * pSchema = schema->loadGrammar(*is,
Grammar::SchemaGrammarType);
schemaValidator = new SchemaValidator();
schemaValidator->setGrammar(pSchema);

XercesDOMParser * parser = new XercesDOMParser(schemaValidator);
parser->setDoNamespaces(true);
parser->setCreateEntityReferenceNodes(true);  

DOMTreeErrorReporter * errorHandler = new DOMTreeErrorReporter();
parser->setErrorHandler(errorHandler);

parser->setDoSchema(true);
parser->setValidationSchemaFullChecking(true);
parser->setValidationScheme(XercesDOMParser::Val_Always);

parser->parse("C:\\my.xml");

	And when using files I have attached with my previous post, I
got this error:

Fatal Error at file 
C:\my.xml
, line 
1
column 
116
Message: 
An exception occurred! Type:RuntimeException, Message:A DOCTYPE was seen
but the installed validator does not understand DTDs

	What is it? Btw, does input stream ( for loadGrammar() ) have to
end with 0 byte? If not, Is it error if it does?

Thank you,
Milan



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: Schema validation

Posted by Alberto Massari <am...@progress.com>.
Hi Milan,

At 18.00 30/12/2004 +0100, Milan Tomic wrote:
>[...]
>parser->setExternalSchemaLocation("C:\\xmldsig-core-schema.xsd");

The method setExternalSchemaLocation expects a sequence of pairs of 
strings, where the first one is the namespace URI and the second is the 
location of the schema file. In your case, you should write

parser->setExternalSchemaLocation("http://www.w3.org/2000/09/xmldsig# 
C:\\xmldsig-core-schema.xsd");

>[...]
>         Btw, is there a way to give schema as a input stream, and not to 
> have to save it to the file? And if it is in the file, will Xerces be 
> multithread safe?

There are no multithreading problems in reading the schemas from file; but, 
if you want to use an input stream, you should use loadGrammar() before 
invoking parse()

Hope this helps,
Alberto 



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org