Page 1 of 1

Error with XML Schema

Posted: Thu Dec 11, 2008 10:58 pm
by strat1
Hey all, I am having some problems with my schema and was wondering if anyone could help me out. Here are my XML and XSD files, along with the error messages:

XML:

Code: Select all


1	<?xml version="1.0" encoding="utf-8"?>
2
9
16 <music-library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17 xsi:noNamespaceSchemaLocation="musiclibraryschema.xsd" >
18

<!--
19 A music library is zero or more releases
20 followed by zero or more performers
21 -->


22 <!--
23 1) the genre atrribute of a release is optional
24 2) a release has an artist and at least one song
25 3) a song's name attribute is required
26 but its length attribute is optional.
27 -->


28 <!--
29 A genre attribute is optional and can be one of:
30 jazz , soul , altrock , electronic , rb , baroquePop ,
31 indieRock , techno , soundtrack , country , reggae ,
32 altcountry , classical , metal , rock , hiphop , rap ,
33 newage , alternative , pop , elevator , folk
34
35 The id attribute is unique and required for each release
36
37 The artist attribute refers to the artist of this release and is required
38
39 The title is required
40
41 A release has one or more songs. The song length is optional but the song name is required.
42 The song length, if it exists, should be one or more digits followed by a mandatory semicolon,
43 followed by two digits.
44 -->



45 <release id="r4" title="Giant Steps" genre="jazz" artist="jc">
46 <song length="4:02" name="Giant Steps" />
47 <song length="8:15" name="Naima" />
48 </release>
49
50 <release id="r3" title="The Band - Greatest Hits" genre="rock" artist="tb">
51 <song length="4:31" name="Up On Cripple Creek" />
52 <song length="3:32" name="The Night They Drove Old Dixie Down" />
53 <song length="3:41" name="Stage Fright" />
54 </release>
55 <release id="r2" title="Drive In Movie" genre="altcountry" artist="fe">
56 <song length="3:43" name="White Rose"/>
57 <song length="4:16" name="I Like Trains"/>
58 </release>
59
60 <release id="r1" title="Balin" genre="altcountry" artist="fe">
61 <song length="4:14" name="The Rocket"/>
62 <song length="3:45" name="Small Motors"/>
63 <song name="Tin Pot Nelly"/>
64 </release>
65

<!--
66 performers
67
68 A performer is an individual artist or a band
69
70 A performer also has an optional note field.
71
72 A performer has a unique id attribute
73 -->



74 <performer id="jc">
75 <artist>
76 <first>John</first>
77
78 <last>Coltrane</last>
79 </artist>
80 <note>Jazz Saxophonist</note>
81 <rel r="r4" />
82 </performer>
83 <performer id="tb">
84 <band name="The Band">
85

<!--
86 a band has one or more artists
87
88 An artist is a first name, optional middle name, followed by a last name.
89
90 An artist can have zero or more performers they listen to.
91 -->

92
93 <members>
94 <artist>
95 <first>Robbie</first>
96 <last>Robertson</last>
97 <listensto performer="jc" />
98 <listensto performer="fe" />
99 </artist>
100 <artist>
101
102 <first>Richard</first>
103 <last>Manuel</last>
104 </artist>
105 </members>
106 </band>
107 <rel r="r2" />
108 <rel r="r1" />
109 </performer>
110
111 <performer id="fe">
112 <band name="Fred Eaglesmith and the Flathead Noodlers">
113 <members>
114 <artist>
115 <first>Fred</first>
116 <last>Eaglesmith</last>
117 </artist>
118 <artist>
119
120 <first>Willie</first>
121 <middle>P.</middle>
122 <last>Bennett</last>
123 </artist>
124 <artist>
125 <first>Kori</first>
126 <middle>P.</middle>
127
128 <last>Heppner</last>
129 </artist>
130 </members>
131 </band>
132 <rel r="r3" />
133 </performer>
134 </music-library>
Schema:

Code: Select all


<?xml version="1.0" encoding="utf-8"?>

<!--
XML schema to represent a music library
-->

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- MUSIC LIBRARY defined -->
<xs:element name="music-library">
<xs:complexType>
<xs:sequence>
<xs:element ref="release" maxOccurs="unbounded" />
<xs:element ref="performer" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<!-- RELEASE element defined -->
<xs:element name="release">
<xs:complexType>
<xs:sequence>
<xs:element ref="song" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="title" type="xs:string" use="required" />
<xs:attribute name="genre" type="genreType" use="optional" />
<xs:attribute name="artist" type="xs:string" use="required" />
</xs:complexType>
</xs:element>

<!-- SONG element defined -->
<xs:element name="song">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="length" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>

<!-- GENRE types allowed defined -->
<xs:simpleType name="genreType">
<xs:restriction base="xs:string">
<xs:enumeration value="jazz" />
<xs:enumeration value="soul" />
<xs:enumeration value="altrock" />
<xs:enumeration value="electronic" />
<xs:enumeration value="rb" />
<xs:enumeration value="baroquePop" />
<xs:enumeration value="indieRock" />
<xs:enumeration value="techno" />
<xs:enumeration value="soundtrack" />
<xs:enumeration value="country" />
<xs:enumeration value="reggae" />
<xs:enumeration value="altcountry" />
<xs:enumeration value="classical" />
<xs:enumeration value="metal" />
<xs:enumeration value="rock" />
<xs:enumeration value="hiphop" />
<xs:enumeration value="rap" />
<xs:enumeration value="newage" />
<xs:enumeration value="alternative" />
<xs:enumeration value="pop" />
<xs:enumeration value="elevator" />
<xs:enumeration value="folk" />
</xs:restriction>
</xs:simpleType>

<!-- ARTIST element defined -->
<xs:element name="artist">
<xs:complexType>
<xs:sequence>
<xs:element name="first" type="xs:string" minOccurs="1" />
<xs:element name="middle" type="xs:string" minOccurs="0" />
<xs:element name="last" type="xs:string" minOccurs="1" />
<xs:element ref="listensto" />
</xs:sequence>
</xs:complexType>
</xs:element>

<!-- LISTENSTO element defined -->
<xs:element name="listensto">
<xs:complexType>
<xs:attribute name="performer" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>

<!-- PERFORMER element defined -->
<xs:element name="performer">
<xs:complexType>
<xs:choice>
<xs:element ref="artist" />
<xs:element ref="band" />
<xs:element name="note" type="xs:string" />
<xs:element ref="rel" />
</xs:choice>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
</xs:element>

<!-- BAND element defined -->
<xs:element name="band">
<xs:complexType>
<xs:sequence>
<xs:element ref="members" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>

<!-- MEMBERS element defined -->
<xs:element name="members">
<xs:complexType>
<xs:sequence>
<xs:element ref="artist" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<!-- REL element defined -->
<xs:element name="rel">
<xs:complexType>
<xs:attribute name="r" type="xs:string" />
</xs:complexType>
</xs:element>

</xs:schema>
Errors in the XML document:
79: 14 cvc-complex-type.2.4.b: The content of element 'artist' is not complete. One of '{listensto}' is expected.
80: 11 cvc-complex-type.2.4.d: Invalid content was found starting with element 'note'. No child element is expected at this point.
98: 39 cvc-complex-type.2.4.d: Invalid content was found starting with element 'listensto'. No child element is expected at this point.
104: 18 cvc-complex-type.2.4.b: The content of element 'artist' is not complete. One of '{listensto}' is expected.
107: 19 cvc-complex-type.2.4.d: Invalid content was found starting with element 'rel'. No child element is expected at this point.
117: 18 cvc-complex-type.2.4.b: The content of element 'artist' is not complete. One of '{listensto}' is expected.
123: 18 cvc-complex-type.2.4.b: The content of element 'artist' is not complete. One of '{listensto}' is expected.
129: 18 cvc-complex-type.2.4.b: The content of element 'artist' is not complete. One of '{listensto}' is expected.
132: 19 cvc-complex-type.2.4.d: Invalid content was found starting with element 'rel'. No child element is expected at this point.



Could anyone explain what the error messages mean? I thought at first some of the elements were out of order, or some of the 'use' variables were wrong, but I've tried fixing them and it's still not validating.

Re: Error with XML Schema

Posted: Mon Dec 15, 2008 11:59 pm
by george
oXygen presents you in the Model view the content of the current element. Also you can use the Show Definition contextual action (CTRL+Shift+Enter) to get to the schema definition of an element.

Now, back to your actual errors. You have for example:

Code: Select all


<artist>
<first>John</first>
<last>Coltrane</last>
</artist>
while the schema defines artist like:

Code: Select all


<xs:element name="artist">
<xs:complexType>
<xs:sequence>
<xs:element name="first" type="xs:string" minOccurs="1" />
<xs:element name="middle" type="xs:string" minOccurs="0" />
<xs:element name="last" type="xs:string" minOccurs="1" />
<xs:element ref="listensto" />
</xs:sequence>
</xs:complexType>
</xs:element>
As you can see the element listensto is not optional, it needs to appear as the last child of artist. That is what the error message is saying, that the content of artist is not complete without a listensto element.

The second message says that note is not expected at that location in

Code: Select all


<performer id="jc">
<artist>
<first>John</first>
<last>Coltrane</last>
</artist>
<note>Jazz Saxophonist</note>
<rel r="r4"/>
</performer>
Again, if you look in the schema the performer element is defined as

Code: Select all


<xs:element name="performer">
<xs:complexType>
<xs:choice>
<xs:element ref="artist" />
<xs:element ref="band" />
<xs:element name="note" type="xs:string" />
<xs:element ref="rel" />
</xs:choice>
<xs:attribute name="id" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
[code]
That is a choice of either artist, band, note or rel. As you already have artist then none of the other elements is allowed.

To fix these you need to either change the schema or the instance accordingly.

Regards,
George