Databases:

XML Storage Options:

  • XML Columns: Some databases have a native XML data type for columns so you can store XML documents directly in the database.
  • Text/BLOB Columns: XML can be stored as text or binary large objects (BLOBs) in databases that don’t have a native XML type.
  • Relational Mapping: XML data can be mapped to relational tables where XML elements and attributes are stored in columns.

Example Using SQL Server (with Native XML Type):

                    
CREATE TABLE Books (
    BookID INT PRIMARY KEY,
    BookInfo XML
);

INSERT INTO Books (BookID, BookInfo)
VALUES (1, '<book><title>XML Fundamentals</title><author>John Smith</author><price>29.99</price></book>');

SELECT BookInfo
FROM Books
WHERE BookID = 1;

                    
                  

Example Using MySQL (Storing XML as Text):

                    
CREATE TABLE Books (
    BookID INT PRIMARY KEY,
    BookInfo TEXT
);

INSERT INTO Books (BookID, BookInfo)
VALUES (1, '<book><title>XML Fundamentals</title><author>John Smith</author><price>29.99</price></book>');

SELECT BookInfo
FROM Books
WHERE BookID = 1;
                    
                  

Storing and Retrieving XML Data in Databases:

XQuery is a language to query and manipulate XML data. It is used in databases that support XML to extract and manipulate XML data.

Example XQuery:

                    
let $books := 
    <bookstore>
        <book>
            <title>XML Fundamentals</title>
            <author>John Smith</author>
            <price>29.99</price>
        </book>
        <book>
            <title>Advanced XML</title>
            <author>Jane Doe</author>
            <price>49.99</price>
        </book>
    </bookstore>

return $books/book[price < 40]
                    
                  

This XQuery example retrieves books with a price less than 40.


Example Using XQuery in SQL Server:

                    
DECLARE @books XML
SET @books = '<bookstore>
                <book>
                    <title>XML Fundamentals</title>
                    <author>John Smith</author>
                    <price>29.99</price>
                </book>
                <book>
                    <title>Advanced XML</title>
                    <author>Jane Doe</author>
                    <price>49.99</price>
                </book>
              </bookstore>'

SELECT @books.query('/bookstore/book[price < 40]')
                    
                  

Using XML Data Types in SQL Databases:

  • Validation: Ensures stored XML data is well formed and optionally validated against a schema.
  • Indexing: Allows you to index XML elements and attributes for faster querying.
  • XQuery: Allows you to use XQuery to query XML data within SQL.

Example Using SQL Server:

                    
-- Create table with XML data type
CREATE TABLE Books (
    BookID INT PRIMARY KEY,
    BookInfo XML
);

-- Insert XML data
INSERT INTO Books (BookID, BookInfo)
VALUES (1, '<book><title>XML Fundamentals</title><author>John Smith</author><price>29.99</price></book>');

-- Query XML data
SELECT BookInfo.value('(/book/title)[1]', 'VARCHAR(50)') AS Title
FROM Books
WHERE BookID = 1;

-- Validate XML data against an XML schema
DECLARE @xml XML
SET @xml = '<book><title>XML Fundamentals</title><author>John Smith</author><price>29.99</price></book>'

DECLARE @schema XML
SET @schema = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
                  <xs:element name="book">
                    <xs:complexType>
                      <xs:sequence>
                       <xs:element name="title" type="xs:string"/>
                       <xs:element name="author" type="xs:string"/>
                       <xs:element name="price" type="xs:decimal"/>
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
               </xs:schema>'

SELECT @xml.value('(/book/title)[1]', 'VARCHAR(50)')
WHERE @xml.exist('(/book)[1]') = 1;

                    
                  

Example Using MySQL (with XML Stored as Text):

                    
-- Create table with TEXT data type
CREATE TABLE Books (
    BookID INT PRIMARY KEY,
    BookInfo TEXT
);

-- Insert XML data
INSERT INTO Books (BookID, BookInfo)
VALUES (1, '<book><title>XML Fundamentals</title><author>John Smith</author><price>29.99</price></book>');

-- Query XML data using XPath functions
SELECT ExtractValue(BookInfo, '/book/title') AS Title
FROM Books
WHERE BookID = 1;