XML validation without internet connection

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>
 
C

Colin Berry

If you need to run a batch why not use xalan or xmllint (or some
windows version of them)? For a more visual one at a time approach I
prefer a validating editor like <oXygen/> or xmlExchanger. Hard to
imagine that you need to write your own. Both these approaches work for
me regardless of connection.
 
J

Joe Kesselman

Can't vouch for those tools. The usual Java API for XML parsers lets one
plug in an Entity Resolver which can redirect URI accesses --
specifically, it can redirect them to local files.
 
M

Marc

Colin Berry a écrit :
If you need to run a batch why not use xalan or xmllint (or some
windows version of them)? For a more visual one at a time approach I
prefer a validating editor like <oXygen/> or xmlExchanger. Hard to
imagine that you need to write your own. Both these approaches work for
me regardless of connection.

Thanks a lot !!

xmllint works very well. I will look into xmllint source code to try to
correct my code
(I have to embed an xml validating parser in a larger C/C++ code).

Marc
 
M

Marc

Joe Kesselman a écrit :
Can't vouch for those tools. The usual Java API for XML parsers lets one
plug in an Entity Resolver which can redirect URI accesses --
specifically, it can redirect them to local files.

Thank you for your suggestion
Is there a similar plug in C xml API ?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top