Hi,
I'm rather new to dealing with XML, so I may be way off on my general
idea here. I need to specify an XML schema for transferring data
between two companies. I'd like to use a ruby script to check that a
given XML document validates against the schema definition. So far
I've found a way to do it in perl, but I don't really want to use
perl..
use XML::SAX::ParserFactory;
use XML::Validator::Schema;
$validator = XML::Validator::Schema->new(file => 'note.xsd');
$parser = XML::SAX::ParserFactory->parser(Handler => $validator);
eval { $parser->parse_uri('note.xml') };
die "File failed validation: $@" if $@;
That seems to work for the few tests I've run. How can I do this in
ruby? I've tried messing around with REXML a bit, but can't figure
out how the validation works if it can even be used in this way.
For example, the perl script gives me this error when I have an
invalid child element:
"File failed validation: Found unexpected <spider> inside <soup>.
This is not a valid child element."
Here's the schema:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="soup">
<xs:complexType>
<xs:sequence>
<xs:element name="type" type="xs:string"/>
<xs:element name="chef" type="xs:string"/>
<xs:element name="spiciness" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And the XML file itself:
<?xml version="1.0"?>
<soup
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="soup.xsd">
<type>Habanero</type>
<chef>Joe Chef</chef>
<spiciness>Really hot</spiciness>
<spider>blah</spider>
</soup>
Thanks,
Bryan
_______________________________________________
PDXRuby mailing list
[email protected]
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby