[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: libxml 0.3.8 - How to validate document with schema ?

Ross Bamford

10/6/2006 11:10:00 AM

Hi,

On Tue, 2006-10-03 at 06:01 +0900, Peter Fitzgibbons wrote:
> Hello all (Ross),
>
> I'm trying to get libxml to validate an XML::Document using #validate with
> an XML::Schema.
>
> Can one of you describe to me what is wrong ?

> irb(main):003:0> xml = XML::Document.new('shiporder.xml')
> => <?xml version="shiporder.xml"?>
>

Problem here - The argument to XML::Document.new is interpreted as the
XML version to use, rather than as a file to read (notice the XML
version is your filename). Try this instead:

xml = XML::Document.file('shiporder.xml')
# => <?xml version="1.0" encoding="ISO-8859-1"?>
# <shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst...
orderid="889923"
# xsi:noNamespaceSchemaLocation="shiporder.xsd">
# < ... snipped ... >
# </shiporder>

> irb(main):004:0> sch = XML::Schema.from_string(File.open('shiporder.xsd
> ').read)

(side note, I'd just use File.read('shiporder.xsd') here).

> => #<XML::Schema:0x365c54>
> irb(main):005:0> xml.validate sch
> error -- found validity error: no root element

Instead of validate, you need to pass your schema to validate_schema:

sch = XML::Schema.from_string(File.read('shiporder.xsd'))
# => #<XML::Schema:0xb7f098f0>

xml.validate_schema(sch)
# => true

Hope that helps,
Ross

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk


3 Answers

David Vallner

10/6/2006 8:34:00 PM

0

Ross Bamford wrote:
> Problem here - The argument to XML::Document.new is interpreted as the
> XML version to use, rather than as a file to read (notice the XML
> version is your filename).

POLS violation? Seriously, has anyone of you ever seen a XML document of
a version other than 1.0 in the wild, or felt a need to write one?

David Vallner

Ross Bamford

10/6/2006 9:55:00 PM

0

On Sat, 2006-10-07 at 05:34 +0900, David Vallner wrote:
> Ross Bamford wrote:
> > Problem here - The argument to XML::Document.new is interpreted as the
> > XML version to use, rather than as a file to read (notice the XML
> > version is your filename).
>
> POLS violation? Seriously, has anyone of you ever seen a XML document of
> a version other than 1.0 in the wild, or felt a need to write one?
>

Yeah, I agree it's pretty confusing, but for 0.3.8 we tried to keep the
API fairly stable. There's a good sized list of small changes to make
and bugs to fix for 0.4.0... Someone (me I guess) just has to get the
time to make them ;)

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk


David Vallner

10/7/2006 2:28:00 AM

0

Ross Bamford wrote:
> Someone (me I guess) just has to get the
> time to make them ;)
>

Ah yes, the usual ;P

Hey, at least you're in the authority to change them. I should start my
own pet project one of these days as stress relief after having to fix
the umptieth bug someone caused by copy / pasting boilerplate code
around without bothering to check if, let's say, the form validation
he's using makes any sense whatsoever after he goes on and changes all
fields in the form without being able to bitchslap said person.

*ponders joining IntelliJ IDEA and Ruby syntax highlighting in unholy
matrimony*

David Vallner