Simple XML schema :)

Associate
Joined
30 Sep 2004
Posts
2,301
Location
City of Rain
hello guys, I need some help here :p

The question:

Write an XML Schema equivalent to the following XML DTD (note:
xmlns:xsd="http://www.w3.org/2001/XMLSchema"):

Code:
<!ELEMENT book (title, chapter+)>
<!ATTLIST book
         type (fiction | non-fiction) #REQUIRED>
<!ELEMENT chapter (title, paragraph+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT paragraph (#PCDATA)>

My answer:

Code:
<?xml version ="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	    targetNamespace="http://www.books.org"
	    xmlns="http://www.books.org"
	    elementFormDefault="qualified">
 <xsd:element name="book">
   <xsd:complexType>
     <xsd:sequence>
        <xsd:element ref="title" minOccurs="1" maxOccurs="1"/>
        <xsd:element ref="chapter" minOccurs="1" maxOccurs="unbounded"/>
     </xsd:sequence>
     <xsd:attribute name="type" use="required">
        <xsd:simpleType>	
	 <xsd:restriction base="xsd:string">
            <xsd:enumeration value="fiction"/>
	    <xsd:enumeration value="non-fiction"/>   	
	 </xsd:restriction>
        </xsd:simpleType>
     </xsd:attribute>	
   </xsd:complexType>
 </xsd:element>
 
 <xsd:element name="chapter">
	<xsd:complexType>
	  <xsd:sequence>
	    <xsd:element ref="title"     minOccurs="1" maxOccurs="1"/>
	    <xsd:element ref="paragraph" minOccurs="1" maxOccurs="unbounded"/>
	  </xsd:sequence>
	</xsd:complexType>  
 </xsd:element>
 <xsd:element name="title" type="xsd:string"/>
 <xsd:element name="paragraph" type="xsd:string"/>
</xsd:schema>

is it correct?

cheers :cool:
 
Last edited:
From what I remember of my XML it looks correct to me, it would help if you had a sample XML document that you could use and then use your DTD and Schema to validate the document, you can probably write the XML document as it seems fairly trivial. I know for certain that Netbeans allows you to validate an XML document with a DTD or Schema, if the W3C one doesn't (I'd assume it looks at the DTD or Schema when validating though so it probably does check).
 
@RobH: do you know any website that let me "compare" the DTD/Schema and sample XML data to see if they "match up"?

@Yeggstry: cheers:D its just a question from the pastpaper. Im revising for my exam on Monday :p
 
Cool :) I'd imagine that even if you do make a mistake on the actual exam you'd probably only drop one or two points (assuming its a 5-10 point question I guess).
 
Cool :) I'd imagine that even if you do make a mistake on the actual exam you'd probably only drop one or two points (assuming its a 5-10 point question I guess).

actually its just a 3 mark question :eek: but every point counts in the exam so I need to make sure I do it correctly :p
 
@RobH: do you know any website that let me "compare" the DTD/Schema and sample XML data to see if they "match up"?

Well I know Netbeans (Java IDE) allows you to validate XML against Schema or DTD because that's what we used at Uni. You have to link the Schema to the XML file, this is an example from the W3C website:

<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
 
Back
Top Bottom