# XML Schema File that validates my XML File

I just want to know if my schema file is correct:

XML File:

  <?xml version="1.0" encoding="UTF-8"?>
<xs:books xmlns="http://www.w3.com/1999/XML/prjXMLValidation"
xmlns:xs="http://www.w3.org/2001/XMLSchema/prjXMLValidation"
xs:noNamespaceSchemaLocation="collection.xsd">
<xs:book title="A Lesson Before Dying" author="Ernest J. Gaines" version="1">
<xs:stars>
5
</xs:stars>
<xs:description>
This is a story about a young man that was wrongly
accused of killing a white man in the south at a time
when blacks were not given the same rights as whites.
This is the story of how Jefferson becomes a man.
</xs:description>
<xs:thoughts>
Great book!
</xs:thoughts>
</xs:book>
</xs:books>


Here is my XSD File:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3.org/2001/XMLSchema/prjXMLSchema">
<xs:element name="books">
<xs:complexType>
<xs:element name="book">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="stars">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element ref="1" />
<xs:element ref="2" />
<xs:element ref="3" />
<xs:element ref="4" />
<xs:element ref="5" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="description" type="xs:string" />
<xs:element name="thoughts" type="xs:string" />
</xs:sequence>
<xs:attribute name="title" type="xs:string" use="required" />
<xs:attribute name="author" type="xs:string" use="required" />
<xs:attribute name="version" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:schema>

• Well, you may want to include the XSD file collection.xsd ... ;-) – rolfl Feb 11 '14 at 5:01

Overall your structures look OK. Your actual XML document could be indented more neatly, but it's OK.

I would recommend that you change your stars element to have an attribute. Using the PCDATA space is not a great place to put a numeric value.... Actually, I would recommend adding a stars attribute to the thoughts element (seems like a logical place to put it)....

<xs:thoughts stars="5" >
Great book!
</xs:thoughts>


This brings up the validation of the stars value too.... you have broken references in your XSD:

          <xs:complexType>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element ref="1" />
<xs:element ref="2" />
<xs:element ref="3" />
<xs:element ref="4" />
<xs:element ref="5" />
</xs:choice>
</xs:complexType>


The references 1 .... 5 are not declared. This is a problem. But, you should be using references (just not those ones....). You should be separating out your XSD types and then merging them up in complex-types with ref references. You should also be declaring the types outside of their usage... which makes things easier to manage.

Consider the following (which changes the stars validation, and location).

<xs:simpleType name="startype">
<xs:restriction base="xs:string">
<xs:pattern value="[1-5]"/>
</xs:restriction>
</xs:simpleType>

.....
<xs:element name="thoughts">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="stars" type="startype" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

• Sorry about the formatting of my XML file. It was not like that originally, but when I put my code into the site it refused to format correctly. At least my XSD turned out the way I made it. And thank you for your advice! – tylerjholmgren Feb 11 '14 at 11:46