craftid Posted January 16, 2011 Share Posted January 16, 2011 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. Link to comment Share on other sites More sharing options...
boen_robot Posted January 16, 2011 Share Posted January 16, 2011 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 comment Share on other sites More sharing options...
craftid Posted January 16, 2011 Author Share Posted January 16, 2011 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 comment Share on other sites More sharing options...
craftid Posted January 16, 2011 Author Share Posted January 16, 2011 Thank you boen_robot, this is just what I needed to get me going.Very well explained! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.