24 XML Databases
Dr R. Baskaran
IDs & IDREFs
- 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
- 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 ouble-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
- 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>
- 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.
- 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 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
- 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
- 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
- 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
- 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.
String Representation
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
- 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
Tree Representation
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
Native XML databases
- These databases are typically better when much of the data is in XML or other non-relational formats. BaseX, Berkeley DB XML edition, eXist, MarkLogic Server, Qizx, Sedna
- All the above databases uses XML as an interface to specify documents as tree structured data that may contain unstructured text, but on disk the data is stored as “optimized binary files.”
- This makes query and retrieval faster.
- For MarkLogic it also allows XML and JSON to co-exist in one binary format.
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.
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