DB Unit-3
DB Unit-3
XML DTD:
The XML Document Type Declaration, commonly known
as DTD, is a way to describe XML language. DTDs check
vocabulary and validity of XML documents.
An XML DTD can be either specified inside the
document, or it can be kept in a separate document and then
linked separately.
Syntax
Basic syntax of a DTD is as follows −
<!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.
• The square brackets [ ] enclose an optional list of entity
declarations called Internal Subset.
Internal DTD
Syntax
Example:
<?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>Manoj</name>
<company>SKP</company>
<phone>123</phone>
</address>
Start Declaration:
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes"
?>
DTD:
<!DOCTYPE address [
DTD Body :
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone_no (#PCDATA)>
End Declaration :
]>
Rules
• The document type declaration must appear at the start
of the document.
• Similar to the DOCTYPE declaration, the element
declarations must start with an exclamation mark.
• The Name in the document type declaration must match
the element type of the root element.
External DTD
Syntax
<!DOCTYPE root-element SYSTEM "file-name">
Example
<?xml version = "1.0" encoding = "UTF-8" standalone = "no"
?>
<!DOCTYPE address SYSTEM "address.dtd">
<address>
<name>Manoj</name>
<company>SKP</company>
<phone>123</phone> </address>
The content of the DTD file address.dtd is as shown −
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
Types
You can refer to an external DTD by using either system
identifiers or public identifiers.
System Identifiers
A system identifier enables you to specify the location of
an external file containing DTD declarations.
Syntax
<!DOCTYPE name SYSTEM "address.dtd" [...]>
Public Identifiers
<!DOCTYPE name PUBLIC "-//Beginning XML//DTD Address
Example//EN">
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.
Schema element supports Namespaces.
Checking Validation
An XML document is called "well-formed" if it contains
the correct syntax. A well-formed and valid XML document is
one which have been validated against Schema.
Let's see the xml file using XML schema or XSD file.
employee.xml
<?xml version="1.0"?>
<employee
xmlns="http://www.javatpoint.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.javatpoint.com employee.x
sd">
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>[email protected]</email>
</employee>
Description of XML Schema
<xs:element name="employee"> : It defines the element
name employee.
<xs:complexType> : It defines that the element 'employee' is
complex type.
<xs:sequence> : It defines that the complex type is a
sequence of elements.
<xs:element name="firstname" type="xs:string"/> : It
defines that the element 'firstname' is of string/text type.
<xs:element name="lastname" type="xs:string"/> : It
defines that the element 'lastname' is of string/text type.
<xs:element name="email" type="xs:string"/> : It defines
that the element 'email' is of string/text type.
XML Schema Data types
There are two types of data types in XML schema.
1. simpleType
2. complexType
simple type
The simple type allows you to have text-based elements.
It contains less attributes, child elements, and cannot be left
empty.
Complex type
The complex type allows you to hold multiple attributes
and elements. It can contain additional sub elements and can
be left empty.
XML DATABASE
XML database is a data persistence software system
used for storing the huge amount of information in XML
format.
You can query your stored data by using XQuery.
Types of XML databases
There are two types of XML databases.
1. XML-enabled database
2. Native XML database (NXD)
XML-enable Database
X-PATH:
XPath defines a pattern or path expression to select
nodes or node sets in an XML document. These patterns are
used by XSLT to perform transformations.
XPath specifies seven types of nodes that can be output of
the execution of the XPath expression.
o Root
o Element
o Text
o Attribute
o Comment
o Processing Instruction
o Namespace
Syntax:
o //tagname[@attribute = ‘value’]
XPath Expressions:
Symbol Description
Example:
//input[@id = 'fakebox-input']
In this example, We are locating the ‘input‘ element whose
‘id‘ is equal to ‘fakebox-input‘
Types of XPath:
1. Absolute XPath
2. Relative Xpath
Absolute XPath:
Absolute XPath uses the root element of the HTML/XML
code and followed by all the elements which are necessary
to reach the desired element. It starts with the forward
slash ‘/’ .
Relative XPath:
In this, XPath begins with the double forward
slash ‘//’ which means it can search the element anywhere in
the Webpage.
XQuery
XQuery is a functional language that is used to retrieve
information stored in XML format.
XQuery can be used on XML documents, relational
databases containing data in XML formats.
Characteristics
• Functional Language − XQuery is a language to
retrieve/querying XML based data.
• Analogous to SQL − XQuery is to XML what SQL is to
databases.
• XPath based − XQuery uses XPath expressions to
navigate through XML documents.
• Universally accepted − XQuery is supported by all major
databases.
• W3C Standard − XQuery is a W3C standard.
Benefits of XQuery
• Using XQuery, both hierarchical and tabular data can be
retrieved.
• XQuery can be directly used to build webpages.
• XQuery can be used to transform xml documents.