Xquery

XQuery


XQuery is a functional programming query language that is used to transform or querying XML or JSON, binary. etc., data.


  • The language is developed by the XML Query a working group of the W3C.
  • XQuery 1.0 [2003] became a W3C Recommendation on January 23, 2007.[4]
  • All XQuery functions and types are written in lower-case.
  • Case-sensitive language
  • The string value can be in single or double code.
  • Variable name defined with $ and cannot start with number
  • Comment (::)

A Complete query always follows the FLOWR expressions, without FLOWR expressions query will never be complete.

F: for – Behave like Loop as for loop in java

L: let – Uses for creating a variable

O: order by – sort the result   

W: where – filter the nodes/result

R: return – return evaluated query result

  ๐Ÿ‘‰for/let and return are must keywords for creating any query.


Example:

             let $a := "abc" return $a
             or
             for $each in (1 to 10) return $each 

Variables:

There are three type variables in XQuery:

1. Local variable (Within a function block)
2. Global variable (Outside of all the functions)
3. External variable (It defines in the library query file and can call in any query by importing the library module.)


Predicate:

Predicate use for limiting the extracted data as to where keyword in FLWOR.

๐Ÿ‘‰Order by keyword works on a single value for sorting the result. 

Example:

1. Where and order by

let $a := ("a", "b", "aaa", "d", "a", "b", "f")
  for $each in $a
  where ($each = "f")
  order by $each ascending
  return $each

2. Predicate:

let $a := ("a", "b", "aaa", "d", "a", "b", "f")
  for $each in $a[. eq 'g']
    order by $each ascending
      return $each


for loop practice questions


Q1. Write a query to print the below-mentioned pyramid.
  
 * * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *

Ans:
(
for $each at $pos in (1 to 4)
    return fn:string-join(
    for $a in (1 to $pos)
    return "*"
     )
     
,

for $each in fn:reverse(1 to 4)
    return
    fn:string-join(
    for $a in (1 to $each)
    return "*"
     ) 
)


Q.2. Write a query to print the following characters via for loop

    *    
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*

Answers:
(
 let $num := 10
  for $each at $i in (1 to $num)
  let $before-after-space-count := xs:int(($num - $i) div 2)
  let $star := fn:string-join(for $each in (1 to $i) return "*")
  let $space   := fn:string-join(for $each in (1 to $before-after-space-count) return(' '))
 return fn:concat($space, $star, $space)
)
,
(
  let $num := 10 
   for $each at $i in fn:reverse(1 to $num)
   let $before-after-space-count := xs:int(($num - $each) div 2)
   let $star := fn:string-join(for $each in (1 to $each) return "*")
   let $space   := fn:string-join(for $each in (1 to $before-after-space-count) return(' '))
   return fn:concat($space, $star, $space)
)

 

Basic difference between string(), text(), data() function in xquery.


text():
       It returns a string for each text value of the context element only.

example:  let $x := <h1>The Taj Mahal is in <em>Agra</em> !</h1>
                  return fn:text($x) 

       It will return as  The Taj Mahal is in !. It will not return 'Agra' because it is not a context not it is a child node of the context node.


string():
    ๐Ÿ‘‰It returns a string for each descendent text node and context nodes text also. All those descendant text nodes will be concatenated into one string.

example:  let $x := <h1>The Taj Mahal is in <em>Agra</em> !</h1>
                  return fn:string($x) 

It will return as  The Taj Mahal is in Agra!

data(): it works the same as the string function but returns an untypedAtomic value.

IF and else: To check the condition whether is true or not

Examples:
for $a in (1 to 10)
return
if($a eq 5)
then "akbar"
else if($a eq 6)
then "girish"
else "abcd"

Data types conversion in XQuery:






Note: If any string value has only numbers then you can convert it into "int". and if contains the "char+num" then you can't.
 


MAP in XQuery - map:map():

The map built-in functions are used to create maps. Maps store name-value pairs in an in-memory data structure. You can also persist a map to a disk by storing it in a document. Some programming languages implement maps using hash tables, but these map functions make it convenient for you to create and update your own maps.


Below are mentioned some map functions and their definitions:

map:clear      Clear a map.
map:contains      Returns true if the key exists in the map.
map:count      Returns the number of keys used in the map.
map:delete      Delete a value from a map.
map:entry      Constructs a new map with a single entry consisting of the key and value.
map:get             Get a value from a map.
map:keys             Get the keys used in the map.
map:map             Creates a map.
map:new            Constructs a new map by combining the keys from the maps given as an argument.
map:put            Put a value into a map at the given key.
map:with           Updates a map, inserting a value into it at the given key.


A example of map Query:

let $map := map:map()
let $doc := xdmp:document-get("D:\22-02-2021\ErrorLog.txt")
let $a :=    for $each at $line in fn:tokenize($doc, "\n")[1 to 200]
                   let $biblio-start := if(fn:contains($each, "<biblioCore>"))
                          then
                          (
                            if(fn:contains($each, "Info: MetadataManager-REST:"))
                            then
                            (
                             let $lines := fn:normalize-space(fn:substring-after($each, "Info: MetadataManager-REST:"))
                             
                             let $result := if(fn:starts-with($lines, "<biblioCore>") and fn:ends-with($lines, "</biblioCore>"))
                                           then $lines
                                           else if(fn:starts-with($lines, "<biblioCore>"))
                                           then
                                           (
                                            if (fn:ends-with($lines, "</biblioCore>")) then ()
                                            else
                                            (
                                            if(map:contains($map, xs:string($line)))
                                            then ()
                                            else map:put($map, xs:string($line), $lines)
                                            )
                                           )
                                           else ()
                                           
                             return $result               
                            )
                            else ()
                           )
                           else()
     let $biblio-end := if(fn:contains($each, "</biblioCore>"))
                          then
                          (
                            if(fn:contains($each, "Info: MetadataManager-REST:"))
                            then
                            (
                            let $lines := fn:normalize-space(fn:substring-after($each, "Info: MetadataManager-REST:"))
                            return if(fn:ends-with($lines, "</biblioCore>"))
                                           then
                                           (
                                            if(map:contains($map, xs:string($line)))
                                            then ()
                                            else map:put($map, xs:string($line), $lines)
                                           )
                                          else () 
                            )
                            else ()
                          )
                          else ()
               return $biblio-start            
let $keys := map:keys($map)
let $staring-key := for $each in $keys
                     let $values := map:get($map, $each)
                     return if(fn:contains($values, "</biblioCore>")) then () else $each
let $end-key := for $each in $keys
                     let $values := map:get($map, $each)
                     return if(fn:contains($values, "<biblioCore>")) then () else $each                    
let $new  := fn:string-join(for $each in fn:tokenize($doc, "\n")[xs:int($staring-key) to xs:int($end-key)] 
 return fn:normalize-space(fn:substring-after($each, "MetadataManager-REST:")))
 return ($new, $a)






Comments

Popular posts from this blog

HTTP Servers, Database, Forest

Marklogic Fundamentals