23 XML Database
Dr R. Baskaran
Introduction
XML: Extensible Markup Language.
Defined by the WWW Consortium (W3C).
Derived from SGML (Standard Generalized Markup Language), but simpler to use than HTML .
Documents have tags giving extra information about sections of the document.
E.g. <title> XML </title> <slide> Introduction …</slide>
Extensible, unlike HTML
Users can add new tags, and separately specify how the tag should be handled for display.
The ability to specify new tags, and to create nested tag structures make XML a great way to exchange data, not just documents.
Much of the use of XML has been in data exchange applications, not as a replacement for HTML.
Tags make data (relatively) self-documenting.
<university>
<department>
<dept_name> CSE </dept_name>
<faculty> 50 </faculty>
<students> 500 </students>
</department>
<course>
<course_code> CS8204 </course_id>
<title> DBMS </title>
<dept_name> CSE </dept_name>
<credits> 4 </credits>
</course>
</university>.
- Data interchange is critical in today’s networked world
- Examples:
- Banking: funds transfer
- Order processing (especially inter-company orders)
- Scientific data
- Chemistry: ChemML, …
- Genetics: BSML (Bio-Sequence Markup Language)
- Each application area has its own set of standards for representing information.
- XML has become the basis for all new generation data interchange formats.
- XML is better than relational tuples as a data-exchange format.
- Unlike relational tuples, XML data is self-documenting due to presence of tags.
- Non-rigid format: tags can be added.
- Allows nested structures.
- Wide acceptance, not only in database systems, but also in browsers, tools, and applications.
Tag: label for a section of data
Element: section of data beginning with <tagname> and ending with matching </tagname>
Elements must be properly nested
Proper nesting
<course> … <title> …. </title> </course>
Improper nesting
<course> … <title> …. </course> </title>
Formally: every start tag must have a unique matching end tag, that is in the context of the same parent element.
Every document must have a single top-level element
- Nesting of data is useful in data transfer.
- Example: elements representing student nested within an department
- Nesting is appropriate when transferring data.
- External application does not have direct access to data referenced by a foreign key.
- Nesting is not supported, or discouraged, in relational databases.
- With multiple orders, customer name and address are stored redundantly.
- normalization replaces nested structures in each order by foreign key into table storing customer name and address information.
- Nesting is supported in object-relational databases.
- Elements can have attributes. <course course_code= “CS8204”>
<title> DBMS </title>
<dept_name> CSE </dept_name> <credits> 3 </credits>
</course>
- Attributes are specified by name-value pairs inside the starting tag of an element.
- An element may have several attributes, but each attribute name can only occur once
<course course_code = “CS8204” credits=“3”>
In the context of documents, attributes are part of markup, while sub-element contents are part of the basic document contents.
In the context of data representation, the difference is unclear and may be confusing.
Same information can be represented in two ways
<course course_code= “CS8204”>
…
</course>
<course>
<course_code>CS8204</course_code> …
</course>
- XML data has to be exchanged between organizations.
- Same tag name may have different meaning in different organizations, causing confusion on exchanged documents.
- Specifying a unique string as an element name avoids confusion.
- Better solution: use unique-name:element-name.
- Avoid using long unique names all over document by using XML Namespaces
<university xmlns:anna_univ=“http://www.annauniv.edu”>…
<annauniv:course>
<annauniv:course_code> CS8204 </annauniv:course_code> <annauniv:title> DBMS </annauniv:title> <annauniv:dept_name> CSE </annauniv:dept_name> <annauniv:credits> 3 </annauniv:credits>
</annauniv:course>
…
</university>
Elements without sub-elements or text content can be abbreviated by ending the start tag with a /> and deleting the end tag
<course course_code=“CS8204” Title=“DBMS” dept_name = “CSE” credits=“3” />
To store string data that may contain tags, without the tags being interpreted as sub-elements, use CDATA as below
<![CDATA[<course> … </course>]]>
Here, <course> and </course> are treated as just strings CDATA stands for “character data”
- Database schemas constrain what information can be stored, and the data types of stored values.
- XML documents are not required to have an associated schema.
- However, schemas are very important for XML data exchange.
- Otherwise, a site cannot automatically interpret data received from another site.
- Two mechanisms for specifying XML schema.
Document Type Definition (DTD)
Widely used
XML Schema
- Newer, increasing use
- The type of an XML document can be specified using a DTD.
- DTD constraints structure of XML data.
- What elements can occur?
- What attributes can/must an element have?
- What subelements can/must occur inside each element, and how many times?
- DTD does not constrain data types.
- All values represented as strings in XML.
- DTD syntax.
- <!ELEMENT element (subelements-specification) >
- <!ATTLIST element (attributes)
- Sub-elements can be specified as
- names of elements, or
- #PCDATA (parsed character data), i.e., character strings
- EMPTY (no subelements) or ANY (anything can be a subelement)
- Example
- <! ELEMENT department (dept_name building, budget)> <! ELEMENT dept_name (#PCDATA)>
- ! ELEMENT budget (#PCDATA)>
- Subelement specification may have regular expressions
<!ELEMENT university ( ( department | course | faculty | teaches )+)>
- Notation:
- “|” – alternatives
- “+” – 1 or more occurrences
- “*” – 0 or more occurrences
- An element can have at most one attribute of type ID.
- The ID attribute value of each element in an XML document must be distinct.
- Thus, the ID attribute value is an object identifier.
- An attribute of type IDREF must contain the ID value of an element in the same document.
- An attribute of type IDREFS contains a set of (0 or more) ID values. Each ID value must contain the ID value of an element in the same document.
Example
<university>
<department dept_name=“CSE”>
<faculty> 50 </faculty>
<students> 500 </students>
</department>
<course course_code=“CS8204” dept_name=“CSE” course_incharge=“54783”> <title> DBMS </title>
<credits> 3 </credits>
</course>
….
<course_incharge staff_id=“54783” dept_name=“CSE”> <name> Arvind </name>
<salary> 75000 </salary>
</course_incharge>
….
</university>
XML Schema
- XML Schema is a more sophisticated schema language which addresses the drawbacks of DTDs. Supports
- Typing of values
- Eg. integer, string, etc
- Also, constraints on min/max values
- User-defined, complex types
- Many more features, including
- uniqueness and foreign key constraints, inheritance
- XML Schema is itself specified in XML syntax, unlike DTDs
- More-standard representation, but verbose
- XML Scheme is integrated with namespaces
- BUT: XML Schema is significantly more complicated than DTDs.
Rationale for XML in Databases
- There are a number of reasons to directly specify data in XML or other document formats such as JSON.
- For XML in particular, they include:
- An enterprise may have a lot of XML in an existing standard format.
- Data may need to be exposed or ingested as XML, so using another format such as relational forces double-modeling of the data.
- XML is very well suited to sparse data, deeply nested data and mixed content (such as text with embedded markup tags).
- XML is human readable whereas relational tables require expertise to access.
- Metadata is often available as XML.
- Semantic web data is available as RDF/XML.
XML in Databases
Steve O’Connell gives one reason for the use of XML in databases: the increasingly common use of XML for data transport hich has meant that “data is extracted from databases and put into XML documents and vice-versa”.
In content-based applications, the ability of the native XML database also minimizes the need for extraction or entry of metadata to support searching and navigation.
Querying and Transforming XML Data
- Translation of information from one XML schema to another
- Querying on XML data
- Above two are closely related, and handled by the same tools
- Standard XML querying/translation languages
- XPath
- Simple language consisting of path expressions
- XSLT
- Simple language designed for translation from XML to XML and XML to HTML
- XQuery
- An XML query language with a rich set of features
Tree Model of XML Data
- Query and transformation languages are based on a tree model of XML data.
- An XML document is modeled as a tree, with nodes corresponding to elements and attributes.
- Element nodes have child nodes, which can be attributes or subelements.
- Text in an element is modeled as a text node child of the element
- Children of a node are ordered according to their order in the XML document.
- Element and attribute nodes (except for the root node) have a single parent, which is an element node.
- The root node has a single child, which is the root element of the document.
XPath
- XPath is used to address (select) parts of documents using path expressions
- A path expression is a sequence of steps separated by “/”
- Think of file names in a directory hierarchy
- Result of path expression: set of values that along with their containing elements/attributes match the specified path
E.g. /university/course_incharge/name evaluated on the university data we saw earlier returns
<name>Arvind</name>
<name>Balu</name>
E.g./university/course_incharge/name/text( )
returns the same names, but without the enclosing tags Arvind, Balu
XQuery
- XQuery is a general purpose query language for XML data
- XQuery is derived from the Quilt query language, which itself borrows from SQL, XQL and XML-QL
- XQuery uses a
for … let … where … order by …result …
syntax
for ó SQL from
where ó SQL where
order by ó SQL order by
result ó SQL select
let allows temporary variables, and has no equivalent in SQL
XSLT
- A stylesheet stores formatting options for a document, usually separately from document.
- E.g. an HTML style sheet may specify font colors and sizes for headings, etc.
- The XML Stylesheet Language (XSL) was originally designed for generating HTML from XML.
- XSLT is a general-purpose transformation language.
- Can translate XML to XML, and XML to HTML.
- XSLT transformations are expressed using rules called templates.
- Templates combine selection using XPath with construction of results.
Application Program Interface
There are two standard application program interfaces to XML data:
SAX (Simple API for XML)
- Based on parser model, user provides event handlers for parsing events
- E.g. start of element, end of element
- DOM (Document Object Model)
- XML data is parsed into a tree representation
- Variety of functions provided for traversing the DOM tree
- E.g.: Java DOM API provides Node class with methods getParentNode( ), getFirstChild( ), getNextSibling( ) getElementsByTagName( ), …
- Also provides functions for updating DOM tree
Storage of XML Data
- XML data can be stored in
- Non-relational data stores
- Flat files
- Natural for storing XML
- But has all problems discussed in Chapter 1 (no concurrency, no recovery, …)
- XML database
- Database built specifically for storing XML data, supporting DOM model and declarative querying
- Currently no commercial-grade systems
- Relational databases
- Data must be translated into relational form
- Advantage: mature database systems
- Disadvantages: overhead of translating data and queries
Storage of XML in Relational Databases
- Alternative Representations :
- String Representation
- Tree Representation
- Map to relations
String Representation
- Store each top level element as a string field of a tuple in a relational database.
- Use a single relation to store all elements, or
- Use a separate relation for each top-level element type.
- E.g. account, customer, depositor relations.
- Each with a string-valued attribute to store the element.
- Indexing:
- Store values of subelements/attributes to be indexed as extra fields of the relation, and build indices on these fields.
- E.g. customer_name or account_number.
- Some database systems support function indices, which use the result of a function as the key value.
- The function should return the value of the required subelement/attribute.
Benefits:
- Can store any XML data even without DTD
- As long as there are many top-level elements in a document, strings are small compared to full document
- Allows fast access to individual elements.
Drawback:
- Need to parse strings to access values inside the elements
- Parsing is slow.
- Tree representation: model XML data as tree and store using relations nodes(id, parent_id, type, label, value)
- Each element/attribute is given a unique identifier
- Type indicates element/attribute
- Label specifies the tag name of the element/name of attribute
- Value is the text value of the element/attribute
- Can add an extra attribute position to record ordering of children
Benefit:
- Can store any XML data, even without DTD.
Drawbacks:
- Data is broken up into too many pieces, increasing space overheads.
- Even simple queries require a large number of joins, which can be slow.
Mapping XML data to Relations
- Relation created for each element type whose schema is known:
- An id attribute to store a unique id for each element.
- A relation attribute corresponding to each element attribute.
- A parent_id attribute to keep track of parent element.
- As in the tree representation.
- Position information (ith child) can be store too.
- All sub-elements that occur only once can become relation attributes.
- For text-valued sub-elements, store the text as attribute value.
- For complex sub-elements, can store the id of the sub-element.
- Sub-elements that can occur multiple times represented in a separate table.
- Similar to handling of multivalued attributes when converting ER diagrams to tables.
Storing XML Data in Relational Systems
- Applying above ideas to department elements in university-1 schema, with nested course elements, we get department(id, dept_name, faculty, students) course(parent id, course_code, dept_name, title, credits)
- Publishing: process of converting relational data to an XML format
- Shredding: process of converting an XML document into a set of tuples to be inserted into one or more relations
- XML-enabled database systems support automated publishing and shredding
- Many systems offer native storage of XML data using the xml data type. Special internal data structures and indices are used for efficiency
XML Enabled databases
XML enabled databases typically offer one or more of the following approaches to storing XML within the traditional relational structure:
- XML is stored into a CLOB (Character Large OBject).
- XML is `shredded` into a series of Tables based on a Schema.
- XML is stored into a native XML Type as defined by ISO Standard 9075-14.
RDBMS that support the ISO XML Type are:
- IBM DB2 (pureXML)
- Microsoft SQL Server.
- Oracle Database.
- PostgreSQL
XML Enabled databases
XML enabled databases typically offer one or more of the following approaches to storing XML within the traditional relational structure:
- XML is stored into a CLOB (Character Large OBject).
- XML is `shredded` into a series of Tables based on a Schema.
- XML is stored into a native XML Type as defined by ISO Standard 9075-14.
RDBMS that support the ISO XML Type are:
- IBM DB2 (pureXML)
- Microsoft SQL Server.
- Oracle Database.
- PostgreSQL
Key Features
- Has an XML document as at least one fundamental unit of (logical) storage, just as a relational database has a Row in a table as a fundamental unit of (logical) storage.
- Need not have any particular underlying physical storage model.
- For example, NXDs can use optimized, proprietary storage formats.
- This is a key aspect of XML databases.
- Managing XML as large strings is inefficient due to the extra markup in XML.
- Compressing and indexing XML allows the illusion of directly accessing, querying and transforming XML while gaining the performance advantages of working with optimized binary tree structures.
Language Features
Supported API
XML Database Applications
- Storing and exchanging data with complex structures
- g. Open Document Format (ODF) format standard for storing Open Office and Office Open XML (OOXML) Format standard for storing Microsoft Office documents
- Numerous other standards for a variety of applications
- ChemML, MathML
XML Database Applications
Standard for data exchange for Web services
- remote method invocation over HTTP protocol Data mediation
- Common data representation format to bridge different systems
Summary
- Introduction to XML
- XML Syntax
- Querying and Transforming XML data
- Different representations of XML in relational data
- Storing XML data in Relational Systems
- XML Database Applications