Jump to content

Question on setting up a new XML file for display on web page


K_Drive

Recommended Posts

I am just learning XML. (I have read the XML tutorial. I am also reading the XSL tutorial.)I am setting up a new XML file with a list of books with their titles, authors, publishing company, etc.Some books have multiple authors or multiple editors.How do I set this up? What will be the consequences down the line of using different techniques?Basically, here is what I have:

<book>   <title>Learning XML</title>   <author>	  <author_first_name>John</author_first_name>	  <author_last_name>Johnson</author_last_name>   </author>   <editor></editor>   <year_published>2005</year_published>   <publishing_company>Red River Publishing</publishing_company>   <number_of_pages>450</number_of_pages>   <edition>4th</edition></book>

What happens when a book has multiple authors or editors?Is it better to to use:

<book>   <title>Learning XML</title>   <author_01>	  <author_first_name>John</author_first_name>	  <author_last_name>Johnson</author_last_name>   </author_01>   <author_02>	  <author_first_name>Frank</author_first_name>	  <author_last_name>Ladder</author_last_name>   </author_02>   <editor></editor>   <year_published>2005</year_published>   <publishing_company>Red River Publishing</publishing_company>   <number_of_pages>450</number_of_pages>   <edition>4th</edition></book>

Or is it better to do something like this:

   <author id="01">	  <author_first_name>John</author_first_name>	  <author_last_name>Johnson</author_last_name>   </author>   <author id="02">	  <author_first_name>Frank</author_first_name>	  <author_last_name>Ladder</author_last_name>   </author>

And, do I need to change the <author_first_name> and <author_last_name> as well?I eventually plan to try to display this XML file on a web page using an XSL file.

Link to comment
Share on other sites

  • If a book has multiple authors, simply define "authors" element to contain them. In other words- group similar elements.
  • There's no need to call an element "author_first_name" if it's always going to be inside an "author" element.
  • Stuff that has functional purpose, but not any informal one, such as IDs, should be included in attributes. Otherwise, you'll need to tell the XPath expression get's a lot complicated. If IDs are based on position (1 for 1st, 2 for 2nd and so on), you might not want to add an ID though. XPath can select things based on their position.
  • I also like to avoid "_" in element names in general, but that's just a personal preference.
  • Every data that is language independant (numeric mostly) should be left independant. For example, turn "4th" to "4", and use XSLT to add "th" in engligh or another suffix for other languages. Another example are month names. Write them in numbers, under their own node (element or an attribute... which one is better is a question of a large debate).
  • If a certain element is empty and doesn't serve any special purpose, the way "editor" is, it should be omitted instead. You can use conditions to perform different actions based on the node's existance, but it's (not hard, but) stupid to perform action based on a test if an element is empty.

So to sum it up:

<book>   <title>Learning XML</title>   <authors>	  <author>		 <firstName>John</firstName>		 <lastName>Johnson</lastName>	  </author>	  <author>		 <firstName>Frank</firstName>		 <lastName>Ladder</lastName>	  </author>   </authors>   <yearPublished>2005</yearPublished>   <publishingCompany>Red River Publishing</publishingCompany>   <pages>450</pages>   <edition>4</edition></book>

With a structure like this, the XPath expressions you'll have become self explaining and straight. For example:Show the authors who's first name is "John".

//book/authors/author[firstName='John']

Show every person that contributed to the book, who's name is "John". This of course applies only if we assume each "editor" element is also going to have "firstName" and "lastName" elements like the "author" element.

//book//*[firstName='John']

Show the title of each book which's pages are less then or equal to 600.

//book/title[pages <= 600]

Show the title of each book with an editor.

//book[editor]/title

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...