Question

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>
Was it helpful?

Solution

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,

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top