antovinraj has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am using the perl and generating the xml file and i have to vaildate the xml file using the xsd document. How I can generate the xsd doument in perl and how to vaildate the xml file datas while saving in the xml file.

Replies are listed 'Best First'.
Re: How to use xml schema in perl
by gellyfish (Monsignor) on Aug 11, 2006 at 09:01 UTC

    I think you are talking in part about "XML Schema Inference", that is generating an XSD schema from an existing instance of XML. As far as I know there is no module on CPAN that provides this facility (though I would be delighted to be proved wrong.) The basic problem of schema inference (or extraction if you would rather) is described in this paper.

    Personally if I need to generate a schema from an existing XML document I use the xsd.exe that is part of the .NET framework, this is also available for mono if you are not on Windows and the mono implementation is described here.

    As for validating an instance against an existing schema, you have a choice - you can use XML::Validator::Schema or the facilities in XML::LibXML among others that you will have seen when you performed the search on CPAN as davorg suggested.

    /J\

      Hi again,

      My two cents on the validation topic.

      I've tried XML::Validator::Schema lately and was not quite happy with it. To be fair, I don't remember what was actually the blocking item (maybe something like inclusion within the Schema itself). Anyway, it seems like nothing happened on this module for two years.

      XML::LibXML on the other hand only validates againts DTD and Relax-NG kind of schemas, although this is a good DOM library.

      Cheers,

      Xavier
        XML::LibXML has been able to validate schemas for a while now (see below)
Re: How to use xml schema in perl
by davorg (Chancellor) on Aug 11, 2006 at 07:57 UTC

    Did you try searching CPAN for xml schema or xsd?

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to use xml schema in perl
by astroboy (Chaplain) on Aug 11, 2006 at 17:47 UTC

    Validation of XML Schemas is covered in the Perl XML FAQ

    use XML::LibXML; my $schema_file = 'po.xsd'; my $document = 'po.xml'; my $schema = XML::LibXML::Schema->new(location => $schema_file); my $parser = XML::LibXML->new; my $doc = $parser->parse_file($document); eval { $schema->validate($doc) }; die $@ if $@; print "$document validated successfully\n";
    You may need to update the libxml2 library on your system if it is quite old (and then rebuild XML::LibXML::Common and XML::LibXML)
Re: How to use xml schema in perl
by maspalio (Scribe) on Aug 11, 2006 at 13:27 UTC
    Hi,

    Dunno about schema generation. It seems like you do not want to generate the XSD itself but rather use it as part of a validation scheme.

    A good validator is found in XML::Xerces module. But beware, you'll have to first compile Xerces-C library as a back-end. Then, you can use the following code sample for validation purposes:

    use strict; use XML::Xerces; XML::Xerces::XMLPlatformUtils::Initialize (); my $parser = XML::Xerces::SAXParser->new; # SAXParser or XercesDOMPa +rser my $options = { setDoNamespaces => 1, setDoSchema => 1, setErrorHandler => XML::Xerces::PerlErrorHandler-> +new, setExitOnFirstFatalError => 0, setExternalSchemaLocation => 'http://www.spiritconsortium.or +g/XMLSchema/SPIRIT/1.2 /data/101/users/xavier_data/soc/BFD/Schema-1.2 +/index.xsd', setValidationConstraintFatal => 0, setValidationSchemaFullChecking => 1, setValidationScheme => $XML::Xerces::AbstractDOMParser +::Val_Always, }; $parser->$_ ( $options->{$_} ) for keys %$options; eval { $parser->parse ( shift ) }; XML::Xerces::error ( $@ ) if $@; XML::Xerces::XMLPlatformUtils::Terminate ();
    I'm using here cheesy setExternalSchemaLocation scheme because in my example, SPIRIT Schema URL is dead and I had to install the XSD files locally.

    Hope this helps,

    Cheers,

    Xavier
Re: How to use xml schema in perl
by Khen1950fx (Canon) on Aug 12, 2006 at 23:07 UTC