1 XML and Database. Objectives 2 Types of XML Databases Mapping Document Schema to Database Schema...

18
1 XML and Database XML and Database

Transcript of 1 XML and Database. Objectives 2 Types of XML Databases Mapping Document Schema to Database Schema...

Page 1: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

1

XML and Database XML and Database

Page 2: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Objectives

2

Types of XML Databases Mapping Document Schema to Database Schema Query Language Workshops

Page 3: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

XML as DB

XML is suitable for database because it handles the data in similar fashion like relational database.

The XML database allows data to be manipulated in the XML format.

The reason to use XML as database are: Easier to store data in XML format. Wider use of XML for data transport. Minimal cost of data conversion.

XPath is used to navigate XML documents and is also used for finding information in an XML document using path expression

XQuery, that is built on XPath expressions, is used for finding and extracting elements and attributes from XML documents

Page 4: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

XML DB

The XML database Can be used more efficiently in comparison to other conventional

databases when dealing with small amount of data, few users, and a moderate performance requirement.

Has some advantages over the conventional database It is portable. It is self-describing in nature. It stores data in nodes. It allows nested and multiple data entries.

There are two types: XML-Enabled: maps all XML data to a traditional database Native XML

Depends on XML & uses XML documents & elements as the central unit of storage.

The benefits It keeps data in one place. It is faster in terms of performance. It can store very large documents.

Page 5: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Document

The XML document database Is a collection of XML documents and its parts maintained by a

system. May be a database of structures documents or unstructured data.

There are two types Data-Centric Documents

An XML document is used as storage for data that is structured and appears in a regular order.

Is similar structure throughout the document Document-Centric Documents

XML is used to create semi-structured documents with irregular content. Is irregular structure throughout the document

Page 6: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Data-Centric

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

<player><name>David Beckham</name><matches>50</matches><goals>70</goals><penalties>15</penalties>

</player><player>

<name>Diego Maradona</name><matches>200</matches><goals>100</goals><penalties>30</penalties>

</player></soccer>

Page 7: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Document Centric

<?xml version=“1.0”?><Book>

<Chapter>Networking</Chapter><Objective>Introduction></Objective><Objective>Introduction to networks</Objective><Objective>Introduction to TCP/IP</Objective><Objective>IP Addressing</Objective><para>

In the previous chapter, we have discussed the JDBC and database access, the JDBC architecture and JDBC-ODBC driver. The four different drivers of JDBC are:</para>

</Book>

Page 8: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Relation DB

An XML document can be considered as a relational database The document node is considered as a database. The element nodes are considered as tables in a database. An element node along with its only child node of type data node

is considered as a field in a table. The child nodes of an element node are considered as records in a

table. Absence of a field node in an XML document is considered as null

value.

Page 9: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Native XML DBs

Some of the features Allow Data Storage in XML Format Enables Query for Data in Collections Provides Query Language to Access Data Enables Updating Data in Collections

Page 10: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Native XML DBs (cont)

Advantages Have robust features to maintain data stored in databases The storage performance is very high, so that you can retrieve the

original and unparsed document piece by piece The partial retrieval feature is important in development

particularly to identify bugs and enhance performance Enable to sort the data stored in database using an indexing

technique Can jump directly to the ID value of the data rather than traversing

the whole tree Can store any type of XML data but this is not the true

replacement of the existing database systems Is not used as a stand-alone database

Page 11: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Table-based mapping

Uses the data in an XML document & maps it with a relational DB. Is efficient to handle similar type of data. Is used for exchanging data between XML documents & RDBMS. Advantages

Matching the structure of table is easier. Coding is simpler. Accessing data is faster.

Disadvantages It works on a small part of an XML document. It cannot maintain the physical structure of an XML document

Page 12: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

XML Object mapping

The object relational mapping uses XML documents as a tree of objects. These objects are specific to the XML document, which are mapped to the database.

Is used due to some drawback, such as limited access to XML documents, of the table-based mapping and use of middleware tools.

Is highly vulnerable to problems while handling mixed contents

Page 13: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Query Language

Template-Based Query Language Template is a system, which is used to rapidly generate some outputs through a

pre-defined program. Template-based query generates the content from XML sources and produces

one result per child in the root node. The query deals with many aspects such as:

Variables: point to an attribute set on the node of an XML database Attributes: are used with the XPath expression to filter the XML data from database Parameters: are used to get the desired result from the database

SQL-Based Query Language Such as XQuery and SQL/XML, use portable queries to get XML data SQL/XML

Is easy to use because it uses some additional queries from the existing SQL language.

Are completely different technologies used in the industry though there is a similarity in the name.

Is a technology used in SQL Server to import XML data into an SQL Server db

Page 14: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

XQuery

Is developed by the XML Query Working Group Is extensively used as a query language for XML. Is particularly designed to create queries not only on XML data but

also on any data presented in XML format. Features

It is used for querying XML data. It is built on XPath expressions. It extracts information using Web services. It is used to transform XML data to XHTML. It is case-sensitive.

Some of the specifications of XQuery are Elements, attributes, and variables should use valid XML names. String values should be enclosed in a single or double quotes. Variables should begin with a $ sign. Comments should be delimited by:. Keywords are case sensitive and should be of lowercase characters

Page 15: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Example<?xml version="1.0" encoding="UTF-8"?><table><row> <id>0001</id> <firstname>Bob</firstname> <lastname>Aranow</lastname> <street>2 Any St.</street> <city>Anytown</city> <state>NY</state> <zip>22000</zip> </row> <row> <id>0002</id> <firstname>Charles</firstname> <lastname>Aranow</lastname> <street>3 Any St.</street> <city>Anytown</city> <state>AL</state> <zip>22000</zip> </row></table>

Page 16: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Example (cont)

public class XQueryTableMappingEx {public static void main (String[] args) {

try { Document doc = new Builder().build (new File(“Person.xml”)); Nodes node = XQueryUtil.xquery (doc, "//table/*"); System.out.println("Out of " + node.size () + " people "); node = XQueryUtil.xquery (doc, "//table/row[state=‘NY']"); System.out.println(node.size () + " stay in " + args[1]); }catch(Exception e){ e.printStackTrace (); } } }

Page 17: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

Data integrating issues

Keeping data as a single unit is known as data integrity. Some of the measures that are available in XML to handle data

integrity issues are: XML Validation:

The content, structure, and data types of an XML document are validated. Must be performed when data integrity is required. Helps the XML data to be well formed and also checks for the semantic

mistakes in XML document. Check Constraints

Helps to put certain restrictions on the XML columns. The insertion or updating of data in the XML column can be performed only

when the constraint criteria is satisfied. Trigger Processing

A trigger is a set of statements, which automatically performs an action when a specific operation, such as insert, update, or delete occurs in a table.

Triggers have two parts, event and action is used to maintain the data integrity by checking data in a consistent manner.

Page 18: 1 XML and Database. Objectives 2  Types of XML Databases  Mapping Document Schema to Database Schema  Query Language  Workshops.

WORKSHOP ACTIVITIES

Building the console Java application using XQuery can do

•Query the XML document with get the introduction of the inputted country

•Query to find number of people living at the inputted state from XML document