avoid using attributes? some of the problems using attributes: attributes cannot contain multiple...

Post on 31-Dec-2015

257 Views

Category:

Documents

6 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

Comments in XML

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

<!-- This is a comment -->

3

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

XML Trees

5

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

Its XML Tree

Root Elementcatalog

Attributeid="bk101"

Elementbook

Elementtitle

Text44.95

Attributecurrency = “US”$

Elementauthor

TextMatthew

TextXML Guide

Elementprice

7

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

XML Validation & Well-formedness

9

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

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

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

Document Type Definition (DTD)

13

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

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

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

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

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

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

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

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

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

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

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

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

top related