|
Totel:13 Click:
1 2 3
XQuery Interview Questions And Answers
Page 1
Questions 1 How you define atomic values?
Answer 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"
Questions 2 How you define terms in XQuery?
Answer 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)
Questions 3 What do you mean by selecting and Filtering elements in XQuery?
Answer 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>
Questions 4 How to define functions in XQuery?
Answer 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>
Questions 5 How to add elements and attributes with XQuery in XML data?
Answer 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>
Goto Page:
1 2 3
XQuery Objective Questions And Answers
XQuery Objective Questions And Answers
XQuery Interview Questions And Answers
XQuery Subjective Questions And Answers
R4R,XQuery Objective, XQuery Subjective, XQuery Interview Questions And Answers,XQuery,XQuery Interview,XQuery Questions ,XQuery Answers
|
|
|