Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple...

25
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

Transcript of Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple...

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

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

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

Covert the following XML code into XML code without attributes

<catalog>     <book id=“bk101”>        <author> Mathew</author>   <title> XML Guide</title>   <price currency=“USD”>44.95</price>   </book> </catalog>  

2

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

Comments in XML

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

<!-- This is a comment -->

3

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

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.

<?xml version=“1.0” encoding ="UTF-8"?>    <catalog>     <book id=“bk101”>        <author> Mathew</author>   <title> XML Guide</title>   <price currency=“USD”>44.95</price>   </book> </catalog>  

4

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

XML Trees

5

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

XML Tree

•The elements in an XML document form a document tree. •Draw the tree for the given XML document

<?xml version=“1.0” encoding ="UTF-8"?> <catalog>     <book id=“bk101”>        <author> Mathew</author>   <title> XML Guide</title>   <price currency=“USD”>44.95</price>   </book> </catalog>    

6

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

Its XML Tree

Root Elementcatalog

Attributeid="bk101"

Elementbook

Elementtitle

Text44.95

Attributecurrency = “US”$

Elementauthor

TextMatthew

TextXML Guide

Elementprice

7

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

Draw the tree for the following XML Document

<?xml version=“1.0” encoding ="UTF-8"?> <EmployeeRecord rid=“100”> <Name> Kim </Name>

<HomeAddress> <Street> 34 South Street </Street> <State> NY </State> <Country> USA </Country>

</HomeAddress> <JobTitle> Vice President </JobTitle> <Salary currency=“USD”> 175,000 </Salary></EmployeeRecord>

8

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

XML Validation & Well-formedness

9

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

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

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

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

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

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

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

Document Type Definition (DTD)

13

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

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

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

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

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

DTD – Declaring Elements

In a DTD, XML elements are declared with an element declaration with the following syntax:

<!ELEMENT element-name (element-content)>

element-content field is used to specify: Value of an element (string, any, empty) Name of other children elements.

16

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

DTD – Declaring Elements Values

•Elements containing string values are declared with #PCDATA (Parsed Character DATA) inside parentheses.

<name> …</name> <!ELEMENT name (#PCDATA)> •Elements declared with the category keyword ANY can contain any combination of data.

<note> …. </note> <!ELEMENT note ANY>

•Empty elements are declared with the category keyword EMPTY.

<product prodid="1345" /> <!ELEMENT product EMPTY>

17

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

DTD – Declaring Elements with Children

Elements with children are declared with the name of the children elements inside parentheses

<student> <name> Ali </name> <ID> 12345 </ID> <major> ISC </major>

</student>

<!ELEMENT student (name, ID, major)>

•Children are declared in a sequence separated by commas. •The children must appear in the same sequence in the document.

18

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

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:

<student> <name> Ali </name> <ID> 12345 </ID> <major> ISC </major>

</student>

<!ELEMENT student (name, ID, major)><!ELEMENT name (#PCDATA)><!ELEMENT ID (#PCDATA)><!ELEMENT major (#PCDATA)>

19

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

DTD – Declaring Occurrence of Elements

A number of operators can be used to control the occurrence of children elements:

Consider the following XML element

<client> <phone> … </phone>

<client>

•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

<!ELEMENT client (phone)>20

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

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

<!ELEMENT client (phone+)>

•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

<!ELEMENT client (phone*)>21

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

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

<!ELEMENT client (phone?)>

•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

<!ELEMENT client (phone|email)>22

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

Example

Write the DTD rules for the following XML fragment.

<?xml version=“1.0” encoding ="UTF-8"?> <EmployeeRecord> <Name> Kim </Name>

<HomeAddress> <Street> 34 South Street </Street> <State> NY </State> <Country> USA </Country>

</HomeAddress> <JobTitle> Vice President </JobTitle> <Salary> $175,000 </Salary></EmployeeRecord>

23

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

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

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

25

Root ElementPARTS_LIST

Attributetype=“pc"

ElementPART

Elementtitle

Textmotherboard

ElementPART

TextComputer Parts

Element|ITEM

ElementCOMPANY

Elementmodel

ElementCOST

ElementITEM

ElementCOMPANY

ElementCOST

TextASUS

TextP3B-F

Text123.00

TextVideo Card

TextATI

Text160.00