Vous êtes sur la page 1sur 33

XML

XML

 XML is a meta markup language for text documents / textual data


 XML allows to define languages („applications“) to represent text documents / textual
data
 XML describe the document structure and meaning.
 FEATURES:
 Domain-specific mark-up language
 Data integration form disparate sources
 Self describing data
 Local computation and manipulation
 Multiple views of data

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 2


Create an XML file

WELL FORMED XML RULES


 XML Tags are Case Sensitive
 All XML Elements Must Have a Closing Tag
 XML Attribute Values Must Always be Quoted
 Elements cannot overlap
 XML tags can contain empty elements
 Each XML element must have unique root tag element
 No attribute can appear more than once in a tag
 Attribute values must be quoted

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 3


XML Tree Structure

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 4


DTD

 The XML Document Type Declaration, commonly known as DTD, is a way to describe
XML language precisely. DTDs check vocabulary and validity of the structure of XML
documents against grammatical rules of appropriate XML language.
 Syntax

<!DOCTYPE element DTD identifier [ declaration1 declaration2 ........ ]>


 The DTD starts with <!DOCTYPE delimiter.
 An element tells the parser to parse the document from the specified root element.
 DTD identifier is an identifier for the document type definition, which may be the path
to a file on the system or URL to a file on the internet. If the DTD is pointing to external
path, it is called External Subset.
 The square brackets [ ] enclose an optional list of entity declarations called Internal
Subset.
16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 5
16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 6
 Description of DTD
 <!DOCTYPE employee : It defines that the root element of the document is employee.
 <!ELEMENT employee: It defines that the employee element contains 3 elements
"firstname, lastname and email".
 <!ELEMENT firstname: It defines that the firstname element is #PCDATA typed.
(parse-able data type).
 <!ELEMENT lastname: It defines that the lastname element is #PCDATA typed.
(parse-able data type).
 <!ELEMENT email: It defines that the email element is #PCDATA typed. (parse-able
data type).

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 7


 A DTD is referred to as an internal DTD if elements are declared within the XML files.

To refer it as internal DTD, standalone attribute in XML declaration must be set to yes.

This means, the declaration works independent of an external source.

 Syntax
 <!DOCTYPE root-element [element-declarations]>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 8


INTERNAL DTD
 <?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>

 <!DOCTYPE address [

 <!ELEMENT address (name,company,phone)>

 <!ELEMENT name (#PCDATA)>

 <!ELEMENT company (#PCDATA)>

 <!ELEMENT phone (#PCDATA)>

 ]>

 <address>

 <name>Tanmay Patil</name>

 <company>TutorialsPoint</company>

 <phone>(011) 123-4567</phone>

 </address>
16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 9
EXTERNAL DTD

 In external DTD elements are declared outside the XML file. They are accessed by
specifying the system attributes which may be either the legal .dtd file or a valid URL.
To refer it as external DTD, standalone attribute in the XML declaration must be set as
no. This means, declaration includes information from the external source.
 Syntax
 Following is the syntax for external DTD −

 <!DOCTYPE root-element SYSTEM "file-name">

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 10


 <?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>

 <!DOCTYPE address SYSTEM "address.dtd">

 <address>

 <name>Tanmay Patil</name>

 <company>TutorialsPoint</company>

 <phone>(011) 123-4567</phone>

 </address>

 The content of the DTD file address.dtd

 <!ELEMENT address (name,company,phone)>

 <!ELEMENT name (#PCDATA)>

 <!ELEMENT company (#PCDATA)>

 <!ELEMENT phone (#PCDATA)>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 11


 System Identifiers
 A system identifier enables you to specify the location of an external file containing
DTD declarations. Syntax is as follows −
 <!DOCTYPE name SYSTEM "address.dtd" [...]>
 As you can see, it contains keyword SYSTEM and a URI reference pointing to the
location of the document.
 Public Identifiers
 Public identifiers provide a mechanism to locate DTD resources and is written as
follows −
 <!DOCTYPE name PUBLIC "-//Beginning XML//DTD Address Example//EN">

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 12


DTD ELEMENTS
 Declaring Elements
 In a DTD, XML elements are declared with the following syntax:
 <!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>
 Declaring Only One Occurrence of an Element
 <!ELEMENT element-name (child-name)>
 Example:
 <!ELEMENT note (message)>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 13


DTD ELEMENTS

 Elements with Parsed Character Data


 <!ELEMENT element-name (#PCDATA)>
 Example:
 <!ELEMENT from (#PCDATA)>
 Elements with any Contents
 <!ELEMENT element-name ANY>

Example:

<!ELEMENT note ANY>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 14


DTD ELEMENTS

 Elements with Children (sequences)


 <!ELEMENT element-name (child1)>
 or
 <!ELEMENT element-name (child1,child2,...)>
 Example:
 <!ELEMENT note (to,from,heading,body)>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 15


DTD ELEMENTS

 Declaring Mixed Content


 <!ELEMENT note (#PCDATA|to|from|header|message)*>
 Declaring either/or Content
 <!ELEMENT note (to,from,header,(message|body))>
 Declaring Zero or One Occurrences of an Element
 <!ELEMENT element-name (child-name?)>

Example:

<!ELEMENT note (message?)>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 16


DTD ELEMENTS
 Declaring Zero or More Occurrences of an Element
 <!ELEMENT element-name (child-name*)>

Example:

<!ELEMENT note (message*)>


 Declaring Minimum One Occurrence of an Element
 <!ELEMENT element-name (child-name+)>

Example:

<!ELEMENT note (message+)>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 17


XML SCHEMA

 XML Schema is commonly known as XML Schema Definition (XSD). It is used to

describe and validate the structure and the content of XML data. XML schema defines

the elements, attributes and data types. Schema element supports Namespaces. It is

similar to a database schema that describes the data in a database.

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 18


 <?xml version="1.0"?>

<xs:schema>
...
...
</xs:schema>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 19


 <?xml version = "1.0" encoding = "UTF-8"?>

 <xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">

 <xs:element name = "contact">

 <xs:complexType>

 <xs:sequence>

 <xs:element name = "name" type = "xs:string" />

 <xs:element name = "company" type = "xs:string" />

 <xs:element name = "phone" type = "xs:int" />

 </xs:sequence>

 </xs:complexType>

 </xs:element>

 </xs:schema>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 20


 Simple Type
 Simple type element is used only in the context of the text. Some of the predefined
simple types are: xs:integer, xs:boolean, xs:string, xs:date. For example −
 <xs:element name = "phone_number" type = "xs:int" />
 Complex Type
 A complex type is a container for other element definitions. This allows you to specify
which child elements an element can contain and to provide some structure within your
XML documents.

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 21


 <xs:element name = "Address">
 <xs:complexType>
 <xs:sequence>
 <xs:element name = "name" type = "xs:string" />
 <xs:element name = "company" type = "xs:string" />
 <xs:element name = "phone" type = "xs:int" />
 </xs:sequence>
 </xs:complexType>
 </xs:element>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 22


 <xs:element name = "AddressType">
 <xs:complexType>
 <xs:sequence>
 <xs:element name = "name" type = "xs:string" />
 <xs:element name = "company" type = "xs:string" />
 </xs:sequence>
 </xs:complexType>
 </xs:element>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 23


 <xs:element name = "Address2">
 <xs:element name = "Address1">
 <xs:complexType>
 <xs:complexType>
 <xs:sequence>
 <xs:sequence>
 <xs:element name = "address" type
 <xs:element name = "address" type = "AddressType" />
= "AddressType" />
 <xs:element name = "phone2" type
 <xs:element name = "phone1" type = "xs:int" />
= "xs:int" />
 </xs:sequence>
 </xs:sequence>
 </xs:complexType>
 </xs:complexType>
 </xs:element>
 </xs:element>
16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 24
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 indicates that the elements and data types used in the schema come from the
"http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements
and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace
should be prefixed with xs:
 targetNamespace="https://www.w3schools.com"
 indicates that the elements defined by this schema (note, to, from, heading, body.)
come from the "https://www.w3schools.com" namespace.
 xmlns="https://www.w3schools.com"
 indicates that the default namespace is "https://www.w3schools.com".
 elementFormDefault="qualified"
 indicates that any elements used by the XML instance document which were declared
in this schema must be namespace qualified.
16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 25
XML NAME SPACE

 A Namespace is a set of unique names. Namespace is a mechanisms by which


element and attribute name can be assigned to a group. The Namespace is identified
by URI(Uniform Resource Identifiers).
 Namespace Declaration
 A Namespace is declared using reserved attributes. Such an attribute name must
either be xmlns or begin with xmlns: shown as below −
 <element xmlns:name = "URL">
 Syntax
 The Namespace starts with the keyword xmlns.
 The word name is the Namespace prefix.
 The URL is the Namespace identifier.
16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 26
XML NAME SPACE

<root>

<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table xmlns:f="https://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

</root>

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 27


XML ATTRIBUTES
 ID − It is used to specify the element as unique.
 IDREF − It is used to reference an ID that has been named for another element.
 IDREFS − It is used to reference all IDs of an element.
 ENTITY − It indicates that the attribute will represent an external entity in the
document.
 ENTITIES − It indicates that the attribute will represent external entities in the
document.
 NMTOKEN − It is similar to CDATA with restrictions on what data can be part of the
attribute.
 NMTOKENS − It is similar to CDATA with restrictions on what data can be part of the
attribute.

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 28


ENUMERATED ATTRIBUTE VALUES

 Syntax:
 <!ATTLIST element-name attribute-name (eval|eval|..) default-value>
 DTD example:
 <!ATTLIST payment type (check|cash) "cash">
 XML example:
 <payment type="check">
 or
 <payment type="cash">

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 29


FIXED ATTRIBUTE VALUE

 Syntax:
 <!ATTLIST element-name attribute-name attribute-type #FIXED "value">
 DTD example:
 <!ATTLIST sender company CDATA #FIXED "Microsoft">

 XML example:
 <sender company="Microsoft">

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 30


REQUIRED ATTRIBUTE

 Syntax:
 <!ATTLIST element-name attribute_name attribute-type #REQUIRED>
 DTD example:
 <!ATTLIST person number CDATA #REQUIRED>

 XML example:
 <person number="5677">

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 31


IMPLIED ATTRIBUTE

 Syntax:
 <!ATTLIST element-name attribute-name attribute-type #IMPLIED>
 DTD example:
 <!ATTLIST contact fax CDATA #IMPLIED>

 XML example:
 <contact fax="555-667788">

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 32


DEFAULT ATTRIBUTE VALUE

 Syntax:
 <!ATTLIST element-name attribute-name CDATA "default-value">

 DTD example:
 <!ATTLIST payment type CDATA "check">

 XML example:
 <payment type="check">`

16CS302 INTERNET PROGRAMMING-XML,H.SUMMIA PARVEEN-AP/CSE 33