W3C home > Mailing lists > Public > xmlschema-dev@w3.org > December 2011

Re: libxml2 does xmlSchemaValidateDoc() support xml schema extensions?

From: Andy Davidson <andy_davidson@apple.com>
Date: Thu, 15 Dec 2011 15:44:50 -0800
Cc: xml@gnome.org, xmlschema-dev@w3.org
Message-id: <9FF0C514-E678-4B25-A27B-7D2892DE23E2@apple.com>
To: Piotr Sipika <piotreks@optonline.net>
Hi Piortr

I still can not validate xml that uses xml schema extensions. I think I figured out what the problem might be, but do not know how to resolve i. I wonder if the problem is with the way I set up my xml namespace?

<zoo:cageRequest xmlns:zoo="http://www.example.org/Zoo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

I am setting up the xml schema namespace the same way eclipse does when you create a new XSD or XML file. I entered http://www.w3.org/2001/XMLSchema-instance in the address bar of my browsers and got quite a surprise!
  <xs:documentation><p>This schema should never be used as such:
                    <a href="http://www.w3.org/TR/xmlschema-1/#no-xsi">the XML
                    Schema Recommendation</a> forbids the declaration of
                    attributes in this namespace</p>
http://www.w3.org/TR/xmlschema-1/#no-xsi is very confusing. 

where does libxml get the schema lang schema/dtd? As a test I put some typos in my xsd file. libxml generated an error when I tried to validate the xsd file as expected?

what URL should I be using?

Do I need to make a special call to cause the libxml to use this other location?

My app will be running on a private network and not have access to www.w3.org, Do I need to copy a dtd or something to my local env?

thanks in advance


p.s. Bellow is a long email I was going to send before I got the surprise I mentioned above. There is a nice code example of how how to validate. Its much simpler than the other examples I have seen posted in the past

On Dec 13, 2011, at 7:19 AM, Piotr Sipika wrote:
> I wrote a sample program (in C) to attempt to mimic what you have, and
> was able to successfully validate the instance against the schema you
> provided.

Would you be willing to send my your sample C program? 

I must be missing a function call somewhere?  My XML/XSD files that do not use extensions validate so I think my basic code structure is correct. In your original post you mentioned "xmllint". I did not know about that tool. All of my files validate according to xmllint.I stepped through xmllint to see how it worked. Its very complicated. I must have missed something. Looking at the xmllint source I was able to simplify my code and get rid some some memory leaks.

Here is my simplified code listing based on the xmllint source

   const char *schemaFileNameStr = ...;
    // http://xmlsoft.org/html/libxml-xmlschemas.html#xmlSchemaNewParserCtxt
    xmlSchemaParserCtxtPtr ctxt = xmlSchemaNewParserCtxt(schemaFileNameStr);
                             (xmlSchemaValidityErrorFunc) fprintf,
                             (xmlSchemaValidityWarningFunc) fprintf,
	xmlSchemaPtr wxschemas = xmlSchemaParse(ctxt);
	if (wxschemas == NULL) {
	    NSLog(@"******* ERROR schema %@ failed to compile\n", xmlSchemaFilePath);
	    return ret; 

	for (all the xml files) {
   	// http://www.xmlsoft.org/html/libxml-xmlschemas.html#xmlSchemaNewValidCtxt
    	xmlSchemaValidCtxtPtr vctxt = xmlSchemaNewValidCtxt(wxschemas);
    	// http://xmlsoft.org/html/libxml-xmlschemas.html#xmlSchemaSetValidErrors
                            (xmlSchemaValidityErrorFunc) fprintf,
                            (xmlSchemaValidityWarningFunc) fprintf,
    	// http://xmlsoft.org/html/libxml-xmlschemas.html#xmlSchemaValidateDoc
		int error = xmlSchemaValidateDoc(vctxt, doc);
		if (error == 0) {
        		ret = YES;
		} else if (error > 0) {
        		ret = NO;
	    		fprintf(stderr, "%s fails to validate\n", filename);
		} else {
        		ret = NO; // validation generated an internal error

<?xml version="1.0" encoding="UTF-8"?>
<zoo:cageRequest xmlns:zoo="http://www.example.org/Zoo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <animal xsi:type="zoo:Fish">
    <name>Blue Fin Tuna</name>

element animal: Schemas validity error : Element 'animal', attribute 'xsi:type': The attribute 'xsi:type' is not allowed.
element numberOfFins: Schemas validity error : Element 'numberOfFins': This element is not expected.

  ELEMENT zoo:cageRequest
    namespace zoo href=http://www.example.org/Zoo
    namespace xsi href=http://www.w3.org/2001/XMLSchema-instanc...
    ELEMENT animal
      ATTRIBUTE xsi:type
      ELEMENT name
          content=Blue Fin Tuna
      ELEMENT numberOfFins
Received on Thursday, 15 December 2011 23:46:06 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:56:19 UTC