Jump to content

Style: multiple elements of the same type or one element with mutlple subelements

Recommended Posts

Hi,The XML DOM Tutorial sample file, books.xml, has a book node with 5 author nodes = multiple elements of the same type: <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> I understand doing it this way but I would tend to group the 5 authors as 5 sub-elements of one author element: <book category="web"> <title lang="en">XQuery Kick Start</title> <author> <data>James McGovern</data> <data>Per Bothner</data> <data>Kurt Cagle</data> <data>James Linn</data> <data>Vaidyanathan Nagarajan</data> </author> <year>2003</year> <price>49.99</price> </book> The pros of doing it the Tutorial's way are a smaller file and slightly easier access to the actual author names.The con is that, to me at least, it makes it harder to see all of the authors - I actually said "a book node with 2 author nodes" before I looked more closely.The pro of doing it my way is that all of the actual author data is in one element separate from the other elements and, for me at least, easier to see.The con is a bigger file and slightly more difficult access to the actual author names.My questions are:is there an XML standard that says the Tutorial's way is THE way it should be done?would someone looking at my XML think it was not up to standard?if there is no standard, which method do you prefer and why?Thanks

Link to post
Share on other sites

There are no rules to XML, besides making sure that the syntax is OK. If instead of calling your nodes <data> you call them <author> and put them in an <authors> element it's just as easy to acess the <author> nodes in both examples. bookElement.getElementsByTagName("author")

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.

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.

  • Create New...