0

I am trying to Translate a small book into an XML document. The book has the following structure,

Title

Acknowledgement

Copyright

Collaborating Members

Publishers name and address 

Table of contents

Preface

Chapter (optional) and under chapter there are sections (must)

I am trying to decide among two options: Can I design schema document with one major tag and everything defined in it with strict sequence, such as,

<xsd:element name="theBook">
 <xsd:complexType>
  <xsd:sequence>
   <xsd:element name="bookTitle" type="xsd:string"/>
   <xsd:element name="bookAck" type="xsd:string"/> 
    <!-- and so on -->
  </xsd:sequence>
 </xsd:complexType>
</xsd:element>

or can I just define them individually? Such as,

<xsd:element name="theBook">
</xsd:element>
<xsd:element name="bookTitle" type="xsd:string">
</xsd:element>
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Gravity M
  • 1,485
  • 5
  • 16
  • 28
  • 1
    Why not just use DocBook? – Robby Cornelissen Jul 10 '14 at 01:41
  • @RobbyCornelissen the Book is an example. But I am trying to design a schema for a book-like other document. – Gravity M Jul 10 '14 at 02:07
  • 2
    I do get cross when people close "design" questions as being opinion-based. Good engineering design is an entirely legitimate subject for discussion, just as good coding is, and to dismiss questions about design as being "opinion-based" in my view shows a poor understanding of how software design should be approached. – Michael Kay Jul 10 '14 at 07:30
  • @MichaelKay I completely agree. Given the high standards of Stackoverflow questioning, I spent time to carefully put this design question. I was looking to get some suggestion either to enforce a structure or liberalize it. It does show the poor understanding of software where it is not always code but a sound design. – Gravity M Jul 10 '14 at 14:33

2 Answers2

1

There are a number of different patterns available for designing XML schema, each with their own pros and cons.

However, with regards to your question: "should I have multiple defined top-level elements or just one?":

Any top level elements, beingthose defined as direct children of the xs:schema element, can be used as root-level elements in a document. So using your second example:

<xs:schema>
  <xsd:element name="theBook">
  </xsd:element>
  <xsd:element name="bookTitle" type="xsd:string">
  </xsd:element>
</xs:schema>

I could have 2 different documents one with book as the root and another with bookTitle as the root. Obviously, a bookTitle can't really be a root element, which restricts your choice here. But other times it is less obvious.

For example, you could have instead:

<xs:schema>
  <xsd:element name="theBook">
  </xsd:element>
  <xsd:element name="chapter">
  </xsd:element>
</xs:schema>

Here, each chapter could be its own XML document, and then a book could have a URI reference to each of its chapters, which might be useful.

As for which is best - that's an architectural decision and is up to you.

-1

I would recommend your schema define only one root element, so that it can be parsed as a document by a parser. Your xml document that conforms to the schema would look like:

<theBook>
  <bookTitle>title</bookTitle>
  <bookAck>ack</bookAck>
  ...
</theBook>

You may be thinking of having more than one root element in your document. This is not a good idea.

The alternative and have your book still be an xml document that could be parsed would be having multiple xml documents that each validate with your schema but are fragments of your book (perhaps by chapters).

If the book is very large or you want multiple people updating individual paragraphs simultaneously, you'll want fragments, but you originally stated in your question that you wanted an xml document and were not clear if having multiple was ok. So with that, I recommend one root element in your schema.

Community
  • 1
  • 1
paquetp
  • 1,639
  • 11
  • 19
  • An XML schema can have multiple top level defined in a schema, this has other considerations, but it will still validate as the document can only have one. –  Jul 10 '14 at 02:15
  • But the op said "an xml document", not documents. I thought maybe the op may not have been aware that an xml document can have only one root. But thanks for the downvote. – paquetp Jul 10 '14 at 12:04
  • @Lego clarified my answer as to why i choose the design based on your comment. I like how SO can motivate the best answers out of people, because I'm hoping this may undo my downvote. – paquetp Jul 10 '14 at 12:35