
April 6th, 2008, 09:50 PM
|
|
Contributing User
|
|
Join Date: Jul 2007
Posts: 36
Time spent in forums: 8 h 39 m 34 sec
Reputation Power: 1
|
|
|
Should be a simple problem for someone
Ok I am creating a schema for an XML document that manages an inventory for a CD distributor.
The XML is pretty simple...
Code:
<inventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="music.xsd">
<album category="Modern" albumID="JW1234">
<title>The Noise We Make</title>
<artist>Chris Tomlin</artist>
<tracks length="00:49:05">
<track length="00:04:22">The Noise We Make</track>
<track length="00:05:13">Forever</track>
<track length="00:04:21">Kindness</track>
<track length="00:04:48">America</track>
<track length="00:07:08">The Wonderful Cross</track>
<track length="00:04:26">Captured</track>
<track length="00:03:52">Be Glorified</track>
<track length="00:03:18">Happy Song</track>
<track length="00:04:05">Need You Now</track>
<track length="00:05:35">This is Our God</track>
<track length="00:03:57">Forever (radio remix)</track>
</tracks>
</album>
</inventory>
I have all of my schema down except one part I am very confused about. When I'm defining my <tracks> as a complex datatype I have the element <track> in it as the XML does; however, <track> is also it's own complex datatype because it has an attribute [length]. How would you do this part of the schema because the way I have it gets an error because the element <track> is used to often; because it's both in my TracksType and Tracktype complex datatype definitions. Any help would be very much appreciated. Thanks!
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple types -->
<xs:simpleType name="nameType">
<xs:restriction base="xs:string">
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="lengthType">
<xs:restriction base="xs:string">
<xs:pattern value="\d{2}:\d{2}:\d{2}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="albumIDType">
<xs:restriction base="xs:string">
<xs:pattern value="JW\d{4}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="categoryType">
<xs:restriction base="xs:string">
<xs:enumeration value="Swing"/>
<xs:enumeration value="Modern"/>
<xs:enumeration value="Hip Hop"/>
</xs:restriction>
</xs:simpleType>
<!-- definition of complex types -->
<xs:complexType name="trackType">
<xs:element name="track" type="nameType"/>
<xs:attribute name="length" type="lengthType" use="optional"/>
</xs:complexType>
<xs:complexType name="tracksType">
<xs:sequence>
<xs:element name="track" type="trackType" minOccurs="1" maxOccurs="unbounded"/> <!-- the definition of the "track" element is
using the "trackType" complex type -->
</xs:sequence>
<xs:attribute name="length" type="lengthType" use="optional"/>
</xs:complexType>
<xs:complexType name="albumType">
<xs:sequence>
<xs:element name="title" type="nameType"/>
<xs:element name="artist" type="nameType" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="tracks" type="tracksType"/>
<!-- the definition of the "tracks" element is
using the "tracksType" complex type -->
</xs:sequence>
<xs:attribute name="category" type="categoryType" use="required"/>
<xs:attribute name="albumID" type="albumIDType" use="required"/>
</xs:complexType>
<xs:complexType name="inventoryType">
<xs:sequence>
<xs:element name="album" type="albumType"/>
<!-- the definition of the "album" element is
using the "albumType" complex type -->
</xs:sequence>
</xs:complexType>
<!-- Reference to "inventoryType" to define the
"inventory" element -->
<xs:element name="inventory" type="inventoryType"/>
</xs:schema>
|