Jump to content

Writing a schema, maybe use <xs:extension base=>


Recommended Posts

Hey folks,I'm following the schema tutorial here on w3schools, I've hit a point where I "think" I know the correct method, but I was hoping some of you experienced folks could have a look at this.I'm trying to write a schema for this XML document.

<minutes>	<entry id="1">		<date>			<day>16</day>			<month>January</month>			<year>2011</year>		</date>		<recap>			<recapheader>Recap of last week:</recapheader>			<recapcontent>Blah de blah de blah</recapcontent>		</recap>		<new>			<newheader>New Points</newheader>			<newcontent>Bluu de bluu de bluu.</newcontent>		</new>	</entry></minutes>

I'm struggling to imagine how to write for these elements. As I understand it, I'd first use complex types and sequences to get entry,data,recap,new defined, then somehow use "xs:extension base" to further define the elements? Could anyone point me on the right track please?I'll post back later when I'm home from work and I get a chance to actually play around with this.Cheers.

Edited by craftid
Link to post
Share on other sites

A "type" in XML Schema, whether it's a simple or complex one defines the contents of a node. Everything within it is just part of that content's specification.You'd have to write a separate complexType for each element, and you can then use certain specifiers (e.g. xs:sequence, xs:all) to arrange them in a certain order.When a certain element or type repeats many times across the document, you can put it aside and reference it, but otherwise, you can embed it directly... here's a partial example for your sample XML to get your started:

<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">	<xs:element name="minutes">		<xs:complexType>			<xs:sequence>				<xs:element name="entry" maxOccurs="unbounded">					<xs:complexType>						<xs:all>							<xs:element name="date" type="date"/>							<xs:element name="recap"/>							<!--minOccurs="0" in case there's nothing new... or am I missing the purpose of this element? -->							<xs:element name="new" minOccurs="0"/>						</xs:all>						<xs:attribute name="id" type="xs:nonNegativeInteger"/>					</xs:complexType>				</xs:element>			</xs:sequence>		</xs:complexType>	</xs:element>	<xs:complexType name="date">		<!--Further specification here, assuming you use dates that need to be stored in this fashion as opposed to simply using xs:date-->	</xs:complexType></xs:schema>

You might want to get yourself a good XML Schema editor... there are a lot of free ones, but the one I'd reccomend is Stylus Studio because of its visual XML Schema editor. It has helped me a lot in figuring out what's right where.

Link to post
Share on other sites

That's great, thank you very much! :) You're exactly correct regarding <xs:element name="new" minOccurs="0"/>, this will be very useful. For the date format, I have to say I followed the w3schools tutorial on XML Attributes, where the "favorite" format for a date is to use all elements instead of attributes. I have no specific need for that format, so I'm happy to play around.I'll put it to practice in a few hours, and report back.Thanks again!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...