Schema validation: Difference between revisions

From COLLADA Public Wiki
Jump to navigation Jump to search
Elf (talk | contribs)
m Schema Validation moved to Schema validation: lowercase for headers
Elf (talk | contribs)
add {{tutorials}} & misc edits
Line 1: Line 1:
The COLLADA format is defined by a W3C schema. If a document doesn't conform to the schema then it isn't valid COLLADA data. This tutorial explains a few different ways to validate a COLLADA document against the schema.
{{tutorials}}
 
The [[COLLADA]] format is defined by a W3C schema. If a document doesn't conform to the schema then it isn't valid COLLADA data. This tutorial explains a few different ways to '''validate a [[COLLADA document]] against the [[COLLADA schema]]'''.


==Using xmllint==
==Using xmllint==
xmllint is a command-line application that can be used to validate an XML document against a W3C schema. xmllint is distributed as part of [http://xmlsoft.org/ libxml]. You can find binaries for Windows [http://www.zlatkovic.com/libxml.en.html here]. Most Linux distributions come with libxml already installed.
<code>xmllint</code> is a command-line application that can be used to validate an XML document against a W3C schema. <code>xmllint</code> is distributed as part of <code>[http://xmlsoft.org/ libxml]</code>. You can find binaries for Windows [http://www.zlatkovic.com/libxml.en.html here]. Most Linux distributions come with <code>libxml</code> already installed.


To validate the file duck.dae against the latest COLLADA 1.4 schema available from Khronos, run the following command:
To validate the COLLADA document <code>duck.dae</code> against the latest COLLADA 1.4 schema available from [[Khronos]], run the following command:
  xmllint --noout --schema http://www.khronos.org/files/collada_schema_1_4 duck.dae
  xmllint --noout --schema http://www.khronos.org/files/collada_schema_1_4 duck.dae


Line 10: Line 12:
  xmllint --noout --schema colladaSchema.xsd duck.dae
  xmllint --noout --schema colladaSchema.xsd duck.dae


The <code>--noout</code> option prevents superfluous output. If the COLLADA document violates the schema, xmllint will print an error message.
The <code>--noout</code> option prevents superfluous output. If the COLLADA document violates the schema, <code>xmllint</code> outputs an error message.


==Using libxml from a C/C++ Application==
==Using libxml from a C/C++ Application==
Using libxml you can validate a COLLADA document against the schema from a C/C++ application. The following code illustrates how it works.
Using <code>libxml</code>, you can validate a COLLADA document against the schema from a C/C++ application. The following code illustrates how it works.
 
<pre>
#include "libxml/xmlschemas.h"
 
// libxml error handler
void schemaErrorCallback(void*, const char* message, ...) {
  va_list varArgs;
  va_start(varArgs, message);
  vfprintf(stdout, message, varArgs);
  va_end(varArgs);
}
 
// libxml warning handler
void schemaWarningCallback(void* callbackData, const char* message, ...) {
}


// Returns true if file validated successfully, false otherwise
#include "libxml/xmlschemas.h"
bool validate(const string& uri) {
  bool result = false;
// libxml error handler
  if (xmlSchemaParserCtxt* schemaParser =  
void schemaErrorCallback(void*, const char* message, ...) {
  va_list varArgs;
  va_start(varArgs, message);
  vfprintf(stdout, message, varArgs);
  va_end(varArgs);
}
// libxml warning handler
void schemaWarningCallback(void* callbackData, const char* message, ...) {
}
// Returns true if file validated successfully, false otherwise
bool validate(const string& uri) {
  bool result = false;
  if (xmlSchemaParserCtxt* schemaParser =  
       xmlSchemaNewParserCtxt("http://www.khronos.org/files/collada_schema_1_4")) {
       xmlSchemaNewParserCtxt("http://www.khronos.org/files/collada_schema_1_4")) {
    if (xmlSchema* schema = xmlSchemaParse(schemaParser)) {
    if (xmlSchema* schema = xmlSchemaParse(schemaParser)) {
      if (xmlSchemaValidCtxt* validityContext = xmlSchemaNewValidCtxt(schema)) {
      if (xmlSchemaValidCtxt* validityContext = xmlSchemaNewValidCtxt(schema)) {
        xmlSchemaSetValidErrors(validityContext,
        xmlSchemaSetValidErrors(validityContext,
                                 schemaErrorCallback,
                                 schemaErrorCallback,
                                 schemaWarningCallback,
                                 schemaWarningCallback,
                                 /* callback data */ 0);
                                 /* callback data */ 0);
 
        // Returns 0 if validation succeeded
        // Returns 0 if validation succeeded
        result = xmlSchemaValidateFile(validityContext, uri.c_str(), 0) == 0;
        result = xmlSchemaValidateFile(validityContext, uri.c_str(), 0) == 0;
        xmlSchemaFreeValidCtxt(validityContext);
        xmlSchemaFreeValidCtxt(validityContext);
      }
      }
      xmlSchemaFree(schema);
      xmlSchemaFree(schema);
    }
    }
    xmlSchemaFreeParserCtxt(schemaParser);
    xmlSchemaFreeParserCtxt(schemaParser);
  }
  }
 
  return result;
  return result;
}
}
</pre>


[[Category:Tutorials]]
[[Category:Tutorials]]

Revision as of 05:14, 25 May 2007

Tutorials
This article is one several tutorials, guides, and annotated examples available in this wiki.
Multipage tutorials:  • COLLADA DOM user guide
Shorter how-tos:  • Using accessors  • Schema validation • Using URIs
 • Various annotated examples

Instructions for adding a tutorial

[[Category: ]]


The COLLADA format is defined by a W3C schema. If a document doesn't conform to the schema then it isn't valid COLLADA data. This tutorial explains a few different ways to validate a COLLADA document against the COLLADA schema.

Using xmllint

xmllint is a command-line application that can be used to validate an XML document against a W3C schema. xmllint is distributed as part of libxml. You can find binaries for Windows here. Most Linux distributions come with libxml already installed.

To validate the COLLADA document duck.dae against the latest COLLADA 1.4 schema available from Khronos, run the following command:

xmllint --noout --schema http://www.khronos.org/files/collada_schema_1_4 duck.dae

You could also copy the schema locally and use your local version:

xmllint --noout --schema colladaSchema.xsd duck.dae

The --noout option prevents superfluous output. If the COLLADA document violates the schema, xmllint outputs an error message.

Using libxml from a C/C++ Application

Using libxml, you can validate a COLLADA document against the schema from a C/C++ application. The following code illustrates how it works.

#include "libxml/xmlschemas.h"

// libxml error handler
void schemaErrorCallback(void*, const char* message, ...) {
  va_list varArgs;
  va_start(varArgs, message);
  vfprintf(stdout, message, varArgs);
  va_end(varArgs);
}

// libxml warning handler
void schemaWarningCallback(void* callbackData, const char* message, ...) {
}

// Returns true if file validated successfully, false otherwise
bool validate(const string& uri) {
  bool result = false;
  if (xmlSchemaParserCtxt* schemaParser = 
     xmlSchemaNewParserCtxt("http://www.khronos.org/files/collada_schema_1_4")) {
    if (xmlSchema* schema = xmlSchemaParse(schemaParser)) {
      if (xmlSchemaValidCtxt* validityContext = xmlSchemaNewValidCtxt(schema)) {
        xmlSchemaSetValidErrors(validityContext,
                               schemaErrorCallback,
                               schemaWarningCallback,
                               /* callback data */ 0);

        // Returns 0 if validation succeeded
        result = xmlSchemaValidateFile(validityContext, uri.c_str(), 0) == 0;
        xmlSchemaFreeValidCtxt(validityContext);
      }
      xmlSchemaFree(schema);
    }
    xmlSchemaFreeParserCtxt(schemaParser);
  }

  return result;
}