0% found this document useful (0 votes)
19 views48 pages

ch07 - ADB - Semi Structured Database

The document discusses semi-structured databases and XML. It describes XML, XML namespaces, DTDs, XML schemas, XSLT, XPath, and XQuery. It provides examples of XML documents and schemas defining elements and their relationships.

Uploaded by

tahar.dey20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views48 pages

ch07 - ADB - Semi Structured Database

The document discusses semi-structured databases and XML. It describes XML, XML namespaces, DTDs, XML schemas, XSLT, XPath, and XQuery. It provides examples of XML documents and schemas defining elements and their relationships.

Uploaded by

tahar.dey20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Advanced Database

Chapter 07:
Semi structured database
PLAN

 Semi structured database


I. XML
II. XML Namespaces
III. Document Type Definitions (DTDs)
IV. XML Schemas
V. Extensible Stylesheet Language and XSL Transformations
VI. XPath
VII. XQuery

Chapter 07: Semi structured database 2


XML
 XML stands for EXtensible Markup Language
 XML was designed to carry and store data, not to display data
 XML is a markup language much like HTML
 XML tags are not predefined. You must define your own tags
 Example,

player.xml

The root element contains all other


elements in the document

Chapter 07: Semi structured database 3


XML Tree
 XML documents are formed as element trees.
 XML documents must contain one root element that is the parent of
all other elements.
 An XML tree starts at a root element and branches from the root to
child elements.

Chapter 07: Semi structured database 4


XML Tree

article.xml

Chapter 07: Semi structured database 5


XML Namespaces
 XML namespaces provide a means for document authors to prevent
naming collisions
 Each namespace prefix is bound to a uniform resource identifier (URI)
that uniquely identifies the namespace
 Example,
xmlns: bk = “http://www.example.com/bookinfo/”

Prefix URI(URL)
Namespace declaration

Chapter 07: Semi structured database 6


XML Namespace
 Solving the Name Conflict Using a Prefix
 Name conflicts in XML can easily be avoided using a name prefix.

Chapter 07: Semi structured database 7


XML Namespace
• An XML namespace declared without a prefix becomes the default
namespace for all sub-elements

• All elements without a prefix will belong to the default namespace:

<BOOK xmlns=“http://www.bookstuff.org/bookinfo”>
<TITLE> All About XML </TITLE>
<AUTHOR> Joe Developer </AUTHOR>
</BOOK>

Chapter 07: Semi structured database 8


XML Namespace
 Xml namespace demonstration

Chapter 07: Semi structured database 9


Default namespace

The default namespace is set in


the directory element

Elements with no namespace


prefix use the default
namespace

defaultnamespace.xml

Chapter 07: Semi structured database 10


Document Type Definitions (DTDs)
 DTDs and schemas specify documents’ element types and attributes,
and their relationships

 DTDs and schemas enable an XML parser to verify whether an XML


document is valid (i.e., its elements contain the proper attributes and
appear in the proper sequence)

Chapter 07: Semi structured database 11


Document Type Definitions (DTDs)

Define the requirements


for the letter element

letter.dtd
Define the requirements for
the contact element

A contact element may have a type


attribute, but it is not required
Each of these elements contains
parsed character data
The flag element must be empty and its
gender attribute must be set to either M
or F. If there is no gender attribute,
gender defaults to M

Chapter 07: Semi structured database 12


Document Type Definitions (DTDs)

letter.xml

Chapter 07: Semi structured database 13


DTDs

Chapter 07: Semi structured database 14


XML Schema Documents
 Unlike DTDs
 Schemas use XML syntax not EBNF grammar
 XML Schema documents can specify what type of data (e.g., numeric, text) an
element can

 An XML document that conforms to a schema document is schema valid

 Two categories of types exist in XML Schema: simple types and complex
types
 Simple types cannot contain attributes or child elements; complex types can

 Complex types can have either simple content or complex content


 Both can contain attributes, but only complex content can contain child element

 XML schema types: string , boolean, decimal, float, double, int, long, short,
date, time, …

Chapter 07: Semi structured database 15


XML Schema Documents
 Schema valid XML document describing a list of books

book.xml

Chapter 07: Semi structured database 16


XML Schema Documents
 Schema valid XML document describing a list of books

book.xsd

Specify the namespace of the


elements that this schema defines
Define the books element
Define the requirements
for any element of type
BooksType

An element of type BooksType must


contain one or more book elements,
which have type SingleBookType

A SingleBookType element
has a title element, which
contains a string

Chapter 07: Semi structured database 17


XML Schema Documents
 Example Schema
<?xml version="1.0“ encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="book“ type="BookType"/>

<xsd:complexType name="BookType">
<xsd:sequence>
<xsd:element name="title“ type="xsd:string"/>
<xsd:element name="author“ type="PersonType“ minOccurs="1“ maxOccurs="unbounded"/>
<xsd:element name="publisher“ type="xsd:anyType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="first“ type="xsd:string"/>
<xsd:element name="last“ type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

Chapter 07: Semi structured database 18


XML Schema Documents
 Example Schema

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
• Vocabulary of Schema defined in special Namespace. Prefix "xsd" is commonly used
• ” Schema” Element is always the Root

<xsd:element name="book" type="BookType"/>


• ”name” attribute defines the name of the element.
• ”type” defines the type of the element
• Declarations under ”schema” are global, "book" is the only global element,
root element of a valid document must be a "book".
– The type of a "book" is BookType.

<xsd:complexType name="BookType">
• User-defined type
• Defines a sequence of sub-elements
• Attribute "name" specifies name of Type
• This Type definition is global, BookType can be used in any other definition.

Chapter 07: Semi structured database 19


XML Schema Documents

 Example
• <xsd:element name="title“ type="xsd:string"/>

• ”xsd:string” is built-in type of XML Schema

• ” minOccurs", ” maxOccurs” specify cardinality of "author” Elements in BookType.


Default: minOccurs=1, maxOccurs=1

• Every book has exactly one “publisher”, minOccurs, maxOccurs by default 1


• ”anyType” is built-in Type , allows any content

• Attributes may only have a SimpleType


– Default values (default= "” )
– Required and optional attributes (use= ”required” , use= ”optional”)
– Fixed attributes (fixed= ”EUR”)

Chapter 07: Semi structured database 20


XML Schema Documents
 Xml schema
defining simple
and complex type

Chapter 07: Semi structured database 21


XML Schema Documents
 Xml document “shiporder.xml” using the schema

Chapter 07: Semi structured database 22


XML Schema Documents
 Some XML schema types

Chapter 07: Semi structured database 23


XML Schema Documents
 Some XML schema types

• Restrict domain
– minInclusive, maxInclusive are
"facets"

Chapter 07: Semi structured database 24


XML Schema Documents
 Schema validating

Chapter 07: Semi structured database 25


Extensible Stylesheet Language and XSL Transformations

• XSLT convert XML into any text-based document, can be used to transform
XML into HTML before it is displayed by a browser

• XSL documents have the extension .xsl

• XSLT is the recommended style sheet language of XML


– A string-based language of expressions used by XML and many of its related
technologies for efficiently locating structures and data (such as specific elements
and attributes)
– Used to locate parts of the source-tree document that match templates defined in
an XSL style sheet. When a match occurs (i.e., a node matches a template), the
matching template executes and adds its result to the result tree.

• For XSLT to function, the source tree must be properly structured


– Schemas, DTDs and validating parsers can validate document structure before
using XPath and XSLTs

Chapter 07: Semi structured database 26


Extensible Stylesheet Language and XSL Transformations
 Two tree structures are involved in transforming an XML document using XSLT
 source tree (the document being transformed)
 result tree (the result of the transformation)
 XPath character / (a forward slash)
 Selects the document root
 In XPath, a leading forward slash specifies that we are using absolute addressing
 An XPath expression with no beginning forward slash uses relative addressing
 XSL element value-of
 Retrieves an attribute’s value
 The @ symbol specifies an attribute node

Chapter 07: Semi structured database 27


XSL Transformations
 Example

XSLT that create


elements and sports.xsl

attributes in an
HTML document
Write the following HTML for each
game element in the sports element
that is contained in the root element

Write the value of the game’s


Write the value of the game’s
id attribute in a table cell
name child element in a table cell

Write the value of the game’s


paragraph child element in a table cell

Chapter 07: Semi structured database 28


XSL Transformations
 Example

The xml-stylesheet
declaration points to an
XSL style sheet for this
document

sports.xml

Chapter 07: Semi structured database 29


XSL Transformations
 XPath expression
Expression Description

/sports Matches all sports nodes that are child nodes of the
document root node.
/sports/game Matches all game nodes that are child nodes of sports,
which is a child of the document root.
/sports/game/name Matches all name nodes that are child nodes of game.
The game is a child of sports, which is a child of the
document root.
/sports/game/paragraph Matches all paragraph nodes that are child nodes of
game. The game is a child of sports, which is a child
of the document root.
/sports/game [@id=’239’] Matches the game node with the id number 239. The
game is a child of sports, which is a child of the
document root.
/sports/game [name='Cricket'] Matches all game nodes that contain a child element
whose name is Cricket. The game is a child of
sports, which is a child of the document root.

Chapter 07: Semi structured database 30


XQuery
 XQuery is the language for querying XML data
 XQuery for XML is like SQL for databases
 XQuery is built on XPath expressions
 XQuery Example

for $x in
doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

Chapter 07: Semi structured database 31


XQuery
The doc() function is used to open books.xml :
doc("books.xml")

select all the title elements in the "books.xml“


doc("books.xml")/bookstore/book/title

/bookstore selects the bookstore element, /book


selects all the book elements under the bookstore
element, and /title selects all the title elements
under each book element

The XQuery above will extract the following:

Chapter 07: Semi structured database 32


XQuery
 Predicates
 XQuery uses predicates to limit the extracted data from XML
documents.

 Example:
 The following predicate is used to select all the book elements under the
bookstore element that have a price element with a value less than 30:

 The XQuery above will extract the following:

Chapter 07: Semi structured database 33


Document Object Model
 Some XML parsers store document data as tree structures in
memory
 This hierarchical tree structure is called a Document Object Model
(DOM) tree, and an XML parser that creates this type of structure is
known as a DOM parser
 Each element name is represented by a node
 A node that contains other nodes is called a parent node
 A parent node can have many children, but a child node can have
only one parent node
 Nodes that are peers are called sibling nodes.
 The DOM tree has a single root node, which contains all the other
nodes in the document

Chapter 07: Semi structured database 34


Document Object Model
 window.ActiveXObject
 If this object exists, the browser is Internet Explorer
 Loads Microsoft’s MSXML parser is used to manipulate XML
documents in Internet Explorer

 Firefox loads each XML document asynchronously


 You must use the XML document’s onload property to specify a
function to call when the document finishes loading to ensure that you
can access the document’s contents

Chapter 07: Semi structured database 35


Document Object Model
 Tree structure for the document: article.xml

Chapter 07: Semi structured database 36


Document Object Model
 Common Node properties and methods 1/2
Property/Method Description

nodeType An integer representing the node type.


nodeName The name of the node.
nodeValue A string or null depending on the node type.
parentNode The parent node.
childNodes A NodeList (Fig. 14.28) with all the children
of the node.
firstChild The first child in the Node’s NodeList.
lastChild The last child in the Node’s NodeList.
previousSibling The node preceding this node; null if there is
no such node.
nextSibling The node following this node; null if there is
no such node.
attributes A collection of Attr objects (Fig. 14.31)
containing the attributes for this node.

Chapter 07: Semi structured database 37


Document Object Model
 Common Node properties and methods … 2/2

Property/Method Description

insertBefore Inserts the node (passed as the first argument)


before the existing node (passed as the second
argument). If the new node is already in the
tree, it is removed before insertion. The same
behavior is true for other methods that add
nodes.
replaceChild Replaces the second argument node with the
first argument node.
removeChild Removes the child node passed to it.
appendChild Appends the node it receives to the list of child
nodes.

Chapter 07: Semi structured database 38


Document Object Model
 Document properties and methods

Property/Method Description

documentElement The root node of the document.


createElement Creates and returns an element node with the specified
tag name.
createAttribute Creates and returns an Attr node (Fig. 14.31) with
the specified name and value.
createTextNode Creates and returns a text node that contains the
specified text.
getElementsBy TagName Returns a NodeList of all the nodes in the subtree
with the name specified as the first argument, ordered
as they would be encountered in a preorder traversal.
An optional second argument specifies either the
direct child nodes (0) or any descendant (1).

Chapter 07: Semi structured database 39


Document Object Model
 Element property and methods

Property/Method Description

tagName The name of the element.


getAttribute Returns the value of the specified attribute.
setAttribute Changes the value of the attribute passed as the first
argument to the value passed as the second argument.
removeAttribute Removes the specified attribute.
getAttributeNode Returns the specified attribute node.
setAttributeNode Adds a new attribute node with the specified name.

Chapter 07: Semi structured database 40


Document Object Model
 Attr properties
Property Description
value The specified attribute’s value.
name The name of the attribute.

 Text methods

Property Description

data The text contained in the node.


length The number of characters contained in the node.

Chapter 07: Semi structured database 41


Document Object Model

xpath.html

(1 of 3)

Chapter 07: Semi structured database 42


Document Object Model
 Using Xpath to locate nodes in an XML document

Chapter 07: Semi structured database 43


Document Object Model
 Using Xpath to locate nodes in an XML document

Chapter 07: Semi structured database 44


Document Object Model
 Using Xpath to locate nodes in an XML document

xpath.html

(1 of 3)

Chapter 07: Semi structured database 45


Document Object Model
 Using Xpath to locate nodes in an XML document

xpath.html

(2 of 3)

Chapter 07: Semi structured database 46


Document Object Model

xpath.html

(3 of 3)

Chapter 07: Semi structured database 47


DOM

sports.xml

Chapter 07: Semi structured database 48

You might also like