Presentation is loading. Please wait.

Presentation is loading. Please wait.

Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple values (child elements can) Attributes are not easily.

Similar presentations


Presentation on theme: "Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple values (child elements can) Attributes are not easily."— Presentation transcript:

1 Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple values (child elements can) Attributes are not easily expandable (for future changes) Attributes cannot describe structures (child elements can) Attributes are more difficult to manipulate by program code 1

2 Covert the following XML code into XML code without attributes Mathew XML Guide 44.95 2

3 Comments in XML Syntax for writing comments in XML is similar to that of HTML. 3

4 XML File Declaration An XML document can begin with an optional XML declaration which precedes the root element. It identifies:  The document as an XML document.  Version of XML used.  Character encoding and external dependencies. Mathew XML Guide 44.95 4

5 XML Trees 5

6 XML Tree The elements in an XML document form a document tree. Draw the tree for the given XML document Mathew XML Guide 44.95 6

7 Its XML Tree Root Element catalog Attribute id="bk101" Element book Element title Text 44.95 Attribute currency = “US$” Element author Text Matthew Text XML Guide Element price 7

8 Draw the tree for the following XML Document Kim 34 South Street NY USA Vice President 175,000 8

9 XML Validation & Well-formedness 9

10 Well-formed and valid XML documents There are two levels of correctness of an XML document:  Well-formed. A well-formed XML document conforms to all of XML's syntax rules.  Valid. A valid document additionally conforms to some rules relating to the structure of the XML document. These rules are user-defined through DTDs or XML schemas.. 10

11 Well-Formed XML Documents A well-formed XML document is a document that conforms to the XML syntax rules. The syntax rules were described in the slides:  must begin with the XML declaration  must have one unique root element  all start tags must match end-tags  XML tags are case sensitive  all elements must be closed  all elements must be properly nested  all attribute values must be quoted An XML parser (i.e., browser) will not open an XML document if it is NOT well-formed.  Browser will report the error. 11

12 Valid XML Documents In XML, an optional Document Type Definition (DTD) or schema can be used to define the XML document’s structure. DTD typically specify additional rules on elements and attributes of an XML document. For example, an element named ‘student' contains 2 elements: ‘name’, ‘id' and 'day‘. Each is only character data. An XML document is considered valid if  It conforms to a DTD (i.e., has the appropriate structure)  It is well-formed (it’s syntax is correct). Well-tested tools exist that parses XML documents and validates them "against" a DTD. 12

13 Document Type Definition (DTD) 13

14 Why use DTD/Schemas? XML documents can have many different structures, and for this reason an application cannot be certain whether a particular document it receives is complete, ordered properly, and not missing data. DTDs and schemas solve this problem by providing an extensible way to describe XML document structure. Applications use DTDs or schemas to confirm whether XML documents are valid. 14

15 DTD The main purpose of the DTD is to define the structure of XML elements A DTD defines:  The name of the elements,  The type of content of each element,  How often and in which order elements may appear,  The name of attributes and their default values, 15

16 DTD – Declaring Elements In a DTD, XML elements are declared with an element declaration with the following syntax: element-content field is used to specify:  Value of an element (string, any, empty)  Name of other children elements. 16

17 DTD – Declaring Elements Values Elements containing string values are declared with #PCDATA (Parsed Character DATA) inside parentheses. …  Elements declared with the category keyword ANY can contain any combination of data. ….  Empty elements are declared with the category keyword EMPTY.  17

18 DTD – Declaring Elements with Children Elements with children are declared with the name of the children elements inside parentheses Ali 12345 ISC Children are declared in a sequence separated by commas. The children must appear in the same sequence in the document. 18

19 DTD – Declaring Elements with Children The children must also be declared, and the children can also have children. The full declaration of the “student" element is: Ali 12345 ISC 19

20 DTD – Declaring Occurrence of Elements A number of operators can be used to control the occurrence of children elements: Consider the following XML element … Declaring only one occurrence of a child element If we require that each client must have only one phone number then, the rule for client is 20

21 DTD – Declaring Occurrence of Elements Declaring at least one occurrence of an element If we require that each client must have at least one phone number then, the rule for client is Declaring zero or more occurrences of an element If we require that each client can have zero or more phone numbers then, the rule for client is 21

22 DTD – Declaring Occurrence of Elements Declaring Zero or One Occurrences of an Element If we require that each client can optionally have a phone number then, the rule for client is Declaring either/or Content If we require that each client must have either a phone number or an email address then, the rule for client is 22

23 Example Write the DTD rules for the following XML fragment. Kim 34 South Street NY USA Vice President $175,000 23

24 Example The rules must satisfy the following constraints: Each employee must have a single name. An employee must have at least one address. For the address, specifying the street is optional. The address must include the state and country information. The address can also include a phone number element. Each employee can specify zero or more phone numbers. Each employee must have a job title and a salary. 24

25 25 Root Element PARTS_LIST Attribute type=“pc" Element PART Element title Text motherboard Element PART Text Computer Parts Element |ITEM Element COMPANY Element model Element COST Element ITEM Element COMPANY Element COST Text ASUS Text P3B-F Text 123.00 Text Video Card Text ATI Text 160.00


Download ppt "Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple values (child elements can) Attributes are not easily."

Similar presentations


Ads by Google