Reputation: 45
I have this XML for an XSD it is all good except for one error:
Ln 25 Col 50 - s4s-elt-invalid-content.1: The content of '#AnonType_tracks' is invalid. Element 'attribute' is invalid, misplaced, or occurs too often.
Here is my XML code:
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="items.xsd">
<item>
<title>Kind of Blue</title>
<priceus>US: $11.99</priceus>
<priceuk>UK: £8.39</priceuk>
<artist>Miles Davis</artist>
<tracks>
<track length="9:22">So What</track>
<track length="5:37">Blue in Green</track>
<track length="11:33">All Blues</track>
<track length="9:26">Flamenco Sketches</track>
</tracks>
</item>
<item>
<title>Blue Train</title>
<priceus>US: $8.99</priceus>
<priceuk>UK: £6.29</priceuk>
<artist>John Coltrane</artist>
<tracks>
<track length="10:39">Blue Train</track>
<track length="9:06">Moment's Notice</track>
<track length="7:11">Locomotion</track>
</tracks>
</item>
</items>
Here is my XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="title" type="xs:string" />
<xs:element name="priceus" type="xs:string" />
<xs:element name="priceuk" type="xs:string" />
<xs:element name="artist" type="xs:string" />
<xs:attribute name="track" type="xs:string" />
<xs:element name="tracks">
<xs:complexType>
<xs:simpleContent>
<xs:attribute ref="track" use="required" />
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title" />
<xs:element ref="priceus" />
<xs:element ref="priceuk" />
<xs:element ref="artist" />
<xs:element ref="tracks"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="items">
<xs:complexType>
<xs:sequence>
<xs:element ref="item" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Upvotes: 2
Views: 2008
Reputation: 111620
In addition to Jim Garrison's correct answer about adding attributes to simple content elements, another problem is that for your XML, track
is an element, not an attribute.
So change,
<xs:attribute name="track" type="xs:string" />
<xs:element name="tracks">
<xs:complexType>
<xs:simpleContent>
<xs:attribute ref="track" use="required" />
</xs:simpleContent>
</xs:complexType>
</xs:element>
to
<xs:element name="track">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="length" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="tracks">
<xs:complexType>
<xs:sequence>
<xs:element ref="track" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
and your XSD will successfully validate your XML.
Upvotes: 3
Reputation: 86774
<xs:simpleContent>
declarations cannot contain <attribute>
declarations, only <restriction>
or <extension>
. Attributes can be specified within the <restriction>
or <extension>
blocks.
See https://www.safaribooksonline.com/library/view/xml-schema/0596002521/re49.html
Upvotes: 3