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

Piotr Sipika <[email protected]>
Newsgroups gmane.comp.gnome.lib.xml.general,gmane.text.xml.schema.devel
Message-ID <[email protected]>
Hi Andy,

> 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!
> 
The XML Namespace spec(1) states that the namespace URI need not be a
valid URL (i.e. point to a valid document on the network). In other
words, you are declaring your namespaces correctly.

> http://www.w3.org/TR/xmlschema-1/#no-xsi is very confusing. 
If you look at section 3.2.7 (first table) at that location, you'll see
that using 'xsi:type' is perfectly fine.

> 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?
You don't have to worry about libxml retrieving resources at namespace
locations, that does not happen. Only the actual URI string matters (as
per (1)).

> what URL should I be using?
> 
> Do I need to make a special call to cause the libxml to use this other
> location?
Nope, you're using the right URIs, no network access is necessary.

> Would you be willing to send my your sample C program? 
Sure thing (I should have done so in my original reply).

[] to compile:
$ gcc `xml2-config --cflags --libs` zoo.c -o zoo

[] sample run:
$ ./zoo zoo.xml ZooRequest.xsd
Attempting to validate zoo.xml with ZooRequest.xsd
Document in zoo.xml is valid

[] contents of zoo.xml:
$ cat zoo.xml
<?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>
    <numberOfFins>4</numberOfFins>
  </animal>
</zoo:cageRequest>

Hope you get things sorted out.

Piotr


(1) - http://www.w3.org/TR/REC-xml-names/#ns-decl

_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
http://mail.gnome.org/mailman/listinfo/xml
zoo.c (text/plain, 2.4 KB)
/** 
 * Sample namespace XSD validation program. 
 * Released into public domain.
 * Piotr Sipika
 */

#include <stdlib.h>
#include <stdio.h>

#include <libxml/xmlschemas.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

int
main(int argc, char * argv[])
{
  if (argc != 3)
    {
      fprintf(stderr, "Need file name and schema file name\n");

      return EXIT_FAILURE;
    }

  const char *pczFile = argv[1];
  const char *pczSchema = argv[2];

  xmlInitParser();

  xmlDocPtr pDoc = xmlReadFile(pczFile, NULL, XML_PARSE_NONET);

  if (!pDoc)
    {
      fprintf(stderr, "Unable to read %s\n", pczFile);

      xmlCleanupParser();

      return EXIT_FAILURE;
    }

  xmlDocPtr pSchemaDoc = xmlReadFile(pczSchema, NULL, XML_PARSE_NONET);

  if (!pSchemaDoc)
    {
      fprintf(stderr, "Unable to read %s\n", pczSchema);

      xmlFreeDoc(pDoc);

      xmlCleanupParser();

      return EXIT_FAILURE;
    }

  // create actual schema
  xmlSchemaParserCtxtPtr pSchemaCtxt = xmlSchemaNewDocParserCtxt(pSchemaDoc);

  if (!pSchemaCtxt)
    {
      fprintf(stderr, "Unable to create schema context\n");

      xmlFreeDoc(pDoc);

      xmlFreeDoc(pSchemaDoc);

      xmlCleanupParser();

      return EXIT_FAILURE;
    }

  xmlSchemaPtr pSchema = xmlSchemaParse(pSchemaCtxt);

  if (!pSchema)
    {
      fprintf(stderr, "Unable to create schema\n");

      xmlSchemaFreeParserCtxt(pSchemaCtxt);

      xmlFreeDoc(pDoc);

      xmlFreeDoc(pSchemaDoc);

      xmlCleanupParser();

      return EXIT_FAILURE;
    }

  xmlSchemaValidCtxtPtr pValidCtxt = xmlSchemaNewValidCtxt(pSchema);

  if (!pValidCtxt)
    {
      fprintf(stderr, "Unable to create validity context for schema\n");

      xmlSchemaFree(pSchema);

      xmlSchemaFreeParserCtxt(pSchemaCtxt);

      xmlFreeDoc(pDoc);

      xmlFreeDoc(pSchemaDoc);

      xmlCleanupParser();

      return EXIT_FAILURE;
    }

  fprintf(stdout, "Attempting to validate %s with %s\n", pczFile, pczSchema);

  xmlSchemaFreeParserCtxt(pSchemaCtxt);

  xmlFreeDoc(pSchemaDoc);

  int iError = xmlSchemaValidateDoc(pValidCtxt, pDoc);

  if (iError == 0)
    {
      fprintf(stdout, "Document in %s is valid\n", pczFile);
    }
  else
    {
      fprintf(stdout, "Document in %s is NOT valid\n", pczFile);
    }

  xmlSchemaFree(pSchema);
  
  //  xmlSchemaFreeParserCtxt(pSchemaCtxt);
  
  xmlFreeDoc(pDoc);
  
  //  xmlFreeDoc(pSchemaDoc);
  
  xmlCleanupParser();
                               
  return EXIT_SUCCESS;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.