XML Documents and Database

Different approaches for storing XML  documents are as given below:
  • A RDBMS or object-oriented database management system is used to store XML document in the form of text.
  • A tree model is very useful to store data elements located at leaf level in tree structure.
  • The large amount of data is stored in the form of relational database or object-oriented databases. A middleware software is used to manage communication between XML document and relational database.

XML Query:

XML query is based on two methods.

1. Xpath:
  • Xpath is a syntax for defining parts or elements of XML documents.
  • Xpath is used to navigate between elements and attributes in the XML documents.
  • Xpath uses path of expressions to select nodes in XML documents.
Example:
In this example, the tree structure of employee is represented in the form of XML document.

<Employee>
     <Name>
          <First name>Brian</First name>
          <Last name>Lara<</Last name>
     </Name>
     <Contact>
          <Mobile>9800000000</Mobile>
          <Landline>020222222</Landline>
     </Contact>
     <Address>
          <City>Pune</city>
          <Street>Tilak road</Street>
          <Zip code>4110</Zip code>
     </Address>
</Employee>


In the above example, a (/) is used as </Employee>  where Employee is a root node and Name, Contact and Address are descendant nodes.

2. Xquery:
  • Xquery is a query and functional programming language. Xquery provides a facility to extract and manipulate data from XML documents or any data source, like relational database.
  • The Xquery defines FLWR expression which supports iteration and binding of variable to intermediate results.
    FLWR is an abbreviation of FOR, LET, WHERE, RETURN. Which are explained as follows:
    FOR: Creates a sequence of nodes.
    LET: Binds a sequence to variable.
    WHERE: Filters the nodes on condition.
    RETURN: It is query result specification.

Xquery comparisons:

The two methods for Xquery comparisons are as follows:

1. General comparisons: =,  !=, <=, >, >=
Example:
In this example, the expression (Query) can return true value if any attributes have a value greater than or equal to 15000.
$ TVStore//TV/price > =15000

2. Value comparisons: eq, ne, lt, le, gt , ge
Example:
In this example, the expression (Query) can return true value, if there is only one attribute returned by the expression, and its value is equal to 15000.
$ TVStore//TV/price eq 15000

Example:
Lets take an example to understand how to write a XMLquery.

FOR $x in doc (“student.xml”)/student information /marks
WHERE $x/marks >700
RETURN <res>
                    $x/Name
               </res>


The queries in the above example can return true value ( Name of the students) only if the value (marks) is greater than 700.