0

i have xsd and xml file. xml parse fine when validation is turned off. but with xsd validation it complains about root element in xsd being null.

  1. my xsd file is having multiple global elements. so basically this can be a problem. i guess from xsd,XOM take root element as null. if you can confirm on it

  2. how to declare root element in xsd file and whats best way to do it, in xsd restricting global elements to just 1 element doesnt look good to me


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.popcornmonsters.com/"
xmlns="http://www.popcornmonsters.com/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">

<xs:element name="address_book" >
<xs:complexType>
<xs:sequence>
<xs:element ref="entry" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="email" type="xs:string"/>
<xs:element name="first_name" type="xs:string"/>
<xs:element name="last_name" type="xs:string"/>

<xs:element name="entry">
<xs:complexType>
<xs:sequence>
<xs:element ref="first_name" minOccurs="0"/>
<xs:element ref="last_name" minOccurs="0"/>
<xs:element ref="email" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

<?xml version="1.0" encoding="UTF-8"?>
<address_book xmlns="http://www.popcornmonsters.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.popcornmonsters.com/address_book.xsd">
<entry>
<first_name>Ken</first_name>
<last_name>Cochrane</last_name>
<email>ken@fakeURL.no</email>
</entry>
<entry>
<first_name>Emily</first_name>
<last_name>Cochrane</last_name>
<email>Emily@fakeURL.no</email>
</entry>
</address_book>
skaffman
  • 398,947
  • 96
  • 818
  • 769
nBhati
  • 23
  • 1
  • 8

2 Answers2

0

Your schema is basically Ok, only that it allows at most one <entry> element; you probably want maxOccurs="unbounded" at that point.

However, to solve your problem we need to know more about how you set up the parsing/validation and what tools you use. If it is xom.nu you're using, make sure you pass a namespace aware, schema validating XMLReader to the Builder instance,

forty-two
  • 12,204
  • 2
  • 26
  • 36
  • yeah i need to change it to maxOccurs to unbounded but thats not problem at this point. for validation i am creating a Builder instance passing TRUE as argument, i guess which sets it to be validating builder. xml file which i pass to builder is having schemaLocation atrribute hence it should be validated . but builder complains about root element being null in schema and xml file has root element as adress_book which is a miss match and hence error. So i guess problem lies in multiple global elements in schema and hence its not picking root element. please share what can be wrong here – nBhati Mar 24 '11 at 18:51
  • I think that creates a parser that validates against DTD. Look here for how to create a parser that validates against a schema: http://www.xom.nu/faq.xhtml#d0e390 – forty-two Mar 25 '11 at 15:20
  • Thanks a ton for pointing this....i was pressing hard on other front with no luck – nBhati Mar 26 '11 at 13:52
-1

Pay attention to the following attribute: xsi:schemaLocation="http://www.popcornmonsters.com/address_book.xsd" it's supposed to be a pair of values: URI and file name. So a space before address_book.xsd is mandatory one: xsi:schemaLocation="http://www.popcornmonsters.com/ address_book.xsd" Without space there is no schema associated with XML document.

  • This doesn't answer the question and is incorrect as both formats are acceptable depending on the circumstances. – psubsee2003 Oct 21 '12 at 09:23