Saturday, May 21, 2011

XML: Recipe Document (part 1)

So, in order to teach myself XML, this is the situation I have come up with in order to create and use all the files and techniques described in the previous post. The situation is we would like to create a datatype that stores the information about a recipe. Now, first we should try to think of the things that a recipe might contain, and we should probably also decide the cardinality of these things, that is, how many of each are needed.


First things first, A recipe should have a name. That is a definite must. Since this is a must, we say that there is a 1:1 relationship between the two (As an aside, there is technically a Many:One relationship from Name:Recipe. It is possible, for example, to find many different recipes online for Apple Pie, but each recipe only has one name... unless we would like to be pedantic and say, for example, the name is different depending on where you are from... For example a recipe with a name of French Fries might have a name of Chips in different parts of the English speaking world, or Pomme Frites where French is the native language. For our purposes, we will say that there is only one name for every recipe for now.)


What about an Author? Some recipes have one author; some recipes have many authors, some recipes have no Author. Some people that are authors have written no recipes, some people have written one recipe, and some have written many recipes. What we have is a Many:Many relationship between Authors and Recipes.


Going along, we might say that a recipe has a preparation time, it might have a list of tools we need, sometimes recipes have illustrations if they are more difficult. In terms of defining an XML document, it is important to decide which of these details about a recipe for example, or any other piece of interesting information is important to keep, what is its relationship to other interesting or important pieces of information, and what is the cardinality of that relationship.


Cardinality of relationships is how much of something is related to something else. For example, how many ingredients do recipes have? Do any recipes have 0 ingredients? No, I can't think of any recipes that contain 0 ingredients. Do Any recipes have 1 ingredient? If you want, you could say that the recipe for toast has one ingredient. Do some recipes have more than one ingredient? I can think of many recipes that have more than one ingredient; most do. So, for the sake of this document, we could say that a recipe requires at least one ingredient and can have many more.


For each of the basic cardinal relationships there is generally a short hand symbol for the amount required on each side of the relationship. The table below summarizes most of these shorthands.










SymbolCardinalityOptionality
1,2,3, etc...Specified by the numberMandatory
*0 or moreOptional
+1 or moreMandatory
?0 or 1Optional


Additionally, these can be combined in ways that make sense, for example, if it makes sense to require two or more references on a application for a job, you might describe this as "2+"



After spending a little bit of time coming up with what might be a reasonable amount of information to want to store about a particular recipe, pretty much following the above process, I used erviz to make a diagram. (As of the time of my writing of this post, it seems the main erviz website is down, not sure where you might be able to get the software currently.) You can see what I have with the below link



This will be the reference document I will use to define the Schema of the final recipe XML document.


EDIT: This is why the planning process and having a diagram can be helpful. After taking a break from the brainstorming process, I realized an oversight, the oversight being that some recipes sometimes have optional ingredients, and sometimes they have ingredients that are added "to taste." Also, for a certain purpose, it seems like a good idea to optionally relate steps to ingredients, for example when not all of a particular ingredient is used at a certain step (for example, using some eggs in step one, some eggs in step five.) The chart looks a tiny bit more complicated, but it does allow me to learn an additional element of XSD design down the way. New Diagram below.




No comments:

Post a Comment