tutorial 3: xml creating a valid xml document. 2 creating a valid document you validate documents to...
out of 52
Post on 25-Dec-2015
Embed Size (px)
- Slide 1
- Tutorial 3: XML Creating a Valid XML Document
- Slide 2
- 2 Creating a Valid Document You validate documents to make certain necessary elements are never omitted. For example, each customer order should include a customer name, address, and phone number. A document is validated to prevent errors in their content or structure. An XML document can be validated using either DTDs (Document Type Definitions) or schemas.
- Slide 3
- Customer orders table
- Slide 4
- David Lynn (315) 555-1812 firstname.lastname@example.org 8/1/2008 DCT5Z SM128 RCL
- Slide 5
- The structure of the orders.xml document
- Slide 6
- DTD statements are inserted here the root element of the document must match the root element listed in the DOCTYPE declaration Writing the Document Type Declaration the root element
- Slide 7
- 7 Declaring a DTD A DTD can be used to: ensure all required elements are present prevent undefined elements from being used enforce a specific data structure specify the use of attributes and define their possible values define default values for attributes describe how the parser should access non- XML or non-textual content
- Slide 8
- 8 Declaring a DTD A document type definition is a collection of rules or declarations that define the content and structure of the document. A document type declaration attaches those rules to the documents content. You create a DTD by first entering a document type declaration into your XML document.
- Slide 9
- 9 Declaring a DTD While there can only be one DTD per XML document, it can be divided into two parts: an internal subset and an external subset. An internal subset is declarations placed in the same file as the document content. An external subset is located in a separate file.
- Slide 10
- 10 Declaring a DTD To declare an internal DTD subset, use: Where root is the name of the documents root element, and declarations are the statements that comprise the DTD.
- Slide 11
- To declare an external DTD subset with a system or public location, use: or id is a text string that tells an application how to locate the external subset uri is the location and filename of the external subset Unless your application requires a public identifier, you should use the SYSTEM location form.
- Slide 12
- A DOCTYPE declaration can indicate both an external and an internal subset. The syntax is: or
- Slide 13
- 13 Declaring a DTD The real power of XML comes from an external DTD that can be shared among many documents. If a document contains both an internal and an external subset, the internal subset takes precedence over the external subset if there is a conflict between the two. This way, the external subset would define basic rules for all the documents, and the internal subset would define those rules specific to each document.
- Slide 14
- Combining an External and Internal DTDs
- Slide 15
- 15 Declaring Document Elements In a valid document, every element must be declared in the DTD. An element (type) declaration specifies the name of the element and indicates what kind of content the element can contain. The syntax is: Where element is the name of the element and content-model specifies what type of content the element contains. The element name is case sensitive
- Slide 16
- 16 Five different types of element content for content-model ANY - No restrictions on the elements content. EMPTY - The element cannot store any content. #PCDATA - The element can only contain parsed character data. Elements - The element can only contain child elements. Mixed - The element contains both parsed character data and child elements.
- Slide 17
- 17 ANY Content: The declared element can store any type of content The syntax is: Example: Any of the following would satisfy the above declaration: SLR100 Digital Camera
- Slide 18
- 18 EMPTY content: This is reserved for elements that store no content The syntax is: Example: The following would satisfy the above declaration:
- Slide 19
- 19 #PCDATA Content: can store parsed character data The syntax is: would permit the following element in an XML document: Lea Ziegler PCDATA element does not allow for child elements
- Slide 20
- 20 Working with Child Elements The syntax is: Where element is the parent element and children is a listing of its child elements. The declaration indicates that the following would be invalid: Lea Ziegler 555-2819
- Slide 21
- 21 Working with Child Elements To declare the order of child elements, use: Where child1, child2, is the order in which the child elements must appear within the parent element. Thus, indicates the customer element should contain three child elements named name, phone, email.
- Slide 22
- 22 Working with Child Elements To allow for a choice of child elements, use: where child1, child2, etc. are the possible child elements of the parent element. allows the customer element to contain either the name element or the company element.
- Slide 23
- 23 Modifying Symbols A modifying symbol specifies the number of occurrences of each element: ? allows zero or one of the item. + allows one or more of the item. * allows zero or more of the item. Modifying symbols can be applied within sequences or choices. They can also modify entire element sequences or choices by placing the character immediately following the closing parenthesis of the sequence or choice.
- Slide 24
- 24 Modifying Symbols indicates that the customers element must contain at least one element named customer. indicates that the child element sequence (orderDate, items) can be repeated one or more times within each order element. allows the customer element to contain zero or one email elements.
- Slide 25
- 25 Working with Mixed Content Mixed content elements contain both parsed character data and child elements. The syntax is: The parent element can contain character data or any number of the specified child elements, or it can contain no content at all. It is better not to work with mixed content if you want a tightly structured document.
- Slide 26
- 26 Declaring Element Attributes For a document to be valid, all the attributes associated with elements must also be declared. To enforce attribution properties, you must add an attribute-list declaration to the documents DTD.
- Slide 27
- ElementAttributesRequired?Default Value(s) customercustID custType Yes No None home or business nameTitleNoMr., Mrs., Ms. orderorderID orderBy Yes None none itemitemPrice itemQty Yes No None 1 Attributes used in orders.xml
- Slide 28
- 28 Declaring Element Attributes The attribute-list declaration: Lists the names of all attributes associated with a specific element Specifies the data type of the attribute Indicates whether the attribute is required or optional Provides a default value for the attribute, if necessary
- Slide 29
- 29 Declaring Element Attributes The syntax to declare a list of attributes is: Where element is the name of the element associated with the attributes, attribute is the name of an attribute, type is the attributes data type, and default indicates whether the attribute is required and whether it has a default value.
- Slide 30
- 30 Declaring Element Attributes Attribute-list declaration can be placed anywhere within the document type declaration, although it is easier if they are located adjacent to the declaration for the element with which they are associated.
- Slide 31
- 31 Working with Attribute Types Attribute values can consist only of character data, but you can control the format of those characters. Three general categories of attribute values are: CDATA can contain any character except those reserved by XML Enumerated types are attributes that are limited to a set of possible values Tokenized types are text strings that follow certain rules for the format and content
- Slide 32
- 32 CDATA The syntax is: Examples: Any of the following attributes values are allowed:.........
- Slide 33
- 33 Enumerated Types The general form for an enumerated type is: where value1, value2,.. are allowed values Under the declaration below: any custType attribute whose value is not home or business causes parsers to reject the document as invalid.
- Slide 34
- 34 Working with Attribute Types Another type of enumerated attribute is notation. It associates the value of the attribute with a declaration located elsewhere in the DTD. The notation provides information to the XML parser about how to handle non-XML data.
- Slide 35
- 35 Tokenized Types are character strings that follow certain rules for format and content To declare an attribute as a tokenized type, use: attribute token DTDs support seven tokens: IDs, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, ENTITIES An ID is used when an attribute value must be unique within an document. For example: This ensures each customer will have a unique ID.
- Slide 36
- 36 IDREF token IDREF token must have a value eq
View more >