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

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

Post on 31-Dec-2015




6 download

Embed Size (px)


Introduction to XML

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

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

22Comments in XMLSyntax for writing comments in XML is similar to that of HTML.

33XML File DeclarationAn 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

4XML Trees5XML TreeThe elements in an XML document form a document tree. Draw the tree for the given XML document

Mathew XML Guide 44.95

6Its XML TreeRoot ElementcatalogAttributeid="bk101"ElementbookElementtitleText44.95Attributecurrency = US$ElementauthorTextMatthewTextXML GuideElementprice77Draw the tree for the following XML Document

Kim 34 South Street NY USA Vice President 175,000 88XML Validation & Well-formedness9Well-formed and valid XML documentsThere 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..

10Well-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.1111Valid XML DocumentsIn XML, an optional Document Type Definition (DTD) or schema can be used to define the XML documents 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 ifIt conforms to a DTD (i.e., has the appropriate structure) It is well-formed (its syntax is correct).

Well-tested tools exist that parses XML documents and validates them "against" a DTD.

12Document Type Definition (DTD)13Why 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.14DTD 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,

15DTD Declaring ElementsIn 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.

16DTD Declaring Elements ValuesElements 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.

17DTD Declaring Elements with ChildrenElements 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.18DTD Declaring Elements with ChildrenThe children must also be declared, and the children can also have children. The full declaration of the student" element is:

Ali 12345 ISC

19DTD Declaring Occurrence of ElementsA 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

20DTD Declaring Occurrence of ElementsDeclaring 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 occurrencesof an element

If we require that each client can have zero or more phone numbers then, the rule for client is

21DTD Declaring Occurrence of ElementsDeclaring 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

22ExampleWrite the DTD rules for the following XML fragment.

Kim 34 South Street NY USA Vice President $175,000

23ExampleThe 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.

2425Root ElementPARTS_LISTAttributetype=pc"ElementPARTElementtitleTextmotherboardElementPARTTextComputer PartsElement|ITEMElementCOMPANYElementmodelElementCOSTElementITEMElementCOMPANYElementCOSTTextASUSTextP3B-FText123.00TextVideo CardTextATIText160.00


View more >