
In this week we are going to add more complexity to your data structure, and use these data structures to represent more complicated data models. In this week we'll be developing models that go beyond the address book, including recipe books, and ontolgies that represent families and more complicated models.
<?xml version="1.0" ?> <quiz>
<question type="multiple" number="1">
Which form of life lifes under harsh conditions?
<answers>
<answer choice="a" text="bacteria"/>
<answer choice="b" text="fungi"/>
<answer choice="c" correct="true" text="archaea"/>
<answer choice="d" text="animal"/>
</answers>
</question>
</quiz>
<?xml version="1.0" ?> <album>
<image>
<year_taken>083079</year_taken>
<location>London, England</location>
<src>http://www.familyimage.com/23456/21</src>
</image>
<image year_taken="082879" location="Eastborn, England" src="http://www.familyimage.com/23456/21"/> </album>
<?xml version="1.0" encoding="UTF-8"?> <recipe_counting>
<ingredients>
<ingredient_1>ingredient one</ingredient_1>
<ingredient_2>ingredient two</ingredient_2>
<ingredient_3>ingredient three</ingredient_3>
<ingredient_4>ingredient four</ingredient_4>
<ingredient_5>ingredient five</ingredient_5>
</ingredients>
<steps>
<step_1>this is step one</step_1>
<step_2>this is step two</step_2>
<step_3>this is step three</step_3>
<step_4>this is step four</step_4>
<step_5>this is step five</step_5>
</steps>
</recipe_counting>
<?xml version="1.0" encoding="UTF-8"?> <recipe_attributes>
<ingredients>
<ingredient item="1">ingredient one</ingredient>
<ingredient item="2">ingredient two</ingredient>
<ingredient item="3">ingredient three</ingredient>
<ingredient item="4">ingredient four</ingredient>
<ingredient item="5">ingredient five</ingredient>
</ingredients>
<steps>
<step number="1">this is step one</step>
<step number="2">this is step two</step>
<step number="3">this is step three</step>
<step number="4">this is step four</step>
<step number="5">this is step five</step>
</steps>
</recipe_attributes>
There are no rules about when to use empty, and when to use nested elements. My experience is that empty elements are handy in HTML, but in XML you should try to avoid them. Use nested elements if the information feels like data.
The following is an example of a nested model.
<?xml version="1.0" encoding="UTF-8"?>
<Family>
<Parent>
<Type>Father</Type>
<Name>Dad</Name>
</Parent>
<Parent>
<Type>Mother</Type>
<Name>Mom</Name>
</Parent>
<Child>
<Type>Brother</Type>
<Name>John</Name>
</Child>
<Child>
<Type>Sister</Type>
<Name>Sue</Name>
</Child>
</Family>
The following is an example of an empty model.
<?xml version="1.0" encoding="UTF-8"?>
<Family>
<Parent type="father" name="Dad"/>
<Parent type="mother" name="Mom"/>
<Child type="brother" name="John"/>
<Child type="sister" name="Sue"/>
</Family>
The following is an example of a mixed model which combines nested with empty elements.
<?xml version="1.0" encoding="UTF-8"?>
<Family>
<Parent type="father">
<Name>Dad</Name>
</Parent>
<Parent type="mother">
<Name>Mom</Name>
</Parent>
<Child type="brother">
<Name>John</Name>
</Child>
<Child type="sister">
<Name>Sue</Name>
</Child>
</Family>
In summary here is a list of some of the problems with using the empty elements (attributes) model .
If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data. An exception is to use an attribute as an id which is just a counter. In the address book it would be used to count the records, each address book entry. In this case the id is being used as metadata, providing information about the data, which is a great way to use attributes.
<?xml version="1.0" encoding="UTF-8"?>
<!--address book using nested elements-->
<address_book>
<record ID="1">
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
<address>
<street_address>street address goes here</street_address>
<street_address_detail>apartment number goes here</street_address_detail> <city>city goes here</city>
<state>state goes here</state>
<zipcode>zipcode goes here</zipcode>
</address>
<contact>
<home_phone>home phone goes here</home_phone>
<work_phone>work phone goes here</work_phone>
<cell_phone>cell phone goes here</cell_phone>
<fax_number>fax number goes here</fax_number>
<email_address>email address goes here</email_address>
</contact>
<comments>
<misc_comments>comments go here</misc_comments>
</comments>
</record>
<record ID="2">
<name>
<first_name>first name</first_name>
<middle_name>middle name</middle_name>
<last_name>last name</last_name>
<nick_name>nick name</nick_name>
</name>
<address>
<street_address>street address goes here</street_address>
<street_address_detail>apartment number goes here</street_address_detail> <city>city goes here</city>
<state>state goes here</state>
<zipcode>zipcode goes here</zipcode>
</address>
<contact>
<home_phone>home phone goes here</home_phone>
<work_phone>work phone goes here</work_phone>
<cell_phone>cell phone goes here</cell_phone>
<fax_number>fax number goes here</fax_number>
<email_address>email address goes here</email_address>
</contact>
<comments>
<misc_comments>comments go here</misc_comments>
</comments>
</record>
</address_book>