# Libxml 0.3.8 - How to validate document with schema?

**URL:** <https://www.ruby-forum.com/t/libxml-0-3-8-how-to-validate-document-with-schema/70785>\
**Category:** Ruby\
**Created:** [October 2, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/libxml-0-3-8-how-to-validate-document-with-schema/70785 "2006-10-02T23:02:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![churp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@churp](https://www.ruby-forum.com/u/churp)\
**Post date:** [October 2, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/libxml-0-3-8-how-to-validate-document-with-schema/70785/1 "2006-10-02T23:02:07Z")

</div>

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 ?

I have this, with the examples pasted at the end of this post. This is  
from  
the W3Schools example at :  
[http://www.w3schools.com/schema/schema\_example.asp](http://www.w3schools.com/schema/schema_example.asp)

$ irb  
irb(main):001:0\> require ‘rubygems’  
=\> true  
irb(main):002:0\> require ‘xml/libxml’  
=\> true  
irb(main):003:0\> xml = XML::Document.new(‘shiporder.xml’)  
=\> \<?xml version="shiporder.xml"?\>

irb(main):004:0\> sch = XML::Schema.from\_string(File.open('shiporder.xsd  
').read)  
=\> #XML::Schema:0x365c54  
irb(main):005:0\> xml.validate sch  
error – found validity error: no root element  
=\> false  
irb(main):006:0\>

============ shiporder.xml ===============

\<?xml version="1.0" encoding="ISO-8859-1"?\>

John S.  
  
Ola Nordmann

 Langgt 23 4000 Stavanger Norway Empire Burlesque Special Edition 1 10.90 Hide your heart 1 9.90 ============ shiporder.xml ===============

============ shiporder.xsd ===============

\<?xml version="1.0" encoding="ISO-8859-1" ?\>

\<xs:schema xmlns:xs=“[XML Schema](http://www.w3.org/2001/XMLSchema)”\>

\<xs:simpleType name=“stringtype”\>  
\<xs:restriction base=“xs:string”/\>  
\</xs:simpleType\>

\<xs:simpleType name=“inttype”\>  
\<xs:restriction base=“xs:positiveInteger”/\>  
\</xs:simpleType\>

\<xs:simpleType name=“dectype”\>  
\<xs:restriction base=“xs:decimal”/\>  
\</xs:simpleType\>

\<xs:simpleType name=“orderidtype”\>  
\<xs:restriction base=“xs:string”\>  
\<xs:pattern value=“[0-9]{6}”/\>  
\</xs:restriction\>  
\</xs:simpleType\>

\<xs:complexType name=“shiptotype”\>  
xs:sequence  
\<xs:element name=“name” type=“stringtype”/\>  
\<xs:element name=“address” type=“stringtype”/\>  
\<xs:element name=“city” type=“stringtype”/\>  
\<xs:element name=“country” type=“stringtype”/\>  
\</xs:sequence\>  
\</xs:complexType\>

\<xs:complexType name=“itemtype”\>  
xs:sequence  
\<xs:element name=“title” type=“stringtype”/\>  
\<xs:element name=“note” type=“stringtype” minOccurs=“0”/\>  
\<xs:element name=“quantity” type=“inttype”/\>  
\<xs:element name=“price” type=“dectype”/\>  
\</xs:sequence\>  
\</xs:complexType\>

\<xs:complexType name=“shipordertype”\>  
xs:sequence  
\<xs:element name=“orderperson” type=“stringtype”/\>  
\<xs:element name=“shipto” type=“shiptotype”/\>  
\<xs:element name=“item” maxOccurs=“unbounded” type=“itemtype”/\>  
\</xs:sequence\>  
\<xs:attribute name=“orderid” type=“orderidtype” use=“required”/\>  
\</xs:complexType\>

\<xs:element name=“shiporder” type=“shipordertype”/\>

\</xs:schema\>  
============ shiporder.xsd ===============

---

<div class="post-metadata">

**Author:** ![churp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@churp](https://www.ruby-forum.com/u/churp)\
**Post date:** [October 6, 2006, 1:11pm UTC](https://www.ruby-forum.com/t/libxml-0-3-8-how-to-validate-document-with-schema/70785/2 "2006-10-06T13:11:08Z")

</div>

Hi,

On Tue, 2006-10-03 at 06:01 +0900, Peter F. 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-instance](http://www.w3.org/2001/XMLSchema-instance)”

orderid=“889923”

# xsi:noNamespaceSchemaLocation=“shiporder.xsd”\>

# \< … snipped … \>

# 

> 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

---

<div class="post-metadata">

**Author:** ![churp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@churp](https://www.ruby-forum.com/u/churp)\
**Post date:** [October 6, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/libxml-0-3-8-how-to-validate-document-with-schema/70785/3 "2006-10-06T22:35:17Z")

</div>

Ross B. 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 V.

---

<div class="post-metadata">

**Author:** ![churp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@churp](https://www.ruby-forum.com/u/churp)\
**Post date:** [October 6, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/libxml-0-3-8-how-to-validate-document-with-schema/70785/4 "2006-10-06T23:55:29Z")

</div>

On Sat, 2006-10-07 at 05:34 +0900, David V. wrote:

> Ross B. 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 😉

---

<div class="post-metadata">

**Author:** ![churp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f14d63/32.png) [@churp](https://www.ruby-forum.com/u/churp)\
**Post date:** [October 7, 2006, 4:29am UTC](https://www.ruby-forum.com/t/libxml-0-3-8-how-to-validate-document-with-schema/70785/5 "2006-10-07T04:29:48Z")

</div>

Ross B. wrote:

> Someone (me I guess) just has to get the  
> time to make them 😉

Ah yes, the usual 😜

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 V.
