M
Marc
Hi,
I wrote a simple tool to validate XML files using XMLSchema (C program
+ libxml2 library).
It works when I'm connected to the internet (access to www.w3.org)
Is it possible to modify this tool and/or the XML files to validate off
line (see source code below) ?
Thanks in advance for your help,
Marc
Here are simplified versions of the tool, the XMLSchema file,
and a test file :
________________________________________________________________
validate.c:
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlschemas.h>
int validate(const char *filename) {
int res;
xmlDocPtr doc;
xmlSchemaValidCtxtPtr validationCtxt;
doc = xmlReadFile(filename, NULL, 0);
validationCtxt = xmlSchemaNewValidCtxt(NULL);
res = xmlSchemaValidateDoc(validationCtxt, doc);
xmlSchemaFreeValidCtxt(validationCtxt);
xmlFreeDoc(doc);
return res;
}
int main(int argc, char **argv) {
int res = validate(argv[1]);
if (res == -1)
fprintf(stderr, "Internal error during validation.\n");
else if (res == 0)
fprintf(stdout, "The document '%s' is valid.\n", filename);
else
fprintf(stderr, "The document '%s' is not valid.\n", filename);
xmlCleanupParser();
return(res);
}
________________________________________________________________
XML Schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DataType">
<xs:complexType>
<xs:sequence>
<xs:element name="extends" type="xs:string"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="restricts" type="xs:string"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="DataTypes">
<xs:complexType>
<xs:sequence>
<xs:element ref="DataType"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
________________________________________________________________
test file
<?xml version="1.0"?>
<DataTypes
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="dataType.xsd">
<DataType name = "int">
<extends>short</extends>
<restricts>long</restricts>
</DataType>
</DataTypes>
I wrote a simple tool to validate XML files using XMLSchema (C program
+ libxml2 library).
It works when I'm connected to the internet (access to www.w3.org)
Is it possible to modify this tool and/or the XML files to validate off
line (see source code below) ?
Thanks in advance for your help,
Marc
Here are simplified versions of the tool, the XMLSchema file,
and a test file :
________________________________________________________________
validate.c:
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlschemas.h>
int validate(const char *filename) {
int res;
xmlDocPtr doc;
xmlSchemaValidCtxtPtr validationCtxt;
doc = xmlReadFile(filename, NULL, 0);
validationCtxt = xmlSchemaNewValidCtxt(NULL);
res = xmlSchemaValidateDoc(validationCtxt, doc);
xmlSchemaFreeValidCtxt(validationCtxt);
xmlFreeDoc(doc);
return res;
}
int main(int argc, char **argv) {
int res = validate(argv[1]);
if (res == -1)
fprintf(stderr, "Internal error during validation.\n");
else if (res == 0)
fprintf(stdout, "The document '%s' is valid.\n", filename);
else
fprintf(stderr, "The document '%s' is not valid.\n", filename);
xmlCleanupParser();
return(res);
}
________________________________________________________________
XML Schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DataType">
<xs:complexType>
<xs:sequence>
<xs:element name="extends" type="xs:string"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="restricts" type="xs:string"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="DataTypes">
<xs:complexType>
<xs:sequence>
<xs:element ref="DataType"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
________________________________________________________________
test file
<?xml version="1.0"?>
<DataTypes
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="dataType.xsd">
<DataType name = "int">
<extends>short</extends>
<restricts>long</restricts>
</DataType>
</DataTypes>