What is XQuery?
|
XQuery is stands for XML Query. XQuery 1.0 has W3c recommendation Since 23 Jan 2007. We use XQuery to solve the queries of XML database. Some basic information about XQuery are: 1.Using XQuery we can solve the queries of an XML data. 2.We can say work of XQuery for XML are same as SQL for database. 3.XQuery is compatible with database engines like: Oracle, Microsoft etc. 4.XQuery can work with many W3C standardds like: XML, XPath, XML Schema and XSLT.
|
WHich type of problems we can solved with XQuery?
|
We use XQuery to solve some queries in an XML data like: 1. Use XQuery to retrieve information in a web service. 2. Use XQuery to generate summary report. 3. Using XQuery we can transform data from XML to XHTML. 4. Use XQuery when we want some relevant information from Web documents. |
How to perform operation on node using XQuery function in XML?Explain with an example?
|
To explain given problem first I written a example in XML. Example: Save the given XML file with \"bookmart.xml\"
<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <bookstore> <book category=\"CHILDREN\"> <title lang=\"en\">Jungle Book</title> <author>Rudyard Kipling</author> <year>1894</year> <price>99.89</price> </book> <book category=\"ADVENTURE\"> <title lang=\"en\">The Book of the Thousand Nights and a Night </title> <author>Sir Richard Francis Burton</author> <year>1885-88</year> <price>149.99</price> </book> <book category=\"WEB PROGRAMING\"> <title lang=\"en\">XPath and XPointer</title> <author>John E. Simpson</author> <year>2002</year> <price>299.99</price> </book> <book category=\"WEB PROGRAMING\"> <title lang=\"en\">XML Programing</title> <author>R.Allen Wype</author> <author>Sultan Rehman</author> <author>Brad Leupen</author> <year>2002</year> <price>229.95</price> </book> </bookstore>
Now, I given you how to perform operations on node with XQuery. 1. XQuery uses doc() function to retrieve data from XML document. If you want to select all title elements of \"bookmart.xml\". Than you should write like that, doc(\"bookmart.xml\")/bookstore/book/title When this XQuery run than it will get like that, <title lang=\"en\">Jungle Book</title> <title lang=\"en\">The Book of the Thousand Nights and a Night</title> <title lang=\"en\">XPath and XPointer</title> <title lang=\"en\">XML Programing</title>
2. Using XQuery we can predicate the limit of an exiracted data from XML documents. Now, We write a predicate for XQuery in which we got all book elements those price have less than 150. doc(\"books.xml\")/bookstore/book[price<150] Output is like that,
<book category=\"CHILDREN\"> <title lang=\"en\">Jungle Book</title> <author>Rudyard Kipling</author> <year>1894</year> <price>99.89</price> </book> <book category=\"ADVENTURE\"> <title lang=\"en\">The Book of the Thousand Nights and a Night </title> <author>Sir Richard Francis Burton</author> <year>1885-88</year> <price>149.99</price> </book>
|
How to use FLWOR expression given by XQuery in XML data?
|
We use FLWOR expression with XQuery like that,
for $a in doc(\"bookmart.xml\")/bookstore/book where $a/price<150 return $a/title
We use above expression to select all title elements in bookmart element from bookstore element those has price smaller than 150. Output: <title lang=\"en\">Jungle Book</title> <title lang=\"en\">The Book of the Thousand Nights and a Night </title> We can also acheive this by using path expression like that, doc(\"bookmart.xml\")/bookstore/book[price<150]/title
Now, We can also sort this like that, for $a in doc(\"books.xml\")/bookstore/book where $a/price<150 order by $a/title return $a/title
output: <title lang=\"en\">Jungle Book</title> <title lang=\"en\">The Book of the Thousand Nights and a Night </title> Where, for : is used to select all book elements from bookstore into variable $a. where : is used to select only those book elements with a price element those are less than 150. order by : is used to inform that sorting is performed by title element. return : used to return the title elements.
|
How you use XQuery FLWOR with HTML?
|
Consider previous example.If you want to find list of all titles from book in our bookstore with in HTML.Than we should have to add <ul> and <li> tags into the FLWOR expression.
<ul> { for $a in doc(\"bookmart.xml\")/bookstore/book/title order by $a return <li>{$a}</li> } </ul> Output: <ul> <li><author>Rudyard Kipling</author></li> <li><author>Sir Richard Francis Burton</author></li> <li><author>John E. Simpson</author></li> <li><author>R.Allen Wype</author></li> <li><author>Sultan Rehman</author></li> <li><author>Brad Leupen</author></li> </ul>
You can also data in an HTML list when you do like: <ul> { for $a in doc(\"bookmart.xml\")/bookstore/book/title order by $a return <li>{data($a)}</li> } </ul> Output: <ul> <li>Rudyard Kipling</li> <li>Sir Richard Francis Burton</li> <li>John E. Simpson</li> <li>R.Allen Wype</li> <li>Sultan Rehman</li> <li>Brad Leupen</li> </ul>
|
Give us syntax rules of XQuery?
|
I have given you some basic syntax rules of XQuery. 1. XQuery is an case sensitive. 2. In XQuery elements , attributes and variables that we used should have valid XML names. 3. Should write XQuery with in single and double quotes. 4. We defined XQuery variable with like: $(variable name).e.g. $book. 5. We can write XQuery comments between colon(:). like:(:Comment in XQuery:) |
How to perform conditional operations in XQuery?
|
I have given you how to use conditions in XQuery. For example I told you how to use \"If-Then-Else\" expressions in XQuery. Example: for $a in doc(\"bookmart.xml\")/bookstore/book return if ($a/@category=\"ADVENTURE\") then <child>{data($a/title)}</child> else <adult>{data($a/title)}</adult>
Output: <adult>Jungle Book</adult> <child>The Book of the Thousand Nights and a Night </child> <adult>XML Programing</adult> <adult>XPath and XPointer</adult>
|
How to perform comparisons in XQuery?
|
In XQuery we can compare the values with general comparisons and value comparisons.
1. General Comparisons : We perform general comparisons by using these symbols. Ex. =, !=, <, <=, >, >=
2. Value Comparisons : We use these symbols to perform value comparisons. Ex. ne, lt, le, eq, ge, gt.
Now, I given you how to use comparisons in XQuery. $bookstore//book/@q > 100 If any q attributes have values which is greater than 100 expression will return true.
$bookstore//book/@q gt 100 if there is only one q attribute returned by the expression and also its value is greater than 100 expression return true. If there are more than one than it will return an error occurs.
|
How to add elements and attributes with XQuery in XML data?
|
Consider the example \"bookmart.xml\".I write an expression to return litle and lang elements both. for $a in doc(\"bookmart.xml\")/bookstore/book/title order by $a return $a
Output: <title lang=\"en\">Jungle Book</title> <title lang=\"en\">The Book of the Thousand Nights and a Night</title> <title lang=\"en\">XPath and XPointer</title> <title lang=\"en\">XML Programing</title> Now, I will told you how to add elements and attributes in XML document by using XQuery.
To add elements using XQuery : I have given you example to add some HTML elements.Like: <html> <body> <h1>Bookstore</h1> <ul> { for $a in doc(\"bookmart.xml\")/bookstore/book order by $a/title return <li>{data($a/title)}. Category: {data($a/@category)}</li> } </ul> </body> </html>
Now, the output generated through this XQuery will be: <html> <body> <h1>Bookstore</h1> <ul> <li>Jungle Book. Category: CHILDREN</li> <li>The Book of the Thousand Nights and a Night. Category: ADVENTURE</li> <li>XPath and XPointer. Category: WEB PROGRAMING</li> <li>XML Programing. Category: WEB PROGRAMING</li> </ul> </body> </html>
To add attributes using XQuery : I explain you with an example.In this example we add attributes into an HTML elements. <html> <body> <h1>Bookstore</h1> <ul> { for $a in doc(\"bookmart.xml\")/bookstore/book order by $a/title return <li class=\"{data($a/@category)}\">{data($a/title)}</li> } </ul> </body> </html>
Now, the HTML elements will looks like: <html> <body> <h1>Bookstore</h1> <ul> <li class=\"CHILDREN\">Jungle Book</li> <li class=\"ADVENTURE\">The Book of the Thousand Nights and a Night</li> <li class=\"WEB PROGRAMING\">XPath and XPointer</li> <li class=\"WEB PROGRAMING\">XML Programing</li> </ul> </body> </html>
|
How to define functions in XQuery?
|
XML has many build-in functions. XQuery functions generally made perform with string values, numeric values, date and time comparisons, boolean values etc. XQuery has also give us facility to made our own functions. Now, I told you how to call XQuery functions. Ex 1: When you use function in an element. <name>{uppercase($booktitle)}</name>
Ex 2: When you use function in the predicate of a path expression. doc(\"bookmart.xml\")/bookstore/book[substring(title,1,5)=\'Jungle\']
Ex 3: When you use function in a let clause let $name := (substring($booktitle,1,4))
Now, I told you how to made functions with XQuery. Write some points in mind when you created any functions: Point1 : Starting with declare function keyword. Point2 : We should prefix the function name. Point3 : Data type of parameter should be same as data type that you defined in XML data. Point4 : To body of the function between the curly braces. Syntax: declare function prefix:function_name($parameter AS datatype) AS returnDatatype { (: ...Here write function code... :) };
Example: declare function local:minPrice( $price as xs:decimal?, $discount as xs:decimal?) AS xs:decimal? { let $disc := ($price * $discount) div 100 return ($price - $disc) }; (: Now, below I told you how you call the above functoin :) <minPrice>{local:minPrice($book/price, $book/discount)}</minPrice>
|
What do you mean by selecting and Filtering elements in XQuery?
|
We can selecting and filtering the xml elements with FLWOR and Path expression. I have given you a FLWOR expression: for $a in doc(\"bookmart.xml\")/bookstore/book where $a/price<150 order by $a/title return $a/title Where, 1. for : It is an optional and use to bind a variable to each item. for $a in (1 to 5) return <test>{$a}</test>
Output: <test>1</test> <test>2</test> <test>3</test> <test>4</test> <test>5</test> Using at keyword we can also count the iterations. for $a at $b in doc(\"bookmart.xml\")/bookstore/book/title return <book>{$b}. {data($a)}</book>
Output: <book>1. Jungle Book</book> <book>2. The Book of the Thousand Nights and a Night</book> <book>3. XPath and XPointer</book> <book>4. XML Programing</book> We can also use more than one expressions with for and seperate expressions using comma(,). for $a in (5,10), $b in (50,100) return <test>a={$a} and b={$b}</test>
Output: <test>a=5 and b=50</test> <test>a=5 and b=100</test> <test>a=10 and b=50</test> <test>a=10 and b=100</test>
2. let : It is an optional. let $a := (1 to 6) return <test>{$a}</test>
Output: <test>1 2 3 4 5 6</test>
3. where : It is an optional.Which are use to define a criteria. where $a/price>50 and $a/price<150
4. order by : It is an optional.Which is used to set our result in sort-order. for $a in doc(\"bookmart.xml\")/bookstore/book order by $a/@category, $a/title return $a/title
Output: <title lang=\"en\">The Book of the Thousand Nights and a Night</title> <title lang=\"en\">Jungle Book</title> <title lang=\"en\">XPath and XPointer</title> <title lang=\"en\">XML Programing</title>
5. return : It is use to define whats the result or return the result. for $a in doc(\"bookmart.xml\")/bookstore/book return $a/title
Output: <title lang=\"en\">Jungle Book</title> <title lang=\"en\">The Book of the Thousand Nights and a Night</title> <title lang=\"en\">XPath and XPointer</title> <title lang=\"en\">XML Programing</title>
|
How you define terms in XQuery?
|
Ther are seven kinds of nodes in XQuery.These are, 1.element 2.attribute 3.text 4.namespace 5.comment 6.document or root 7.processing-instruction nodes
Now, consider an XML example that I have given below: <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <bookstore> <book> <title lang=\"en\">Jungle Book</title> <author>Rudyard Kipling</author> <year>1894</year> <price>99.89</price> </book> </bookstore> In the above example nodes are: <bookstore> (document node) <author>Rudyard Kipling</author> (element node) lang=\"en\" (attribute node)
|
How you define atomic values?
|
Consider the example that have written given below: <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <bookstore> <book> <title lang=\"en\">Jungle Book</title> <author>Rudyard Kipling</author> <year>1894</year> <price>99.89</price> </book> </bookstore> Atomic values are those nodes which have no children and parent. Example: Atomic values from above examples are: Rudyard Kipling \"en\" |