List Info

Thread: Re: Necessary steps involved in validating against an




Re: Necessary steps involved in validating against an
country flaguser name
Australia
2007-02-04 20:02:00
Hi,

Regaring Delphi programmer Erik Andersen's query about using
an XSD when
using libxml2, I offer the code below, based on
testSchemas.c, as a
simple example of how to accomplish this in a Delphi
application
(although it wasn't entirely clear from his original
question whether
Erik is developing his code in C or Delphi). 

Most of this is almost trivial. The biggest challenge is in
developing
the error-handling code, as Delphi does not have an exact
equivalent of
the C "printf" family of functions. 

The easiest way around this difficulty is to not bother
calling either
xmlSchemaSetParserErrors or xmlSchemaSetValidErrors, and
rely on the
default error handler. The default error handler in libxml2
will, I
believe, attempt to write any error message to stderr. This
should work
OK in a console application, but is likely to fail if the
application is
using a Windows GUI (since "stderr" is not
well-defined in that
context). 

In the code below, I attempt to get around this problem in a
very simple
way. "ErrorFunc" serves as the error-handling
function. Using a bit of
declaration trickery to allow it to pass the parameters, it
calls the
wvsprintf function provided by Windows to format the message
string into
a buffer. The buffer is then written to a file, but the ctx
parameter
could just as easily be used to pass a pointer to a Windows
edit control
where the text could be displayed. 

I hope this helps.

////////////////////////////////////////////////////////////
////////////
/////////////////////// 
program Schematest;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  windows,
  libxml2;

procedure ErrorFunc(ctx: pointer; const msg: PChar); cdecl;
var
  ptr_args: array[0..100] of Pointer absolute msg;
  buffer: string;
  len: integer;
type
  PFile = ^Text;
begin
  SetLength(buffer, 1024); // Allocate enough space
  len := wvsprintf(PChar(buffer), msg, (ptr_args[1]));
  SetLength(buffer, len);  // Truncate to the actual string
length
  if Assigned(ctx) then
    write(PFile(ctx)^, buffer)
  else
    write(buffer);
end;

var
  parserCtxt: xmlSchemaParserCtxtPtr;
  validCtxt: xmlSchemaValidCtxtPtr;
  doc: xmlDocPtr;
  schema: xmlSchemaPtr;
  ret: integer;
  logfile: Text;
begin
  if ParamCount < 2 then
    writeln('Usage: ' + ParamStr(0) + ' schemaFile
xmlFile')
  else
  begin
    AssignFile (logfile, 'test.log');
    Rewrite(logfile);
    parserCtxt :=
xmlSchemaNewParserCtxt(PChar(ParamStr(1)));
    xmlSchemaSetParserErrors(parserCtxt,
        xmlSchemaValidityErrorFunc(ErrorFunc),
        xmlSchemaValidityWarningFunc(ErrorFunc),
        logfile);
    schema := xmlSchemaParse(parserCtxt);
    xmlSchemaFreeParserCtxt(parserCtxt);

    if Assigned(schema) then
    begin
      doc := xmlReadFile(PChar(ParamStr(2)), nil, 0);
      if (doc = nil) then
          writeln('Could not parse ' + ParamStr(2))
      else
      begin
        validCtxt := xmlSchemaNewValidCtxt(schema);
        xmlSchemaSetValidErrors(validCtxt,
            xmlSchemaValidityErrorFunc(ErrorFunc),
            xmlSchemaValidityWarningFunc(ErrorFunc),
            logfile);
        ret := xmlSchemaValidateDoc(validCtxt, doc);
        write (ParamStr(2));
        if (ret = 0) then
          writeln (' validates')
        else if (ret > 0) then
          writeln (' fails to validate')
        else
          writeln(' validation generated an internal
error');
        xmlSchemaFreeValidCtxt(validCtxt);
        xmlFreeDoc(doc);
      end;
    end;
    CloseFile(logfile);
    xmlSchemaCleanupTypes();
    xmlCleanupParser();
  end;
end.

////////////////////////////////////////////////////////////
////////////
/////////////////////// 

-----Original Message-----
>Date: Fri, 2 Feb 2007 04:40:00 -0500
>From: Daniel Veillard <veillardredhat.com>
>Subject: Re: [xml] Necessary steps involved in
validating against an
XSD?
>To: "Erik F. Andersen" <eaascott.dk>
>Cc: xmlgnome.org
>Message-ID: <20070202094000.GC17367redhat.com>
>Content-Type: text/plain; charset=us-ascii
>
>On Thu, Feb 01, 2007 at 11:50:23AM +0100, Erik F.
Andersen wrote:
>> Hello!
>> 
>> I have looked at the code in xmllint.c because I
need only the part
that validates a XML against a schema, that is:
>> "xmllint --schema xsdfile.xsd
xmlfile.xml"
>> 
>> Since I'm a Delphi programmer and I'm not too
experienced with C I
find the code in xmllint.c a bit confusing however (and I
cannot debug
either).
>> 
>> I was hoping that maby some kind soul would outline
the necessary
steps involved for me in XSD validation using LIBXML2? I'm
using the
latest version.
>
>  Check the testSchemas.c whose source is available in
the
distribution,
>it should be quite easier to understand.
>
>Daniel
>
>-- 
>Red Hat Virtualization group http://redhat.com/v
irtualization/
>Daniel Veillard      | virtualization library  http://libvirt.org/
>veillardredhat.com  | libxml GNOME XML XSLT toolkit
http://xmlsoft.org/
>http://veillard.com/
| Rpmfind RPM search engine  http://rpmfind.net/


Eric Zurcher
CSIRO Plant Industry
Canberra, Australia
Eric.Zurchercsiro.au 
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xmlgnome.org
http://mai
l.gnome.org/mailman/listinfo/xml

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )