SlideShare uma empresa Scribd logo
1 de 32
XML DTD and Namespaces
Chapter 2
Core XML / Chapter 2 / Slide 2 of 25
Review-1
 A markup language defines a set of rules that adds
meaning to the content and structure of documents
 XML is extensible, which means that we can define our
own set of tags, and make it possible for other parties
(people or programs) to know and understand these
tags. This makes XML much more flexible than HTML
 XML inherits features from SGML and includes the
features of HTML. XML can be generated from
existing databases using a scalable three-tier model.
XML-based data does not contain information about
how data should be displayed
 An XML document is composed of a set of “entities”
identified by unique names
Core XML / Chapter 2 / Slide 3 of 25
Review-2
 A well-formed document is one that conforms to the
basic rules of XML; a valid document is a well-formed
document that conforms to the rules of a DTD
(Document Type Definition)
 The parser helps the computer to interpret an XML file
 Steps involved in the building of an XML document
are:
 Stating an XML declaration
 Creating a root element
 Creating the XML code
 Verifying the document
 Character data is classified into PCDATA and CDATA
Core XML / Chapter 2 / Slide 4 of 25
Review-3
 Entities are used to avoid typing long pieces of text
repeatedly in a document. The two types of entities are:
 General entities
 Parameter entities
 The <!DOCTYPE […]> declaration follows the XML
declaration in an XML document.
 An attribute gives information about an element
Core XML / Chapter 2 / Slide 5 of 25
Chapter Objectives
 Explain Document Type Definition
 Create Document Type Definitions:
 Declaring an Element
 Declaring Attributes
 Explain the use of DTD
 Describe namespaces
Core XML / Chapter 2 / Slide 6 of 25
Document Type Definition (DTD)
 It is a feature of SGML, which is inherited by
XML.
 It contains the list of tags that specifies the
grammatical structure of an XML document.
 DTD defines the way elements relate to one
another within the document’s tree structure, and
specifies the attributes.
 DTD are of two types:
 An external DTD
 An internal DTD
Core XML / Chapter 2 / Slide 7 of 25
Why Use a DTD
 DTDs are used by XML to provide an application
independent way of sharing data.
 Common DTD can be used to interchange data
between independent groups of people.
 DTD can be used by the application to verify that
valid data has been entered.
 It defines the legal building blocks of an XML
document.
Core XML / Chapter 2 / Slide 8 of 25
Structure of a DTD
<!DOCTYPE dtd-name
[
<!ELEMENT element-
name (element-
content type) >
<!ATTLIST element-
name attribute-name
attribute-type
default-value>
]>
DOCTYPE declaration
ELEMENT declaration
ATTRIBUTE
declaration
Core XML / Chapter 2 / Slide 9 of 25
Declaring an Element
 XML elements are declared with an element
declaration.
 Syntax
<!ELEMENT element-name (element-content
type)>
 Example
<!ELEMENT SHOWROOM (TV|LAPTOP)+>
Core XML / Chapter 2 / Slide 10 of 25
Empty Element
 EMPTY element-content type specifies that the
element has no child elements or character data.
 Syntax
<!ELEMENT element-name (EMPTY)>
 Example
<!ELEMENT img (EMPTY)>
 Empty elements with attributes are possible:
<img src=“Tittle.gif”></img>
Core XML / Chapter 2 / Slide 11 of 25
Elements with Data
 Syntax
<!ELEMENT element-name (#CDATA)>
or
<!ELEMENT element-name (#PCDATA)>
or
<!ELEMENT element-name (ANY)>
Where:
#CDATA = element contains character data that is not parsed
#PCDATA = element contains character data that is to be parsed
ANY = element with any content
Core XML / Chapter 2 / Slide 12 of 25
Elements with Child
Elements Elements with one or more children are defined with the name
of the child element inside the parentheses.
 Syntax
<!ELEMENT element-name (child-element-name)>
or
<!ELEMENT element-name (child-element-name, child-element-name,.....)>
 Example
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#CDATA)> <!ELEMENT from (#CDATA)>
<!ELEMENT heading (#CDATA)> <!ELEMENT body
(#CDATA)>
Core XML / Chapter 2 / Slide 13 of 25
Declaring According to the
Occurrences of Elements
Element occurrences
Only one occurrence
<!ELEMENT element-name (child-name)>
Minimum one
occurrence
<!ELEMENT
element-name
(child-name+)>
Zero or more
occurrences
<!ELEMENT
element-name
(child-
name*)>
Zero or one occurrence
<!ELEMENT element-name (child-name?)>
Core XML / Chapter 2 / Slide 14 of 25
Declaring Mixed Content
 An element can have a mixed combination of child
elements.
 Example
<!ELEMENT note (to+, from, header,
message*,#PCDATA)>
 The sub elements and subgroups can be declared in
Sequence or Choice.
Core XML / Chapter 2 / Slide 17 of 25
Declaring Attributes
 Elements can have attributes.
 Syntax
<!ATTLIST element-name attribute-name attribute-type
default-value>
 Example: 1
<!DOCTYPE Book
[
<!ELEMENT Book (Title, Chapter+)>
<!ATTLIST Book
Author CDATA #REQUIRED>
<!ELEMENT Chapter (#PCDATA)>
<!ATTLIST Chapter
id (4 | 7) #REQUIRED>
<!ELEMENT Title (#PCDATA)> ]>
Core XML / Chapter 2 / Slide 18 of 25
Declaring Attributes
Example: 2
Core XML / Chapter 2 / Slide 19 of 25
Attribute (Attribute- Type Values)
Value Explanation
CDATA The value is character data
(eval|eval|..) The value must be an enumerated value
ID The value is an unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation
xml: The value is predefined
Core XML / Chapter 2 / Slide 20 of 25
Attribute (Attribute-Default-Value)
Value Explanation
#DEFAULT The attribute has a default value.
#REQUIRED
The attribute value must be included in the
element.
#IMPLIED The attribute does not have to be included.
#FIXED The attribute value is fixed.
Core XML / Chapter 2 / Slide 21 of 25
Internal DTD
 It is written directly in
the XML document after
the XML declaration.
 Writing the DTD within
the DOCTYPE definition
is called as Wrapping.
 The file with the DTD
and XML code has a
.xml extension.
<!DOCTYPE SHOWROOM
[
<!ELEMENT SHOWROOM
(TV|LAPTOP)+>
<!ELEMENT TV (#PCDATA)>
<!ATTLIST TV
count CDATA #REQUIRED>
<!ELEMENT
LAPTOP(#PCDATA)>
<!ATTLIST LAPTOP
count CDATA #REQUIRED>
]
>
Core XML / Chapter 2 / Slide 22 of 25
External DTD
 It exists outside the
content of a document.
 The DTD file has a
.dtd extension.
 The reference to the
DTD file is added at
the beginning of the
XML file.
The DTD reference in the XML
document file:
<!DOCTYPE SHOWROOM SYSTEM
"show.dtd">
The show.dtd file:
<!ELEMENT SHOWROOM
(TV|LAPTOP)+>
<!ELEMENT TV (#PCDATA)>
<!ATTLIST TV
count CDATA #REQUIRED>
<!ELEMENT LAPTOP (#PCDATA)>
<!ATTLIST LAPTOP
count CDATA #REQUIRED>
Core XML / Chapter 2 / Slide 23 of 25
Internal Entity Declaration
 Entities that have their contents within the XML
document are called internal entities.
 Syntax
<!ENTITY entity-name "entity-value">
 Example
<!ENTITY writer “Nicole D.">
<!ENTITY copyright "Copyright Aptech Ltd.">
In XML document the entities would be referred as shown below:
<author>&writer;&copyright;</author>
Core XML / Chapter 2 / Slide 24 of 25
External Entity Declaration
 Entities whose contents are found outside the
XML document are called external entities.
 They are declared using the SYSTEM keyword.
 Syntax
<!ENTITY entity-name SYSTEM "URI/URL">
 Example
<!ENTITY writer SYSTEM
"http://www.xml101.com/entities/entities.
xml">
<!ENTITY copyright SYSTEM
"http://www.xml101.com/entities/entities.
dtd">
Core XML / Chapter 2 / Slide 25 of 25
XML Namespaces - 1
 Two or more applications on the Internet may also
have some element names that are common.
Namespaces help avoid such ambiguity that may
arise.
 It also allows to combine documents from different
sources and enables the identification of what
element or attributes come from which source.
 It instructs the user agent to access the DTD against
which the document is validated.
Core XML / Chapter 2 / Slide 26 of 25
XML Namespaces - 2
 A URI(Uniform Resource Identifier) is used
to identify namespaces in XML.
 It includes Uniform Resources Name(URN)
and a Uniform Resource Locator(URL).
 URL contains the reference for a document
or an HTML page on a web.
 URN is a universally unique number that
identifies Internet resources.
Core XML / Chapter 2 / Slide 27 of 25
Needs of a Namespace
 Namespaces are used to overcome the conflict
that arise when reuse and extension of the
DTD’s take place.
 Namespaces help standardize and uniquely
brand elements and attributes.
 Namespaces employ the URI to instruct the
user-agent about the location of the DTD
against which the XML document is checked
for validity.
 Namespaces ensure that element names do not
conflict and do clarify their origins.
Core XML / Chapter 2 / Slide 28 of 25
Syntax for Namespace
 A prefix is associated with the URI that can be
used as a namespace.
 Syntax
xmlns:[prefix]= “[URI of namespace]”
 The xmlns: is a reserved attribute
 Example
xmlns:ins= “http://www.Aptech_edu.ac”
 Namespace needs to be declared before using
 It is declared in the root element of the document
Core XML / Chapter 2 / Slide 29 of 25
Attributes and Namespaces
 Attributes comes within the namespace of
their element unless they are predefined.
 We can also incorporate attributes from
two domains:
<sample
xmlns= “http://www.Aptech_edu.ac”
xmlns:tea_batch= “http://www.tea.org”>
<batch-list>
<batch type=“thirdbatch”>Evening Batch</batch>
<batch tea_batch:type= “thirdbatch”>Tea batch
III </batch>
<batch>Afternoon Batch</batch>
</batch-list>
</sample>
Core XML / Chapter 2 / Slide 30 of 25
Namespace Application
 The new XSL syntax makes use of namespace
to identify both its own tags, and the
formatting vocabulary tags.
 The xsl: prefix are in the
http//www.w3.org/TR/WD-xsl namespace.
 The fo: prefix are in the
http//www.w3.org/TR/WD-xsl/FO.
 XSL is written in XML syntax and uses tags,
elements, and attributes.
Core XML / Chapter 2 / Slide 31 of 25
Namespace Example
<book
xmlns:html=“http//www.w3.org/TR/WD-xsl/FO”>
<index>
<chapter>this is chapter 1</chapter>
<html:br/>
<chapter>this is chapter 1</chapter>
</index>
</book>
Core XML / Chapter 2 / Slide 32 of 25
Summary-1
 A well-formed document is one that conforms to
the basic rules of XML.
 A valid document is well formed and is also
validated against a DTD.
 The DTD specifies the grammatical structure of an
XML document, thereby allowing XML parsers to
understand and interpret the document’s contents.
 The use of the SYSTEM keyword indicates to the
parser that this is an external declaration, and that
the set of rules for this XML document can be
found in a specified file.
 EMPTY element-content type specifies that the
element has no child elements or character data.
Core XML / Chapter 2 / Slide 33 of 25
Summary-2
 #CDATA means that the element contains character
data that is not to be parsed by a parser.
#PCDATA means that the element contains data
that is to be parsed by a parser.
 Specifying a default value for an attribute in the
DTD ensures that the attribute will get a value, even
if the author of the XML document does not include
it.
 Specifying the value of an attribute as ‘Implied’
means that the particular attribute is not mandatory
and can be specified in the XML document.
 Specifying the value of an attribute as ‘Required’
means that the particular attribute is mandatory
(that is, its value must be provided in the XML
document).
Core XML / Chapter 2 / Slide 34 of 25
Summary-3
 ‘ID’ is the identifier type, and should be unique.
This attribute value is used to search for a
particular instance of an element. Each element
can only have one attribute of type ID.
 A DTD can be either External or Internal.
 Entities allow us to create an alias to some large
piece of text, so that, in the document, the same
piece of text can be referred to, simply by
referring to the alias.
 Namespaces allow us to combine documents from
different sources, and be able to identify which
elements or attributes come from which source.

Mais conteúdo relacionado

Mais procurados (20)

XML Schema
XML SchemaXML Schema
XML Schema
 
Xml schema
Xml schemaXml schema
Xml schema
 
Xml schema
Xml schemaXml schema
Xml schema
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Xml schema
Xml schemaXml schema
Xml schema
 
XSD
XSDXSD
XSD
 
Dtd
DtdDtd
Dtd
 
XML's validation - DTD
XML's validation - DTDXML's validation - DTD
XML's validation - DTD
 
Xml session08
Xml session08Xml session08
Xml session08
 
Xml dtd
Xml dtdXml dtd
Xml dtd
 
01 xml document structure
01 xml document structure01 xml document structure
01 xml document structure
 
DTD
DTDDTD
DTD
 
Xml dtd
Xml dtdXml dtd
Xml dtd
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
 
Xml2
Xml2Xml2
Xml2
 
Xml Java
Xml JavaXml Java
Xml Java
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
 
DTD
DTDDTD
DTD
 
Document Type Definition
Document Type DefinitionDocument Type Definition
Document Type Definition
 
Difference between dtd and xsd
Difference between dtd and xsdDifference between dtd and xsd
Difference between dtd and xsd
 

Destaque

mgmnt dev. and org. development
mgmnt dev. and org. developmentmgmnt dev. and org. development
mgmnt dev. and org. developmentumesh yadav
 
Overview of XSL, XPath and XSL-FO
Overview of XSL, XPath and XSL-FOOverview of XSL, XPath and XSL-FO
Overview of XSL, XPath and XSL-FOSuite Solutions
 
Introduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSSIntroduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSSJussi Pohjolainen
 
XML Avancé : DTD, XSD, XPATH, XSLT, XQuery
XML Avancé : DTD, XSD, XPATH, XSLT, XQueryXML Avancé : DTD, XSD, XPATH, XSLT, XQuery
XML Avancé : DTD, XSD, XPATH, XSLT, XQueryRachid NID SAID
 
2 dtd - validating xml documents
2   dtd - validating xml documents2   dtd - validating xml documents
2 dtd - validating xml documentsgauravashq
 

Destaque (8)

mgmnt dev. and org. development
mgmnt dev. and org. developmentmgmnt dev. and org. development
mgmnt dev. and org. development
 
4 xslt
4   xslt4   xslt
4 xslt
 
Html ,css,xml
Html ,css,xmlHtml ,css,xml
Html ,css,xml
 
HTML, CSS and XML
HTML, CSS and XMLHTML, CSS and XML
HTML, CSS and XML
 
Overview of XSL, XPath and XSL-FO
Overview of XSL, XPath and XSL-FOOverview of XSL, XPath and XSL-FO
Overview of XSL, XPath and XSL-FO
 
Introduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSSIntroduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSS
 
XML Avancé : DTD, XSD, XPATH, XSLT, XQuery
XML Avancé : DTD, XSD, XPATH, XSLT, XQueryXML Avancé : DTD, XSD, XPATH, XSLT, XQuery
XML Avancé : DTD, XSD, XPATH, XSLT, XQuery
 
2 dtd - validating xml documents
2   dtd - validating xml documents2   dtd - validating xml documents
2 dtd - validating xml documents
 

Semelhante a Tp2 (20)

Xml
XmlXml
Xml
 
XML.ppt
XML.pptXML.ppt
XML.ppt
 
Xml11
Xml11Xml11
Xml11
 
Xml
XmlXml
Xml
 
XML-Unit 1.ppt
XML-Unit 1.pptXML-Unit 1.ppt
XML-Unit 1.ppt
 
Xml
XmlXml
Xml
 
Xml sasidhar
Xml  sasidharXml  sasidhar
Xml sasidhar
 
distributed system concerned lab sessions
distributed system concerned lab sessionsdistributed system concerned lab sessions
distributed system concerned lab sessions
 
Document type definition
Document type definitionDocument type definition
Document type definition
 
XXE
XXEXXE
XXE
 
Xxe
XxeXxe
Xxe
 
XML Validations.ppt
XML Validations.pptXML Validations.ppt
XML Validations.ppt
 
Dtd
DtdDtd
Dtd
 
DTD1.pptx
DTD1.pptxDTD1.pptx
DTD1.pptx
 
Unit 5 xml (1)
Unit 5   xml (1)Unit 5   xml (1)
Unit 5 xml (1)
 
Test for an issue
Test for an issueTest for an issue
Test for an issue
 
Chen's first test slides
Chen's first test slidesChen's first test slides
Chen's first test slides
 
Chen test paper20abcdeftfdfd
Chen test paper20abcdeftfdfdChen test paper20abcdeftfdfd
Chen test paper20abcdeftfdfd
 
uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2uptu web technology unit 2 Xml2
uptu web technology unit 2 Xml2
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 

Mais de Bình Trọng Án

A Developer's Guide to CQRS Using .NET Core and MediatR
A Developer's Guide to CQRS Using .NET Core and MediatRA Developer's Guide to CQRS Using .NET Core and MediatR
A Developer's Guide to CQRS Using .NET Core and MediatRBình Trọng Án
 
Nếu con em vị nói lắp
Nếu con em vị nói lắpNếu con em vị nói lắp
Nếu con em vị nói lắpBình Trọng Án
 
Bài giảng chuyên đề - Lê Minh Hoàng
Bài giảng chuyên đề - Lê Minh HoàngBài giảng chuyên đề - Lê Minh Hoàng
Bài giảng chuyên đề - Lê Minh HoàngBình Trọng Án
 
Các câu chuyện toán học - Tập 3: Khẳng định trong phủ định
Các câu chuyện toán học - Tập 3: Khẳng định trong phủ địnhCác câu chuyện toán học - Tập 3: Khẳng định trong phủ định
Các câu chuyện toán học - Tập 3: Khẳng định trong phủ địnhBình Trọng Án
 
2816 mcsa--part-11--domain-c111ntroller--join-domain-1
2816 mcsa--part-11--domain-c111ntroller--join-domain-12816 mcsa--part-11--domain-c111ntroller--join-domain-1
2816 mcsa--part-11--domain-c111ntroller--join-domain-1Bình Trọng Án
 
Tỷ lệ vàng - một phát hiện vĩ đại của hình học
Tỷ lệ vàng - một phát hiện vĩ đại của hình họcTỷ lệ vàng - một phát hiện vĩ đại của hình học
Tỷ lệ vàng - một phát hiện vĩ đại của hình họcBình Trọng Án
 
Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET componentsBình Trọng Án
 
Sách chữa tật nói lắp Version 1.0 beta
Sách chữa tật nói lắp Version 1.0 betaSách chữa tật nói lắp Version 1.0 beta
Sách chữa tật nói lắp Version 1.0 betaBình Trọng Án
 
Displaying XML Documents Using CSS and XSL
Displaying XML Documents Using CSS and XSLDisplaying XML Documents Using CSS and XSL
Displaying XML Documents Using CSS and XSLBình Trọng Án
 

Mais de Bình Trọng Án (19)

A Developer's Guide to CQRS Using .NET Core and MediatR
A Developer's Guide to CQRS Using .NET Core and MediatRA Developer's Guide to CQRS Using .NET Core and MediatR
A Developer's Guide to CQRS Using .NET Core and MediatR
 
Nếu con em vị nói lắp
Nếu con em vị nói lắpNếu con em vị nói lắp
Nếu con em vị nói lắp
 
Bài giảng chuyên đề - Lê Minh Hoàng
Bài giảng chuyên đề - Lê Minh HoàngBài giảng chuyên đề - Lê Minh Hoàng
Bài giảng chuyên đề - Lê Minh Hoàng
 
Tìm hiểu về NodeJs
Tìm hiểu về NodeJsTìm hiểu về NodeJs
Tìm hiểu về NodeJs
 
Clean code-v2.2
Clean code-v2.2Clean code-v2.2
Clean code-v2.2
 
Các câu chuyện toán học - Tập 3: Khẳng định trong phủ định
Các câu chuyện toán học - Tập 3: Khẳng định trong phủ địnhCác câu chuyện toán học - Tập 3: Khẳng định trong phủ định
Các câu chuyện toán học - Tập 3: Khẳng định trong phủ định
 
Luyện dịch Việt Anh
Luyện dịch Việt AnhLuyện dịch Việt Anh
Luyện dịch Việt Anh
 
2816 mcsa--part-11--domain-c111ntroller--join-domain-1
2816 mcsa--part-11--domain-c111ntroller--join-domain-12816 mcsa--part-11--domain-c111ntroller--join-domain-1
2816 mcsa--part-11--domain-c111ntroller--join-domain-1
 
LinQ to XML
LinQ to XMLLinQ to XML
LinQ to XML
 
Chuyên đề group policy
Chuyên đề group policyChuyên đề group policy
Chuyên đề group policy
 
Chapter 4 xml schema
Chapter 4   xml schemaChapter 4   xml schema
Chapter 4 xml schema
 
Tỷ lệ vàng - một phát hiện vĩ đại của hình học
Tỷ lệ vàng - một phát hiện vĩ đại của hình họcTỷ lệ vàng - một phát hiện vĩ đại của hình học
Tỷ lệ vàng - một phát hiện vĩ đại của hình học
 
Attributes & .NET components
Attributes & .NET componentsAttributes & .NET components
Attributes & .NET components
 
Ajax Control ToolKit
Ajax Control ToolKitAjax Control ToolKit
Ajax Control ToolKit
 
Linq intro
Linq introLinq intro
Linq intro
 
Sách chữa tật nói lắp Version 1.0 beta
Sách chữa tật nói lắp Version 1.0 betaSách chữa tật nói lắp Version 1.0 beta
Sách chữa tật nói lắp Version 1.0 beta
 
Mô hình 3 lớp
Mô hình 3 lớpMô hình 3 lớp
Mô hình 3 lớp
 
Displaying XML Documents Using CSS and XSL
Displaying XML Documents Using CSS and XSLDisplaying XML Documents Using CSS and XSL
Displaying XML Documents Using CSS and XSL
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 

Tp2

  • 1. XML DTD and Namespaces Chapter 2
  • 2. Core XML / Chapter 2 / Slide 2 of 25 Review-1  A markup language defines a set of rules that adds meaning to the content and structure of documents  XML is extensible, which means that we can define our own set of tags, and make it possible for other parties (people or programs) to know and understand these tags. This makes XML much more flexible than HTML  XML inherits features from SGML and includes the features of HTML. XML can be generated from existing databases using a scalable three-tier model. XML-based data does not contain information about how data should be displayed  An XML document is composed of a set of “entities” identified by unique names
  • 3. Core XML / Chapter 2 / Slide 3 of 25 Review-2  A well-formed document is one that conforms to the basic rules of XML; a valid document is a well-formed document that conforms to the rules of a DTD (Document Type Definition)  The parser helps the computer to interpret an XML file  Steps involved in the building of an XML document are:  Stating an XML declaration  Creating a root element  Creating the XML code  Verifying the document  Character data is classified into PCDATA and CDATA
  • 4. Core XML / Chapter 2 / Slide 4 of 25 Review-3  Entities are used to avoid typing long pieces of text repeatedly in a document. The two types of entities are:  General entities  Parameter entities  The <!DOCTYPE […]> declaration follows the XML declaration in an XML document.  An attribute gives information about an element
  • 5. Core XML / Chapter 2 / Slide 5 of 25 Chapter Objectives  Explain Document Type Definition  Create Document Type Definitions:  Declaring an Element  Declaring Attributes  Explain the use of DTD  Describe namespaces
  • 6. Core XML / Chapter 2 / Slide 6 of 25 Document Type Definition (DTD)  It is a feature of SGML, which is inherited by XML.  It contains the list of tags that specifies the grammatical structure of an XML document.  DTD defines the way elements relate to one another within the document’s tree structure, and specifies the attributes.  DTD are of two types:  An external DTD  An internal DTD
  • 7. Core XML / Chapter 2 / Slide 7 of 25 Why Use a DTD  DTDs are used by XML to provide an application independent way of sharing data.  Common DTD can be used to interchange data between independent groups of people.  DTD can be used by the application to verify that valid data has been entered.  It defines the legal building blocks of an XML document.
  • 8. Core XML / Chapter 2 / Slide 8 of 25 Structure of a DTD <!DOCTYPE dtd-name [ <!ELEMENT element- name (element- content type) > <!ATTLIST element- name attribute-name attribute-type default-value> ]> DOCTYPE declaration ELEMENT declaration ATTRIBUTE declaration
  • 9. Core XML / Chapter 2 / Slide 9 of 25 Declaring an Element  XML elements are declared with an element declaration.  Syntax <!ELEMENT element-name (element-content type)>  Example <!ELEMENT SHOWROOM (TV|LAPTOP)+>
  • 10. Core XML / Chapter 2 / Slide 10 of 25 Empty Element  EMPTY element-content type specifies that the element has no child elements or character data.  Syntax <!ELEMENT element-name (EMPTY)>  Example <!ELEMENT img (EMPTY)>  Empty elements with attributes are possible: <img src=“Tittle.gif”></img>
  • 11. Core XML / Chapter 2 / Slide 11 of 25 Elements with Data  Syntax <!ELEMENT element-name (#CDATA)> or <!ELEMENT element-name (#PCDATA)> or <!ELEMENT element-name (ANY)> Where: #CDATA = element contains character data that is not parsed #PCDATA = element contains character data that is to be parsed ANY = element with any content
  • 12. Core XML / Chapter 2 / Slide 12 of 25 Elements with Child Elements Elements with one or more children are defined with the name of the child element inside the parentheses.  Syntax <!ELEMENT element-name (child-element-name)> or <!ELEMENT element-name (child-element-name, child-element-name,.....)>  Example <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#CDATA)> <!ELEMENT from (#CDATA)> <!ELEMENT heading (#CDATA)> <!ELEMENT body (#CDATA)>
  • 13. Core XML / Chapter 2 / Slide 13 of 25 Declaring According to the Occurrences of Elements Element occurrences Only one occurrence <!ELEMENT element-name (child-name)> Minimum one occurrence <!ELEMENT element-name (child-name+)> Zero or more occurrences <!ELEMENT element-name (child- name*)> Zero or one occurrence <!ELEMENT element-name (child-name?)>
  • 14. Core XML / Chapter 2 / Slide 14 of 25 Declaring Mixed Content  An element can have a mixed combination of child elements.  Example <!ELEMENT note (to+, from, header, message*,#PCDATA)>  The sub elements and subgroups can be declared in Sequence or Choice.
  • 15. Core XML / Chapter 2 / Slide 17 of 25 Declaring Attributes  Elements can have attributes.  Syntax <!ATTLIST element-name attribute-name attribute-type default-value>  Example: 1 <!DOCTYPE Book [ <!ELEMENT Book (Title, Chapter+)> <!ATTLIST Book Author CDATA #REQUIRED> <!ELEMENT Chapter (#PCDATA)> <!ATTLIST Chapter id (4 | 7) #REQUIRED> <!ELEMENT Title (#PCDATA)> ]>
  • 16. Core XML / Chapter 2 / Slide 18 of 25 Declaring Attributes Example: 2
  • 17. Core XML / Chapter 2 / Slide 19 of 25 Attribute (Attribute- Type Values) Value Explanation CDATA The value is character data (eval|eval|..) The value must be an enumerated value ID The value is an unique id IDREF The value is the id of another element IDREFS The value is a list of other ids NMTOKEN The value is a valid XML name NMTOKENS The value is a list of valid XML names ENTITY The value is an entity ENTITIES The value is a list of entities NOTATION The value is a name of a notation xml: The value is predefined
  • 18. Core XML / Chapter 2 / Slide 20 of 25 Attribute (Attribute-Default-Value) Value Explanation #DEFAULT The attribute has a default value. #REQUIRED The attribute value must be included in the element. #IMPLIED The attribute does not have to be included. #FIXED The attribute value is fixed.
  • 19. Core XML / Chapter 2 / Slide 21 of 25 Internal DTD  It is written directly in the XML document after the XML declaration.  Writing the DTD within the DOCTYPE definition is called as Wrapping.  The file with the DTD and XML code has a .xml extension. <!DOCTYPE SHOWROOM [ <!ELEMENT SHOWROOM (TV|LAPTOP)+> <!ELEMENT TV (#PCDATA)> <!ATTLIST TV count CDATA #REQUIRED> <!ELEMENT LAPTOP(#PCDATA)> <!ATTLIST LAPTOP count CDATA #REQUIRED> ] >
  • 20. Core XML / Chapter 2 / Slide 22 of 25 External DTD  It exists outside the content of a document.  The DTD file has a .dtd extension.  The reference to the DTD file is added at the beginning of the XML file. The DTD reference in the XML document file: <!DOCTYPE SHOWROOM SYSTEM "show.dtd"> The show.dtd file: <!ELEMENT SHOWROOM (TV|LAPTOP)+> <!ELEMENT TV (#PCDATA)> <!ATTLIST TV count CDATA #REQUIRED> <!ELEMENT LAPTOP (#PCDATA)> <!ATTLIST LAPTOP count CDATA #REQUIRED>
  • 21. Core XML / Chapter 2 / Slide 23 of 25 Internal Entity Declaration  Entities that have their contents within the XML document are called internal entities.  Syntax <!ENTITY entity-name "entity-value">  Example <!ENTITY writer “Nicole D."> <!ENTITY copyright "Copyright Aptech Ltd."> In XML document the entities would be referred as shown below: <author>&writer;&copyright;</author>
  • 22. Core XML / Chapter 2 / Slide 24 of 25 External Entity Declaration  Entities whose contents are found outside the XML document are called external entities.  They are declared using the SYSTEM keyword.  Syntax <!ENTITY entity-name SYSTEM "URI/URL">  Example <!ENTITY writer SYSTEM "http://www.xml101.com/entities/entities. xml"> <!ENTITY copyright SYSTEM "http://www.xml101.com/entities/entities. dtd">
  • 23. Core XML / Chapter 2 / Slide 25 of 25 XML Namespaces - 1  Two or more applications on the Internet may also have some element names that are common. Namespaces help avoid such ambiguity that may arise.  It also allows to combine documents from different sources and enables the identification of what element or attributes come from which source.  It instructs the user agent to access the DTD against which the document is validated.
  • 24. Core XML / Chapter 2 / Slide 26 of 25 XML Namespaces - 2  A URI(Uniform Resource Identifier) is used to identify namespaces in XML.  It includes Uniform Resources Name(URN) and a Uniform Resource Locator(URL).  URL contains the reference for a document or an HTML page on a web.  URN is a universally unique number that identifies Internet resources.
  • 25. Core XML / Chapter 2 / Slide 27 of 25 Needs of a Namespace  Namespaces are used to overcome the conflict that arise when reuse and extension of the DTD’s take place.  Namespaces help standardize and uniquely brand elements and attributes.  Namespaces employ the URI to instruct the user-agent about the location of the DTD against which the XML document is checked for validity.  Namespaces ensure that element names do not conflict and do clarify their origins.
  • 26. Core XML / Chapter 2 / Slide 28 of 25 Syntax for Namespace  A prefix is associated with the URI that can be used as a namespace.  Syntax xmlns:[prefix]= “[URI of namespace]”  The xmlns: is a reserved attribute  Example xmlns:ins= “http://www.Aptech_edu.ac”  Namespace needs to be declared before using  It is declared in the root element of the document
  • 27. Core XML / Chapter 2 / Slide 29 of 25 Attributes and Namespaces  Attributes comes within the namespace of their element unless they are predefined.  We can also incorporate attributes from two domains: <sample xmlns= “http://www.Aptech_edu.ac” xmlns:tea_batch= “http://www.tea.org”> <batch-list> <batch type=“thirdbatch”>Evening Batch</batch> <batch tea_batch:type= “thirdbatch”>Tea batch III </batch> <batch>Afternoon Batch</batch> </batch-list> </sample>
  • 28. Core XML / Chapter 2 / Slide 30 of 25 Namespace Application  The new XSL syntax makes use of namespace to identify both its own tags, and the formatting vocabulary tags.  The xsl: prefix are in the http//www.w3.org/TR/WD-xsl namespace.  The fo: prefix are in the http//www.w3.org/TR/WD-xsl/FO.  XSL is written in XML syntax and uses tags, elements, and attributes.
  • 29. Core XML / Chapter 2 / Slide 31 of 25 Namespace Example <book xmlns:html=“http//www.w3.org/TR/WD-xsl/FO”> <index> <chapter>this is chapter 1</chapter> <html:br/> <chapter>this is chapter 1</chapter> </index> </book>
  • 30. Core XML / Chapter 2 / Slide 32 of 25 Summary-1  A well-formed document is one that conforms to the basic rules of XML.  A valid document is well formed and is also validated against a DTD.  The DTD specifies the grammatical structure of an XML document, thereby allowing XML parsers to understand and interpret the document’s contents.  The use of the SYSTEM keyword indicates to the parser that this is an external declaration, and that the set of rules for this XML document can be found in a specified file.  EMPTY element-content type specifies that the element has no child elements or character data.
  • 31. Core XML / Chapter 2 / Slide 33 of 25 Summary-2  #CDATA means that the element contains character data that is not to be parsed by a parser. #PCDATA means that the element contains data that is to be parsed by a parser.  Specifying a default value for an attribute in the DTD ensures that the attribute will get a value, even if the author of the XML document does not include it.  Specifying the value of an attribute as ‘Implied’ means that the particular attribute is not mandatory and can be specified in the XML document.  Specifying the value of an attribute as ‘Required’ means that the particular attribute is mandatory (that is, its value must be provided in the XML document).
  • 32. Core XML / Chapter 2 / Slide 34 of 25 Summary-3  ‘ID’ is the identifier type, and should be unique. This attribute value is used to search for a particular instance of an element. Each element can only have one attribute of type ID.  A DTD can be either External or Internal.  Entities allow us to create an alias to some large piece of text, so that, in the document, the same piece of text can be referred to, simply by referring to the alias.  Namespaces allow us to combine documents from different sources, and be able to identify which elements or attributes come from which source.