Sha256: b883758fe7ee1d770114131699984265ffe3e0b54450673340f1e5dc8ce4344d

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

require "libxml_test"

xp = XML::Parser.new
xp.string = '<?xml version="1.0" encoding="utf-8"?>

<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
 <orderperson>John Smith</orderperson>
 <shipto>
  <name>Ola Nordmann</name>
  <address>Langgt 23</address>
  <city>4000 Stavanger</city>
  <country>Norway</country>
 </shipto>
 <item>
  <title>Empire Burlesque</title>
  <note>Special Edition</note>
  <quantity>1</quantity>
  <price>10.90</price>
 </item>
 <item>
  <title>Hide your heart</title>
  <quantity>1</quantity>
  <price>9.90</price>
 </item>
</shiporder>
'

doc = xp.parse

schema = XML::Schema.from_string('<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="orderperson" type="xs:string"/>
   <xs:element name="shipto">
    <xs:complexType>
     <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="address" type="xs:string"/>
      <xs:element name="city" type="xs:string"/>
      <xs:element name="country" type="xs:string"/>
     </xs:sequence>
    </xs:complexType>
   </xs:element>
   <xs:element name="item" maxOccurs="unbounded">
    <xs:complexType>
     <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="note" type="xs:string" minOccurs="0"/>
      <xs:element name="quantity" type="xs:positiveInteger"/>
      <xs:element name="price" type="xs:decimal"/>
     </xs:sequence>
    </xs:complexType>
   </xs:element>
  </xs:sequence>
  <xs:attribute name="orderid" type="xs:string" use="required"/>
 </xs:complexType>
</xs:element>

</xs:schema>
')

p schema

if doc.validate_schema(schema) { |message, error| puts "#{error ? 'error' : 'warning'} : #{message}" } 
  puts "validation passed"
else
  puts "validation failed"
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
libxml-ruby-0.5.0.1 tests/schema-test.rb
libxml-ruby-0.3.8 tests/schema-test.rb
libxml-ruby-0.3.8.2 tests/schema-test.rb
libxml-ruby-0.3.8.4 tests/schema-test.rb
libxml-ruby-0.5.0 tests/schema-test.rb
libxml-ruby-0.5.1.0 tests/schema-test.rb
libxml-ruby-0.5.2.0 tests/schema-test.rb