Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Syntax - Writing XML and Designing DTD's

Similar presentations


Presentation on theme: "XML Syntax - Writing XML and Designing DTD's"— Presentation transcript:

1 XML Syntax - Writing XML and Designing DTD's

2 HTML – 1st Example <html><head><title>Chocolate Cake</title><body> <b>Ingredient List</b><hr /> <br>2 cups flour <br>1 cup sugar <br>2 bars chocolate <br>1 cup milk <br><br><b>Instructions</b> <hr><br>Mix flour, sugar and milk <br>Eat chocolate <br>Bake at 400 degrees </body></html> Ingredients and Instructions marked by <b> Bold tag, Each ingredient / instruction distinguishable only by the new line <br /> Break tag.

3 XML Document Structure
Text file containing Elements, Attributes & Text <?xml version=“1.0” ?> <Recipe name=“Chocolate Cake” type=“Desert” > <IngredientList> <Ingredient>2 cups flour</Ingredient> <Ingredient>1 cup sugar</Ingredient> </IngredientList> <Instruction>Sift the flour</Instruction> </Recipe> - XML is just the data, no presentation instructions. Later we’ll see how the presentation of the data is taken care of. Show the elements, the attributes and the text Point out the hierarchical / tree structure of the document

4 XML Document Structure
Text file containing Elements, Attributes & Text <?xml version=“1.0” ?> <Recipe name=“Chocolate Cake” type=“Desert” > <IngredientList> <Ingredient>2 cups flour</Ingredient> <Ingredient>1 cup sugar</Ingredient> </IngredientList> <Instruction>Sift the flour</Instruction> </Recipe> - XML is just the data, no presentation instructions. Later we’ll see how the presentation of the data is taken care of. Show the elements, the attributes and the text Point out the hierarchical / tree structure of the document

5 10 Rules – Well Formed XML 1. Must start with XML declaration
<?xml version=“1.0” ?> In order for XML to be used, it has to be “WELL FORMED”. This means it is in a structure that makes it readable by an xml program.

6 2. Must be only one document element
Valid Example(s) <?xml version=“1.0” ?> <recipe> </recipe> or <recipeBook> <recipe></recipe> </recipeBook> Invalid Example <?xml version=“1.0”?> <recipe> </recipe> This highlights also the hierarchical / tree structure of all xml documents

7 3. Match opening & closing tags
Carry over from html origins <hr> <p> or <bold><italic></bold></italic> Browsers forgive, XML Parsers do NOT <p></p> or <br /> <bold><italic></italic></bold> <recipe></recipe> HTML doesn’t care if you close your tags, or the order in which you close them. XML is much fussier, you must close ALL tags, you must close them in the hierarchical order.

8 4. Comments allowed, but not inside attribute or element tag
<!-- Isn’t XML really cool? --> <!-- Just like being a student!!! --> Comments can also extend across multiple lines.

9 5. Elements and Attributes must start with a letter
<Recipe> OK <Second third=“false”> OK <2nd> INVALID <Recipe 2nd=“true”> INVALID Comments can also extend across multiple lines.

10 6. Attributes must go in the opening tag
Valid: <recipe name=“Chocolate Cake” category=“Desert”></recipe> Invalid: <recipe></recipe name=“Chocolate Cake”>

11 7. Attributes must be enclosed in matching quotes
Can use either single or double quotes but must use same type to start and end attribute value Name=“Australian Computer Society” Name=‘Australian Computer Society’

12 Let’s finish these rules!
8. Only simple text for attributes, no nested values. Nesting is allowed in elements, not in attributes. 9. Use < & > " and &apos; for special characters. < & > “ ‘ 10. Write empty elements using <recipe /> syntax if no nested values, can still have attributes in tag <recipe type=“desert” />.

13 With these 10 rules, we have a “Well Formed” xml document
It means the xml can be read, processed or parsed. Doesn’t mean the structure makes sense. <recipe model=“Holden”> <chapter></chapter> <engine cylinders=“4”></engine> <recipe> All XML must be well formed, if not well formed, can’t be treated as XML, can’t be processed / parsed. 2nd part of xml basics is validating the xml document. This is optional and allows us to ensure the document follows a specific structure.

14 Examples Buggy dictionary Non-buggy dictionary FIDA

15 DTD – Document Type Definition
Allows us to define the exact elements and attributes for the document These effectively become the rules of our own markup language, the extensible part of xml DTD – really only defines the structure, limited in what you can validate in regards to the text values of the element or attribute.

16 Recipe DTD <!ELEMENT Recipe (Name, Description?, Ingredients?, Instructions?)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Ingredient (Qty, Item)> <!ELEMENT Qty (#PCDATA)> <!ATTLIST Qty unit CDATA #REQUIRED> <!ELEMENT Item (#PCDATA)> <!ATTLIST Item optional CDATA “0” isVegetarian CDATA “true”>

17 Elements Basic rules Start tag <tag_name> and end tag </tag_name> Tags must be nested <tag1><tag2>…</tag2></tag1> Tags may be empty (no enclosed data) <empty_tag/> Whitespace in element content usually ignored <section><p> … </p></section> <section> <p> … </p> </section>

18 Element Declarations Used to define new elements and their content
<!ELEMENT name (#PCDATA)>  <name> … </name> Empty element has no content <!ELEMENT name EMPTY>  <name/> When children allowed - any or model group <!ELEMENT name ANY> <!ELEMENT person (name, *)>

19 Model Groups Used to define content of elements
<!ELEMENT person (name, *)> Used to define hierarchies of elements <!ELEMENT name (fname, surname)> <!ELEMENT fname (#PCDATA)> <!ELEMENT surname (#PCDATA)> <!ELEMENT (#PCDATA)> Control organisation of elements Sequence connector - ',' - (A, B, C) [then] Choice connector - '|' - (A | B | C) [or]

20 Model Group Quantity Indicators
Describe constraints on elements in DTD A? May occur [0..1] A+ Must occur [1..*] A* May occur [0..*] A | B Either A or B A, B A followed by B (A, B)+ ((A,B?) | C+)*

21 Attributes Provide additional information about an element
Enclosed by quotes - either " or ' Case-sensitive May be character data or tokenized value="Blue Peter" (character data) value = "blue" (single token) value = "red green blue" (tokens) Values may be enumerated or defaulted (DTD)

22 Attribute Declarations
Attributes can be attached to elements Declared separately in ATTLIST declaration <!ATTLIST tag … > Rest of definition specifies attribute name attribute type default value

23 Attribute Names and Types
<!ATTLIST tag nme type default> <!ATTLIST tag first_attr … secon_attr … third_attr … > Attribute types CDATA NMTOKEN NMTOKENS ENTITY ENTITIES ID IDREF IDREFS NOTATION name group

24 Attribute Types CDATA NMTOKEN ID NMTOKENS IDREF ENTITY IDREFS ENTITIES
Character data NMTOKEN Single token NMTOKENS Multiple tokens ENTITY Attribute is entity ref ENTITIES Multiple entity ref's ID Unique ID IDREF Match to ID IDREFS Match to multiple ID's NOTATION Describe non-XML data Name group Restricted list

25 Attribute Types CDATA NMTOKEN ID NMTOKENS IDREF ENTITY IDREFS ENTITIES
<!ATTLIST person name CDATA … > NMTOKEN <!ATTLIST mug color NMTOKEN … > NMTOKENS <!ATTLIST temp values NMTOKENS … > ENTITY <!ATTLIST person photo ENTITY … > ENTITIES <!ATTLIST album photos ENTITIES …> ID <!ATTLIST person id ID … > IDREF <!ATTLIST person father IDREF … > IDREFS <!ATTLIST person children IDREFS … > NOTATION <!ATTLIST image format NOTATION (TeX|TIFF) …> Name group <!ATTLIST point coord (X|Y|Z) … >

26 Attribute Types CDATA NMTOKEN ID NMTOKENS IDREF ENTITY IDREFS ENTITIES
name = "Tom Jones" NMTOKEN color="red" NMTOKENS values=" " ENTITY photo="MyPic" ENTITIES photos="pic1 pic2" ID ID = "P09567" IDREF IDREF="P09567" IDREFS IDREFS="A01 A02" NOTATION FORMAT="TeX" Name group coord="X"

27 Default Attribute Values
Can specify a default attribute value for when its missing from XML document, or state that value must be entered #REQUIRED Must be specified #IMPLIED May be specifed "default" Default value if unspecified #FIXED Only one value allowed <ATTLIST tag name type default> <!ATTLIST seqlist sepchar NMTOKEN #REQUIRED type (alpha|num) "num"

28 Declarations Instructions for the XML processor
Format - <! … > or <! … [<! … >]> Document type - <!DOCTYPE … > Character data - <![CDATA[ … ]]> Entities - <!ENTITY … > Notation - <!NOTATION … > Element - <!ELEMENT … > Attributes - <!ATTLIST … > <![INCLUDE[…]]> and <![IGNORE[…]]>

29 Document Type Declaration
Identifies the name of the document root element <!DOCTYPE My_XML_Doc> May also add entity definitions and DTD <!DOCTYPE My_XML_Doc [ … ] > <My_XML_Doc> ... </My_XML_Doc>

30 Comment Declaration Comments are not considered part of XML document and should not be published <!-- A comment --> Cannot have additional '--' in comment Cannot embed inside other declarations

31 Character Data Declaration
For occasions when text must contain uninterpreted markup characters Press <<<ENTER>>> <![CDATA[Press <<<ENTER>>>]]>

32 Processing Instructions
Information required by an external application Processing Instructions Format - <? … ?> XML PI - <?xml version='1.0’ ?> Confusingly, this is called the XML declaration, but is a processing instruction

33 Entities XML document may be distributed among a number of files
Each unit of information is called an entity Each entity has a name to identify it Defined using an entity declaration Used by calling an entity reference

34 When to use Entities Use an entity when the information
Is used in several places May be represented differently Is part of a larger document that needs to be split up to be manageable Conforms to a data format other than XML

35 Types of Entity General Entity Internal Entity Parameter Entity
Referred to in XML document Parameter Entity Referred to in markup declarations in DTD Internal Entity Stored in main document Text content only External Entity Stored externally to the main document Text or binary Can use to group many internal entities together

36 General Entities Declared in 'Document Type Declaration'
<!DOCTYPE My_XML_Doc [ <!ENTITY name "replacement"> ]> <!ENTITY xml "eXtensible Markup Language"> The &xml; includes entities The eXtensible Markup Language includes entities

37 Parameter Entities Declared in 'Document Type Declaration'
<!DOCTYPE My_XML_Doc [ <!ENTITY % name "replacement"> ]> <!ENTITY % param "(para | list)"> <!ELEMENT section (%param;)*>

38 External Entities External Text Entities External Binary Entities
Location specified with SYSTEM keyword <!ENTITY ent SYSTEM "/ENTS/MYENT.XML"> May specify with public identifier <!ENTITY ent PUBLIC "-//EBI//ENTITIES ents//EN" … > External Binary Entities Need to identify format of data - NDATA <!ELEMENT pic EMPTY> <!ATTLIST pic name ENTITY #REQUIRED> <!ENTITY photo SYSTEM "/ENTS/photo.tif" NDATA TIFF> Referenced by empty element A photograph <pic name="photo"/>.

39 Restrictions on Entities
General text entities Can appear in element content <para> … &ent; … </para> Can appear in attribute value <para name="&ent;"> … </para> Can appear in internal entity content <!ENTITY cod "&ent;"> Cannot appear in other parts of DTD

40 Restrictions on Entities (2)
Binary entities If entity content is not XML, the entity cannot be used as a textual reference Error - <!ELEMENT sec (para|&photo;)> Error - <para> &photo; </para> Binary entity can only appear as an attribute of type ENTITY <!ENTITY photo SYSTEM "photo.tif" NDATA TIFF> … <!ELEMENT pic (#PCDATA)> <!ATTLIST pic name ENTITY #REQUIRED>

41 Parameter Entities Use parameter entities within DTD
<!ENTITY % common "(para|list|table)"> <!ELEMENT chapter ((%common;)*, section*)> <!ELEMENT section (%common;)*> Safest to include parentheses in entity definition and around entity reference

42 Putting it all together...
Have now been introduced to the main components and rules of XML and DTD’s Entities, elements, declarations, processing instructions, attribute lists Use all these components in the 'Document Definition Type' (DTD) to specify the rules about the format of the XML document


Download ppt "XML Syntax - Writing XML and Designing DTD's"

Similar presentations


Ads by Google