SlideShare a Scribd company logo
1 of 39
Download to read offline
XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction
                                              XQuery Tutorial
XQuery Type
System

Basics

Tricks                                        Vyacheslav Zholudev
Pitfalls

XQuery in                                      Jacobs University Bremen
TNTBase
                                KWARC – Knowledge Adaptation and Reasoning for Content
Conclusion


                                              September 17, 2010




               Vyacheslav Zholudev                     XQuery Tutorial      September 17, 2010   1
What is XQuery?

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type                XQuery is an XML-database query language
System

Basics                     Like SQL for Relational databases
Tricks                     Not only for XML files
Pitfalls
                           W3C Recommendation
XQuery in
TNTBase
                           Side-effect free
Conclusion




               Vyacheslav Zholudev                 XQuery Tutorial   September 17, 2010   2
For those who know XSLT

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction       XQuery and XSLT both:
XQuery Type
System
                           Take XML as their input and produce XML as their
Basics                     output
Tricks                     Have the same data model and type system
Pitfalls
                           Are declarative and meant to be free from side effects
XQuery in
TNTBase
                           Share XPath, having the same library of built-in
Conclusion
                           functions




               Vyacheslav Zholudev               XQuery Tutorial   September 17, 2010   3
Basic Rules

    XQuery
    Tutorial
 Vyacheslav
  Zholudev
                           XQuery is case-sensitive
Introduction

XQuery Type
                           XQuery elements, attributes, and variables must be
System                     valid XML names
Basics
                           An XQuery string value can be in single or double
Tricks
                           quotes (’foo’ or “foo”)
Pitfalls

XQuery in                  An XQuery variable is defined with a $ followed by a
TNTBase
                           name, e.g. $theory
Conclusion
                           XQuery comments are delimited by (: and :), e.g.
                           ( : This XQuery generates an omdoc document. :)




               Vyacheslav Zholudev                     XQuery Tutorial       September 17, 2010   4
XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type
System

Basics

Tricks

Pitfalls
                                     Type System
XQuery in
TNTBase

Conclusion




               Vyacheslav Zholudev       XQuery Tutorial   September 17, 2010   5
XQuery types

    XQuery
    Tutorial
                   Taken from XML Schema
 Vyacheslav
  Zholudev

Introduction

XQuery Type
System

Basics

Tricks

Pitfalls

XQuery in
TNTBase

Conclusion




               Vyacheslav Zholudev         XQuery Tutorial   September 17, 2010   6
Sequences in XQuery

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction               Ordered list of objects
XQuery Type
System
                           List of sequences is one sequence
Basics                     One element is a one-element sequence
Tricks

Pitfalls
                   Examples:
XQuery in                  (“a”, “b”, “c”)
TNTBase

Conclusion
                           ((“a”, “b”), “c”)
                           ((“a”, “b”), (“c”, ()))




               Vyacheslav Zholudev                   XQuery Tutorial   September 17, 2010   7
XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type
System

Basics

Tricks

Pitfalls
                            XQuery in “5 minutes”
XQuery in
TNTBase

Conclusion




               Vyacheslav Zholudev   XQuery Tutorial   September 17, 2010   8
Basics

    XQuery
    Tutorial       Getting a document using doc() function:
 Vyacheslav        doc(’ http: // tntbase.org/sets.omdoc’)
  Zholudev

                   <omdoc xmlns="http://omdoc.org/ns">
Introduction
                     <theory xml:id="sets−operations" about="#sets−operations">
XQuery Type            <imports from=" ../../ missing/en/highschool#highschool" xml:id="sets−operations.
System
                       <imports from="./sets−introduction#sets−introduction" xml:id="sets−operations.p2
Basics                 <imports from="./mathtalk−definitions#mathtalk−definitions" xml:id="sets−operati
Tricks                 <symbol name="eset" xml:id="eset.sym" about="#eset.sym"/>
                       <notation cd="sets−operations" name="eset">
Pitfalls
                          <prototype>
XQuery in                    <om:OMS cd="sets−operations" name="eset"/>
TNTBase
                          </prototype>
Conclusion                <rendering>
                             <m:mi mathvariant="normal">&#x2205;</m:mi>
                          </rendering>
                       </notation>
                        ....
                   </omdoc>


               Vyacheslav Zholudev                      XQuery Tutorial      September 17, 2010   9
XPath Expressions

    XQuery
    Tutorial
                   Selecting nodes using XPath:
 Vyacheslav        doc(’ http: // tntbase.org/sets.omdoc’)/omdoc/theory/notation
  Zholudev
                   OR
Introduction       doc(’ http: // tntbase.org/sets.omdoc’)/omdoc//notation
XQuery Type         <notation xmlns="http: // omdoc.org/ns" cd="sets−operations" name="eset">
System
                      <prototype>
Basics                   <om:OMS cd="sets−operations" name="eset"/>
Tricks                </prototype>
Pitfalls
                      <rendering>
                         <m:mi mathvariant="normal">X</m:mi>
XQuery in
TNTBase
                      </rendering>
                    </notation>
Conclusion          <notation cd="sets−operations" name="powerset">
                      <prototype>
                         <om:OMS cd="sets−operations" name="powerset"/>
                      </prototype>
                       ...
                   </notation>
                   ... <!−− More results −−>

               Vyacheslav Zholudev                      XQuery Tutorial        September 17, 2010   10
Predicates

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type                doc(’ http: // tntbase.org/sets.omdoc’)//notation [3]
System

Basics                     doc(’ http: // tntbase.org/sets.omdoc’)//notation[ position () <4]
Tricks                     doc(’ http: // tntbase.org/sets.omdoc’)//notation[@cd=’sets−operations’]
Pitfalls
                           doc(’ http: // tntbase.org/sets.omdoc’)//notation[prototype]
XQuery in
TNTBase                    doc (...)// notation [prototype/om:OMS/@name="eset"]
Conclusion




               Vyacheslav Zholudev                         XQuery Tutorial           September 17, 2010   11
FLOWR Expressions

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction
                           FOR
XQuery Type
System
                           LET
Basics

Tricks
                           WHERE
Pitfalls                   ORDER BY
XQuery in
TNTBase
                           RETURN
Conclusion
                   Note: there is no GROUP BY in XQuery




               Vyacheslav Zholudev         XQuery Tutorial   September 17, 2010   12
LET

    XQuery
    Tutorial
 Vyacheslav
                   Variables are prefixed with $
  Zholudev
                           Query:
Introduction
                           let $a := 3
XQuery Type
System
                           let $b := 4
                           return
Basics
                             $a + $b
Tricks

Pitfalls                   Result: 7
XQuery in
TNTBase                    Query:
Conclusion            1    let $x := (<foo><bar/><bla/></foo>, <foo><baz/></foo>)
                           return
                             $x/∗ ( : all children elements :)

                           Result:   (<bar/>, <bla/>, <baz/>)



               Vyacheslav Zholudev                     XQuery Tutorial        September 17, 2010   13
FOR loop

    XQuery
    Tutorial       Syntax:
 Vyacheslav
  Zholudev            for $var in $sequence
                        where <condition>
Introduction            return <result>
XQuery Type
System
                   Examples:
Basics

Tricks              for $notation in doc(’ http: // tntbase.org/sets.omdoc’)//notation
               2      where $notation//∗[@name="eset"]
Pitfalls
                      return $notation/rendering
XQuery in
TNTBase

Conclusion          let $numbers := (2, 23, 1, 32, 8, 13, −1)
               2    return
                      for $num in $numbers
                       where $num > 5
                       order by $num
                       return −($num + 100)



               Vyacheslav Zholudev                       XQuery Tutorial          September 17, 2010   14
Wrapping results

    XQuery
    Tutorial        let $ntns := tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’)//notation
                    return
 Vyacheslav
  Zholudev            <notations>
               4        <amount>{count($ntns)}</amount>
Introduction            <renderings>
                           {
XQuery Type
System                       for $ntn in $ntns return
                              <my−rend>{$ntn/rendering}</my−rend>
Basics
               9           }
Tricks                  </renderings>
Pitfalls              </notations>
XQuery in
TNTBase            Result:
Conclusion         <notations>
                     <amount>38</amount>
                     <renderings>
               4       <my−rend><rendering"><m:mi mathvariant="normal">X</m:mi></rendering>
                       </my−rend>
                        .... <!−− more results −−>
                     </renderings>
                   </notations>
               Vyacheslav Zholudev                       XQuery Tutorial         September 17, 2010   15
IF Expression

    XQuery
    Tutorial
 Vyacheslav
  Zholudev
                   Note: "else" clause is mandatory
Introduction       Syntax:
XQuery Type
System              if <condition> then $seq1 else $seq2
Basics

Tricks             Example:
Pitfalls             for $i in (1, " str " , 3, <foo/>) return
XQuery in               if ($ i instance of element()) then "element"
TNTBase                else if ($ i instance of xs:string ) then " string "
Conclusion     4             else "other"

                   Result:      ( "other" , " string " , "other" , "element")




               Vyacheslav Zholudev                          XQuery Tutorial     September 17, 2010   16
Comparisons

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction
                           General comparisons: =, !=, <, <=, >, >=
XQuery Type
System                     Value comparisons: eq, ne, lt, le, gt, ge
Basics

Tricks
                   Value comparisons operate only on single elements
Pitfalls           (otherwise, error)
XQuery in
TNTBase                    (1, 2, 4) = (10, 2, 8)   returns true
Conclusion                 (1, 2, 4) eq (10, 2, 8)   throws an error




               Vyacheslav Zholudev                       XQuery Tutorial   September 17, 2010   17
Axes

    XQuery
    Tutorial
                   Forward Axes:
 Vyacheslav            child (/*)
  Zholudev
                       descendant (//*)
Introduction
                       attribute (/@*)
XQuery Type
System                 self (/.)
Basics                 descendant-or-self
Tricks
                       following-sibling
Pitfalls

XQuery in
                       following
TNTBase
                   Reverse Axes
Conclusion
                       parent (/..)
                       ancestor
                       preceding-sibling
                       preceding
                       ancestor-or-self
               Vyacheslav Zholudev          XQuery Tutorial   September 17, 2010   18
Functions

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction
                   Built-in Functions + User-defined functions
XQuery Type
System             Built-in functions examples:
Basics                     fn:count($seq) as xs:double
Tricks
                           fn:empty($seq) as xs:boolean
Pitfalls

XQuery in                  fn:uppercase($str as xs:string ) as xs:string
TNTBase
                           fn:substring ($ str , $pos) as xs:string
Conclusion




               Vyacheslav Zholudev                         XQuery Tutorial   September 17, 2010   19
User-defined functions

    XQuery
    Tutorial       declare function prefix:function_name($parameter AS datatype)
 Vyacheslav
                   AS returnDatatype
  Zholudev         {
                     ... function code here...
Introduction   5   }
XQuery Type
System             Example:
Basics
                   declare namespace omdoc = "http://omdoc.org";
Tricks
                   ( : This function returns amounts of notations in OMDoc documents :)
Pitfalls           declare function omdoc:notations−amount($docs as document−node()∗)
XQuery in          as xs:double∗
TNTBase        5   {
Conclusion
                     for $d in $docs return count($d//notation)
                   }

                   Invocation:
                   omdoc:notations−amount((doc("/doc1.omdoc"), doc("doc2.omdoc")))

                   Note: Types are optional, but highly recommended
               Vyacheslav Zholudev                   XQuery Tutorial       September 17, 2010   20
XQuery modules

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction
                           An XQuery module is a set of user-defined functions
XQuery Type
System
                           Every function should be in the module namespace
Basics

Tricks
                           Module declaration:   module namespace tnt="http://tntbase.org/ns";

Pitfalls                   Modules can’t contain expressions
XQuery in
TNTBase
                           Modules can import other modules, the latter will not be
Conclusion                 visible for those who import the former module




               Vyacheslav Zholudev                 XQuery Tutorial        September 17, 2010   21
Importing a module

    XQuery
    Tutorial
 Vyacheslav                Syntax:
  Zholudev
                           import module namespace <prefix>= "<ns_uri>"(at "<hint_for_a_processor";)
Introduction               Example:
XQuery Type
System                     import module namespace omdoc="http://omdoc.org/ns" at
                                    tntbase: /modules/omdoc−module.xq;
Basics
                            let $doc−names := (..., ..., ...)
Tricks                     for $doc in $doc−names return
Pitfalls              5        omdoc:get−notations($doc)
XQuery in
TNTBase
                           The influence of the “at” keyword is
Conclusion
                           implementation-dependent
                           Note: Having multiple modules with the same
                           namespace is a bad practice and have
                           non-deterministic behaviour

               Vyacheslav Zholudev                     XQuery Tutorial       September 17, 2010   22
XQuery Prolog

    XQuery
    Tutorial
                   is a series of declarations and imports that define the
 Vyacheslav
  Zholudev         processing environment (separated by a semicolon)
                   Useful examples:
Introduction

XQuery Type                Module imports (described before)
System

Basics
                           Namespace declarations:
Tricks                     declare namespace tnt="http://tntbase.org/ns";
Pitfalls                   declare default element namespace "http://omdoc.org/ns"
XQuery in
TNTBase
                           Variable declarations:
Conclusion
                           declare variable $NUM := 10;

                           Function declarations (described before)
                           declare function local:test () as xs:string external ;

                           Module declaration (described before)

               Vyacheslav Zholudev                        XQuery Tutorial           September 17, 2010   23
XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type
System

Basics

Tricks

Pitfalls
                                     XQuery Tricks
XQuery in
TNTBase

Conclusion




               Vyacheslav Zholudev        XQuery Tutorial   September 17, 2010   24
Eliminating FOR

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

                   declare function omdoc:notations−amount($docs as document−node()∗)
Introduction
                   as xs:double
XQuery Type        {
System
                     for $d in $docs return count($d//notation)
Basics         5   }
Tricks

Pitfalls           declare function omdoc:notations−amount($docs as document−node()∗)
XQuery in          as xs:double
TNTBase            {
Conclusion           $docs/count(.// notation)
               5   }




               Vyacheslav Zholudev                 XQuery Tutorial      September 17, 2010   25
Default namespace

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction
                   We can define default namespace:
XQuery Type
System             declare default element namespace "http://omdoc.org/ns"
Basics             How to address nodes in no namespace?
Tricks
                   declare default element namespace "http://omdoc.org/ns";
Pitfalls           declare namespace no−ns = "";
XQuery in
TNTBase             tnt:doc ( ’ /sets−operations.omdoc’)/omdoc/theory[1]/no−ns:extra−element
Conclusion




               Vyacheslav Zholudev                    XQuery Tutorial         September 17, 2010   26
Preserving XML-formatting

    XQuery
    Tutorial
                   Query:
 Vyacheslav    1   <foo>
  Zholudev          <bar/>
                   </foo>
Introduction

XQuery Type        Result by default:
System

Basics             <foo><bar/></foo>
Tricks
                   New query:
Pitfalls
                   declare boundary−space preserve;
XQuery in
TNTBase            <foo>
                    <bar/>
Conclusion
               4   </foo>

                   Result:
               1   <foo>
                    <bar/>
                   </foo>

               Vyacheslav Zholudev                    XQuery Tutorial   September 17, 2010   27
XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type
System

Basics

Tricks

Pitfalls
                                     Typical Pitfalls
XQuery in
TNTBase

Conclusion




               Vyacheslav Zholudev         XQuery Tutorial   September 17, 2010   28
Pitfalls

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

                           $seq1 != $seq2,     but meant:      not($seq1 == $seq2)
Introduction

XQuery Type                foo[bar is null ]   instead of foo[empty(bar)]
System

Basics
                           // foo[bar < 5 and bar > 1]   vs.   // foo[bar [. < 5 and . > 1]]

Tricks                     In IF clause you always need ELSE
Pitfalls
                           There are no reserved words “true” and “false”. Use
XQuery in
TNTBase                    built-in functions fn:true() and fn:false().
Conclusion                 One can’t return <foo/> <bar/>.
                           Should return a sequence: (<foo/>,                 <bar/>)




               Vyacheslav Zholudev                         XQuery Tutorial              September 17, 2010   29
Document collections

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type
                           Document from HEAD:
System
                           tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’)
Basics

Tricks
                           Collection from HEAD:
Pitfalls
                            tnt:collection ( ’ / slides // en/∗sets∗.omdoc’)

XQuery in                  Document from previous revisions:
TNTBase
                           tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’, 1397)
Conclusion




               Vyacheslav Zholudev                           XQuery Tutorial        September 17, 2010   30
Repository information

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction               The last revision number:
XQuery Type                 tnt:last −revnum() as xs:integer
System

Basics
                           Revisions in which the file has been changed:
Tricks
                            tnt:file −revs($path as xs:string) as xs:integer∗
Pitfalls                   Get a document in the uncommitted revision:
XQuery in                  tnt:txn −doc($path as xs:string, $svntxn as xs:string ) as document−node()?
TNTBase

Conclusion                 Get a file path to which an element belongs:
                           tnt:get −doc−paths($docs as node()∗) as xs:string∗




               Vyacheslav Zholudev                       XQuery Tutorial        September 17, 2010   31
VDoc functions

    XQuery
    Tutorial
 Vyacheslav
  Zholudev
                           Test a VDoc Spec:
                           tnt:vd −skel($path as xs:string)
Introduction

XQuery Type
                           Get an underlying VDoc Spec path of the VDoc:
System
                           tnt:skel −path($path as xs:string) as xs:string ?
Basics

Tricks
                           Get a VDoc:
Pitfalls
                           tnt:vdoc($path as xs:string ) as item()
XQuery in                  Insert a parameter for the VDoc:
TNTBase
                           tnt:ins −vd−param($path as xs:string, $par as xs:string, $vals as xs:string +)
Conclusion
                           Delete a parameter of the VDoc:
                           tnt:delete −vd−param($path as xs:string, $name as xs:string)

                           ... some others


               Vyacheslav Zholudev                       XQuery Tutorial         September 17, 2010   32
Dynamic evaluation

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type
System                     Query:
Basics
                           tnt:execute($xquery as xs:string )
Tricks

Pitfalls
                           Example:
XQuery in
                           count(tnt:execute( " tnt:collection (//∗. omdoc)"))
TNTBase

Conclusion




               Vyacheslav Zholudev                        XQuery Tutorial        September 17, 2010   33
Some other functions

    XQuery
    Tutorial
 Vyacheslav                Commit documents (dangerous):
  Zholudev
                           tnt:commit($paths as xs:string ∗, $docs as element()∗,
Introduction                    $msg as xs:string, $revnum as xs:integer) as xs:string
XQuery Type
System

Basics
                           XML-diff:
Tricks                     tnt:xml −diff($ differ −name as xs:string, $l as element(), $r as element())
Pitfalls                   Get an auxiliary document produced by a plugin:
XQuery in
TNTBase
                           tnt:typed −doc($path as xs:string, $type as xs:string +)

Conclusion                 Substring before last occurrence of some string:
                           tnt:substring −before−last($arg as xs:string?, $delim as xs:string )

                           Delete a parameter of the VDoc:
                           tnt:delete −vd−param($path as xs:string, $name as xs:string)



               Vyacheslav Zholudev                       XQuery Tutorial          September 17, 2010   34
XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type
System

Basics

Tricks

Pitfalls
                                     Wrapping Up
XQuery in
TNTBase

Conclusion




               Vyacheslav Zholudev       XQuery Tutorial   September 17, 2010   35
Non-covered topics

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction               Type casting in XQuery
XQuery Type
System
                           Stronger typing using XML Schema
Basics                     Node construction
Tricks
                           XQuery Update facility
Pitfalls

XQuery in
                           EXQuery
TNTBase
                           What’s new in XQuery 1.1?
Conclusion
                           XQuery Scripting and side-effects




               Vyacheslav Zholudev              XQuery Tutorial   September 17, 2010   36
Advantages of XQuery

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction               In context of XML DBs when benefiting from indexes
XQuery Type
System                     Processing big documents that do not fit into memory
Basics                     (XSLT streaming doesn’t count here)
Tricks
                           Querying is better, then XSLT for formatting and
Pitfalls
                           presentation
XQuery in
TNTBase                    XQuery is easier to explain
Conclusion
                           Less verbose




               Vyacheslav Zholudev               XQuery Tutorial   September 17, 2010   37
Advantages of XSLT

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction               Recurring over trees is much easier
XQuery Type
System
                           Better in formatting numbers
Basics                     More mature technology
Tricks
                           Well-standardized library of extensions (EXSLT)
Pitfalls

XQuery in
                           XML-language
TNTBase
                           XQuery is a subset of XSLT in terms of functionality
Conclusion
                           Has a form of polymorphism to overwrite templates




               Vyacheslav Zholudev               XQuery Tutorial   September 17, 2010   38
Conclusion

    XQuery
    Tutorial
 Vyacheslav
  Zholudev

Introduction

XQuery Type
System

Basics
                        You should pick out the tool
Tricks

Pitfalls                  that suits your task best!
XQuery in
TNTBase

Conclusion




               Vyacheslav Zholudev   XQuery Tutorial   September 17, 2010   39

More Related Content

What's hot

Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitmfrost503
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)Alok Kumar
 
PhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsPhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsBastian Feder
 
reductio [ad absurdum]
reductio [ad absurdum]reductio [ad absurdum]
reductio [ad absurdum]Shakacon
 

What's hot (6)

Mocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
PhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsPhpUnit - The most unknown Parts
PhpUnit - The most unknown Parts
 
Acm
AcmAcm
Acm
 
reductio [ad absurdum]
reductio [ad absurdum]reductio [ad absurdum]
reductio [ad absurdum]
 

Viewers also liked

Where to use the Oracle Service Bus @ OBUG Connect 22-04-2012
Where to use the Oracle Service Bus @ OBUG Connect 22-04-2012Where to use the Oracle Service Bus @ OBUG Connect 22-04-2012
Where to use the Oracle Service Bus @ OBUG Connect 22-04-2012Jan van Zoggel
 
Processamento consultas-xml-v2
Processamento consultas-xml-v2Processamento consultas-xml-v2
Processamento consultas-xml-v2Luiz Matos
 
XML In The Real World - Use Cases For Oracle XMLDB
XML In The Real World - Use Cases For Oracle XMLDBXML In The Real World - Use Cases For Oracle XMLDB
XML In The Real World - Use Cases For Oracle XMLDBMarco Gralike
 
SharePoint 2013 Javascript Object Model
SharePoint 2013 Javascript Object ModelSharePoint 2013 Javascript Object Model
SharePoint 2013 Javascript Object ModelInnoTech
 
Share point hosted add ins munich
Share point hosted add ins munichShare point hosted add ins munich
Share point hosted add ins munichSonja Madsen
 
Essential Knowledge for SharePoint Add-Ins
Essential Knowledge for SharePoint Add-InsEssential Knowledge for SharePoint Add-Ins
Essential Knowledge for SharePoint Add-InsInnoTech
 
Northeastern DB Class Introduction to Marklogic NoSQL april 2016
Northeastern DB Class Introduction to Marklogic NoSQL april 2016Northeastern DB Class Introduction to Marklogic NoSQL april 2016
Northeastern DB Class Introduction to Marklogic NoSQL april 2016Matt Turner
 
Oracle Service Bus (OSB) for the Busy IT Professonial
Oracle Service Bus (OSB) for the Busy IT Professonial Oracle Service Bus (OSB) for the Busy IT Professonial
Oracle Service Bus (OSB) for the Busy IT Professonial Frank Munz
 
Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 apps
Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 appsChris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 apps
Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 appsChris O'Brien
 
Build and Deploy Provider-hosted SharePoint Add-ins
Build and Deploy Provider-hosted SharePoint Add-insBuild and Deploy Provider-hosted SharePoint Add-ins
Build and Deploy Provider-hosted SharePoint Add-insDanny Jessee
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQueryKatrien Verbert
 
Rev Your Engines - SharePoint Performance Best Practices
Rev Your Engines - SharePoint Performance Best PracticesRev Your Engines - SharePoint Performance Best Practices
Rev Your Engines - SharePoint Performance Best PracticesEric Shupps
 
Real World SharePoint Add-In Development
Real World SharePoint Add-In DevelopmentReal World SharePoint Add-In Development
Real World SharePoint Add-In DevelopmentEric Shupps
 
Develop a SharePoint App in 45 Minutes
Develop a SharePoint App in 45 MinutesDevelop a SharePoint App in 45 Minutes
Develop a SharePoint App in 45 MinutesTom Resing
 
Top 10 sharepoint interview questions with answers
Top 10 sharepoint interview questions with answersTop 10 sharepoint interview questions with answers
Top 10 sharepoint interview questions with answerswillhoward459
 

Viewers also liked (20)

XQuery overview
XQuery overviewXQuery overview
XQuery overview
 
Where to use the Oracle Service Bus @ OBUG Connect 22-04-2012
Where to use the Oracle Service Bus @ OBUG Connect 22-04-2012Where to use the Oracle Service Bus @ OBUG Connect 22-04-2012
Where to use the Oracle Service Bus @ OBUG Connect 22-04-2012
 
XSL, XSL-FO e XSLT + XPath
XSL, XSL-FO e XSLT + XPathXSL, XSL-FO e XSLT + XPath
XSL, XSL-FO e XSLT + XPath
 
Processamento consultas-xml-v2
Processamento consultas-xml-v2Processamento consultas-xml-v2
Processamento consultas-xml-v2
 
XML In The Real World - Use Cases For Oracle XMLDB
XML In The Real World - Use Cases For Oracle XMLDBXML In The Real World - Use Cases For Oracle XMLDB
XML In The Real World - Use Cases For Oracle XMLDB
 
oracle-osb
oracle-osboracle-osb
oracle-osb
 
Xml100 1
Xml100 1Xml100 1
Xml100 1
 
SharePoint 2013 Javascript Object Model
SharePoint 2013 Javascript Object ModelSharePoint 2013 Javascript Object Model
SharePoint 2013 Javascript Object Model
 
Share point hosted add ins munich
Share point hosted add ins munichShare point hosted add ins munich
Share point hosted add ins munich
 
Essential Knowledge for SharePoint Add-Ins
Essential Knowledge for SharePoint Add-InsEssential Knowledge for SharePoint Add-Ins
Essential Knowledge for SharePoint Add-Ins
 
Northeastern DB Class Introduction to Marklogic NoSQL april 2016
Northeastern DB Class Introduction to Marklogic NoSQL april 2016Northeastern DB Class Introduction to Marklogic NoSQL april 2016
Northeastern DB Class Introduction to Marklogic NoSQL april 2016
 
Oracle Service Bus (OSB) for the Busy IT Professonial
Oracle Service Bus (OSB) for the Busy IT Professonial Oracle Service Bus (OSB) for the Busy IT Professonial
Oracle Service Bus (OSB) for the Busy IT Professonial
 
Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 apps
Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 appsChris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 apps
Chris O'Brien - Comparing SharePoint add-ins (apps) with Office 365 apps
 
Build and Deploy Provider-hosted SharePoint Add-ins
Build and Deploy Provider-hosted SharePoint Add-insBuild and Deploy Provider-hosted SharePoint Add-ins
Build and Deploy Provider-hosted SharePoint Add-ins
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQuery
 
Rev Your Engines - SharePoint Performance Best Practices
Rev Your Engines - SharePoint Performance Best PracticesRev Your Engines - SharePoint Performance Best Practices
Rev Your Engines - SharePoint Performance Best Practices
 
Osb student guide
Osb student guideOsb student guide
Osb student guide
 
Real World SharePoint Add-In Development
Real World SharePoint Add-In DevelopmentReal World SharePoint Add-In Development
Real World SharePoint Add-In Development
 
Develop a SharePoint App in 45 Minutes
Develop a SharePoint App in 45 MinutesDevelop a SharePoint App in 45 Minutes
Develop a SharePoint App in 45 Minutes
 
Top 10 sharepoint interview questions with answers
Top 10 sharepoint interview questions with answersTop 10 sharepoint interview questions with answers
Top 10 sharepoint interview questions with answers
 

Similar to Xquery

Design patterns illustrated 010PHP
Design patterns illustrated 010PHPDesign patterns illustrated 010PHP
Design patterns illustrated 010PHPHerman Peeren
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with ClojureJohn Stevenson
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitsmueller_sandsmedia
 
SQL Injection: Amplifying Data Leakeage
SQL Injection: Amplifying Data LeakeageSQL Injection: Amplifying Data Leakeage
SQL Injection: Amplifying Data LeakeageUlisses Castro
 
XML - State of the Art
XML - State of the ArtXML - State of the Art
XML - State of the ArtJakub Malý
 
Cutting Edge Data Processing with PHP & XQuery
Cutting Edge Data Processing with PHP & XQueryCutting Edge Data Processing with PHP & XQuery
Cutting Edge Data Processing with PHP & XQueryWilliam Candillon
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 
Data visualization by Kenneth Odoh
Data visualization by Kenneth OdohData visualization by Kenneth Odoh
Data visualization by Kenneth Odohpyconfi
 
Paradigmas de programação funcional + objetos no liquidificador com scala
Paradigmas de programação funcional + objetos no liquidificador com scalaParadigmas de programação funcional + objetos no liquidificador com scala
Paradigmas de programação funcional + objetos no liquidificador com scalaBruno Oliveira
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code ExamplesNaresh Chintalcheru
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckFranklin Chen
 
Continuous Integration and Drupal
Continuous Integration and DrupalContinuous Integration and Drupal
Continuous Integration and DrupalSteven Merrill
 
Previewing OWL Changes and Refactorings Using a Flexible XML Database
Previewing OWL Changes and Refactorings Using a Flexible XML DatabasePreviewing OWL Changes and Refactorings Using a Flexible XML Database
Previewing OWL Changes and Refactorings Using a Flexible XML DatabaseChristoph Lange
 
Session 1.5 supporting virtual integration of linked data with just-in-time...
Session 1.5   supporting virtual integration of linked data with just-in-time...Session 1.5   supporting virtual integration of linked data with just-in-time...
Session 1.5 supporting virtual integration of linked data with just-in-time...semanticsconference
 
Tulsa techfest2010 security
Tulsa techfest2010   securityTulsa techfest2010   security
Tulsa techfest2010 securityJason Ragsdale
 

Similar to Xquery (20)

Design patterns illustrated 010PHP
Design patterns illustrated 010PHPDesign patterns illustrated 010PHP
Design patterns illustrated 010PHP
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Getting started with Clojure
Getting started with ClojureGetting started with Clojure
Getting started with Clojure
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
 
jQuery
jQueryjQuery
jQuery
 
SQL Injection: Amplifying Data Leakeage
SQL Injection: Amplifying Data LeakeageSQL Injection: Amplifying Data Leakeage
SQL Injection: Amplifying Data Leakeage
 
XML - State of the Art
XML - State of the ArtXML - State of the Art
XML - State of the Art
 
Cutting Edge Data Processing with PHP & XQuery
Cutting Edge Data Processing with PHP & XQueryCutting Edge Data Processing with PHP & XQuery
Cutting Edge Data Processing with PHP & XQuery
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Data visualization by Kenneth Odoh
Data visualization by Kenneth OdohData visualization by Kenneth Odoh
Data visualization by Kenneth Odoh
 
Paradigmas de programação funcional + objetos no liquidificador com scala
Paradigmas de programação funcional + objetos no liquidificador com scalaParadigmas de programação funcional + objetos no liquidificador com scala
Paradigmas de programação funcional + objetos no liquidificador com scala
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
Adventures in TclOO
Adventures in TclOOAdventures in TclOO
Adventures in TclOO
 
Beyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheckBeyond xUnit example-based testing: property-based testing with ScalaCheck
Beyond xUnit example-based testing: property-based testing with ScalaCheck
 
Continuous Integration and Drupal
Continuous Integration and DrupalContinuous Integration and Drupal
Continuous Integration and Drupal
 
Presentation
PresentationPresentation
Presentation
 
Previewing OWL Changes and Refactorings Using a Flexible XML Database
Previewing OWL Changes and Refactorings Using a Flexible XML DatabasePreviewing OWL Changes and Refactorings Using a Flexible XML Database
Previewing OWL Changes and Refactorings Using a Flexible XML Database
 
Session 1.5 supporting virtual integration of linked data with just-in-time...
Session 1.5   supporting virtual integration of linked data with just-in-time...Session 1.5   supporting virtual integration of linked data with just-in-time...
Session 1.5 supporting virtual integration of linked data with just-in-time...
 
Tulsa techfest2010 security
Tulsa techfest2010   securityTulsa techfest2010   security
Tulsa techfest2010 security
 

Xquery

  • 1. XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Tutorial XQuery Type System Basics Tricks Vyacheslav Zholudev Pitfalls XQuery in Jacobs University Bremen TNTBase KWARC – Knowledge Adaptation and Reasoning for Content Conclusion September 17, 2010 Vyacheslav Zholudev XQuery Tutorial September 17, 2010 1
  • 2. What is XQuery? XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type XQuery is an XML-database query language System Basics Like SQL for Relational databases Tricks Not only for XML files Pitfalls W3C Recommendation XQuery in TNTBase Side-effect free Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 2
  • 3. For those who know XSLT XQuery Tutorial Vyacheslav Zholudev Introduction XQuery and XSLT both: XQuery Type System Take XML as their input and produce XML as their Basics output Tricks Have the same data model and type system Pitfalls Are declarative and meant to be free from side effects XQuery in TNTBase Share XPath, having the same library of built-in Conclusion functions Vyacheslav Zholudev XQuery Tutorial September 17, 2010 3
  • 4. Basic Rules XQuery Tutorial Vyacheslav Zholudev XQuery is case-sensitive Introduction XQuery Type XQuery elements, attributes, and variables must be System valid XML names Basics An XQuery string value can be in single or double Tricks quotes (’foo’ or “foo”) Pitfalls XQuery in An XQuery variable is defined with a $ followed by a TNTBase name, e.g. $theory Conclusion XQuery comments are delimited by (: and :), e.g. ( : This XQuery generates an omdoc document. :) Vyacheslav Zholudev XQuery Tutorial September 17, 2010 4
  • 5. XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type System Basics Tricks Pitfalls Type System XQuery in TNTBase Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 5
  • 6. XQuery types XQuery Tutorial Taken from XML Schema Vyacheslav Zholudev Introduction XQuery Type System Basics Tricks Pitfalls XQuery in TNTBase Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 6
  • 7. Sequences in XQuery XQuery Tutorial Vyacheslav Zholudev Introduction Ordered list of objects XQuery Type System List of sequences is one sequence Basics One element is a one-element sequence Tricks Pitfalls Examples: XQuery in (“a”, “b”, “c”) TNTBase Conclusion ((“a”, “b”), “c”) ((“a”, “b”), (“c”, ())) Vyacheslav Zholudev XQuery Tutorial September 17, 2010 7
  • 8. XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type System Basics Tricks Pitfalls XQuery in “5 minutes” XQuery in TNTBase Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 8
  • 9. Basics XQuery Tutorial Getting a document using doc() function: Vyacheslav doc(’ http: // tntbase.org/sets.omdoc’) Zholudev <omdoc xmlns="http://omdoc.org/ns"> Introduction <theory xml:id="sets−operations" about="#sets−operations"> XQuery Type <imports from=" ../../ missing/en/highschool#highschool" xml:id="sets−operations. System <imports from="./sets−introduction#sets−introduction" xml:id="sets−operations.p2 Basics <imports from="./mathtalk−definitions#mathtalk−definitions" xml:id="sets−operati Tricks <symbol name="eset" xml:id="eset.sym" about="#eset.sym"/> <notation cd="sets−operations" name="eset"> Pitfalls <prototype> XQuery in <om:OMS cd="sets−operations" name="eset"/> TNTBase </prototype> Conclusion <rendering> <m:mi mathvariant="normal">&#x2205;</m:mi> </rendering> </notation> .... </omdoc> Vyacheslav Zholudev XQuery Tutorial September 17, 2010 9
  • 10. XPath Expressions XQuery Tutorial Selecting nodes using XPath: Vyacheslav doc(’ http: // tntbase.org/sets.omdoc’)/omdoc/theory/notation Zholudev OR Introduction doc(’ http: // tntbase.org/sets.omdoc’)/omdoc//notation XQuery Type <notation xmlns="http: // omdoc.org/ns" cd="sets−operations" name="eset"> System <prototype> Basics <om:OMS cd="sets−operations" name="eset"/> Tricks </prototype> Pitfalls <rendering> <m:mi mathvariant="normal">X</m:mi> XQuery in TNTBase </rendering> </notation> Conclusion <notation cd="sets−operations" name="powerset"> <prototype> <om:OMS cd="sets−operations" name="powerset"/> </prototype> ... </notation> ... <!−− More results −−> Vyacheslav Zholudev XQuery Tutorial September 17, 2010 10
  • 11. Predicates XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type doc(’ http: // tntbase.org/sets.omdoc’)//notation [3] System Basics doc(’ http: // tntbase.org/sets.omdoc’)//notation[ position () <4] Tricks doc(’ http: // tntbase.org/sets.omdoc’)//notation[@cd=’sets−operations’] Pitfalls doc(’ http: // tntbase.org/sets.omdoc’)//notation[prototype] XQuery in TNTBase doc (...)// notation [prototype/om:OMS/@name="eset"] Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 11
  • 12. FLOWR Expressions XQuery Tutorial Vyacheslav Zholudev Introduction FOR XQuery Type System LET Basics Tricks WHERE Pitfalls ORDER BY XQuery in TNTBase RETURN Conclusion Note: there is no GROUP BY in XQuery Vyacheslav Zholudev XQuery Tutorial September 17, 2010 12
  • 13. LET XQuery Tutorial Vyacheslav Variables are prefixed with $ Zholudev Query: Introduction let $a := 3 XQuery Type System let $b := 4 return Basics $a + $b Tricks Pitfalls Result: 7 XQuery in TNTBase Query: Conclusion 1 let $x := (<foo><bar/><bla/></foo>, <foo><baz/></foo>) return $x/∗ ( : all children elements :) Result: (<bar/>, <bla/>, <baz/>) Vyacheslav Zholudev XQuery Tutorial September 17, 2010 13
  • 14. FOR loop XQuery Tutorial Syntax: Vyacheslav Zholudev for $var in $sequence where <condition> Introduction return <result> XQuery Type System Examples: Basics Tricks for $notation in doc(’ http: // tntbase.org/sets.omdoc’)//notation 2 where $notation//∗[@name="eset"] Pitfalls return $notation/rendering XQuery in TNTBase Conclusion let $numbers := (2, 23, 1, 32, 8, 13, −1) 2 return for $num in $numbers where $num > 5 order by $num return −($num + 100) Vyacheslav Zholudev XQuery Tutorial September 17, 2010 14
  • 15. Wrapping results XQuery Tutorial let $ntns := tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’)//notation return Vyacheslav Zholudev <notations> 4 <amount>{count($ntns)}</amount> Introduction <renderings> { XQuery Type System for $ntn in $ntns return <my−rend>{$ntn/rendering}</my−rend> Basics 9 } Tricks </renderings> Pitfalls </notations> XQuery in TNTBase Result: Conclusion <notations> <amount>38</amount> <renderings> 4 <my−rend><rendering"><m:mi mathvariant="normal">X</m:mi></rendering> </my−rend> .... <!−− more results −−> </renderings> </notations> Vyacheslav Zholudev XQuery Tutorial September 17, 2010 15
  • 16. IF Expression XQuery Tutorial Vyacheslav Zholudev Note: "else" clause is mandatory Introduction Syntax: XQuery Type System if <condition> then $seq1 else $seq2 Basics Tricks Example: Pitfalls for $i in (1, " str " , 3, <foo/>) return XQuery in if ($ i instance of element()) then "element" TNTBase else if ($ i instance of xs:string ) then " string " Conclusion 4 else "other" Result: ( "other" , " string " , "other" , "element") Vyacheslav Zholudev XQuery Tutorial September 17, 2010 16
  • 17. Comparisons XQuery Tutorial Vyacheslav Zholudev Introduction General comparisons: =, !=, <, <=, >, >= XQuery Type System Value comparisons: eq, ne, lt, le, gt, ge Basics Tricks Value comparisons operate only on single elements Pitfalls (otherwise, error) XQuery in TNTBase (1, 2, 4) = (10, 2, 8) returns true Conclusion (1, 2, 4) eq (10, 2, 8) throws an error Vyacheslav Zholudev XQuery Tutorial September 17, 2010 17
  • 18. Axes XQuery Tutorial Forward Axes: Vyacheslav child (/*) Zholudev descendant (//*) Introduction attribute (/@*) XQuery Type System self (/.) Basics descendant-or-self Tricks following-sibling Pitfalls XQuery in following TNTBase Reverse Axes Conclusion parent (/..) ancestor preceding-sibling preceding ancestor-or-self Vyacheslav Zholudev XQuery Tutorial September 17, 2010 18
  • 19. Functions XQuery Tutorial Vyacheslav Zholudev Introduction Built-in Functions + User-defined functions XQuery Type System Built-in functions examples: Basics fn:count($seq) as xs:double Tricks fn:empty($seq) as xs:boolean Pitfalls XQuery in fn:uppercase($str as xs:string ) as xs:string TNTBase fn:substring ($ str , $pos) as xs:string Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 19
  • 20. User-defined functions XQuery Tutorial declare function prefix:function_name($parameter AS datatype) Vyacheslav AS returnDatatype Zholudev { ... function code here... Introduction 5 } XQuery Type System Example: Basics declare namespace omdoc = "http://omdoc.org"; Tricks ( : This function returns amounts of notations in OMDoc documents :) Pitfalls declare function omdoc:notations−amount($docs as document−node()∗) XQuery in as xs:double∗ TNTBase 5 { Conclusion for $d in $docs return count($d//notation) } Invocation: omdoc:notations−amount((doc("/doc1.omdoc"), doc("doc2.omdoc"))) Note: Types are optional, but highly recommended Vyacheslav Zholudev XQuery Tutorial September 17, 2010 20
  • 21. XQuery modules XQuery Tutorial Vyacheslav Zholudev Introduction An XQuery module is a set of user-defined functions XQuery Type System Every function should be in the module namespace Basics Tricks Module declaration: module namespace tnt="http://tntbase.org/ns"; Pitfalls Modules can’t contain expressions XQuery in TNTBase Modules can import other modules, the latter will not be Conclusion visible for those who import the former module Vyacheslav Zholudev XQuery Tutorial September 17, 2010 21
  • 22. Importing a module XQuery Tutorial Vyacheslav Syntax: Zholudev import module namespace <prefix>= "<ns_uri>"(at "<hint_for_a_processor";) Introduction Example: XQuery Type System import module namespace omdoc="http://omdoc.org/ns" at tntbase: /modules/omdoc−module.xq; Basics let $doc−names := (..., ..., ...) Tricks for $doc in $doc−names return Pitfalls 5 omdoc:get−notations($doc) XQuery in TNTBase The influence of the “at” keyword is Conclusion implementation-dependent Note: Having multiple modules with the same namespace is a bad practice and have non-deterministic behaviour Vyacheslav Zholudev XQuery Tutorial September 17, 2010 22
  • 23. XQuery Prolog XQuery Tutorial is a series of declarations and imports that define the Vyacheslav Zholudev processing environment (separated by a semicolon) Useful examples: Introduction XQuery Type Module imports (described before) System Basics Namespace declarations: Tricks declare namespace tnt="http://tntbase.org/ns"; Pitfalls declare default element namespace "http://omdoc.org/ns" XQuery in TNTBase Variable declarations: Conclusion declare variable $NUM := 10; Function declarations (described before) declare function local:test () as xs:string external ; Module declaration (described before) Vyacheslav Zholudev XQuery Tutorial September 17, 2010 23
  • 24. XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type System Basics Tricks Pitfalls XQuery Tricks XQuery in TNTBase Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 24
  • 25. Eliminating FOR XQuery Tutorial Vyacheslav Zholudev declare function omdoc:notations−amount($docs as document−node()∗) Introduction as xs:double XQuery Type { System for $d in $docs return count($d//notation) Basics 5 } Tricks Pitfalls declare function omdoc:notations−amount($docs as document−node()∗) XQuery in as xs:double TNTBase { Conclusion $docs/count(.// notation) 5 } Vyacheslav Zholudev XQuery Tutorial September 17, 2010 25
  • 26. Default namespace XQuery Tutorial Vyacheslav Zholudev Introduction We can define default namespace: XQuery Type System declare default element namespace "http://omdoc.org/ns" Basics How to address nodes in no namespace? Tricks declare default element namespace "http://omdoc.org/ns"; Pitfalls declare namespace no−ns = ""; XQuery in TNTBase tnt:doc ( ’ /sets−operations.omdoc’)/omdoc/theory[1]/no−ns:extra−element Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 26
  • 27. Preserving XML-formatting XQuery Tutorial Query: Vyacheslav 1 <foo> Zholudev <bar/> </foo> Introduction XQuery Type Result by default: System Basics <foo><bar/></foo> Tricks New query: Pitfalls declare boundary−space preserve; XQuery in TNTBase <foo> <bar/> Conclusion 4 </foo> Result: 1 <foo> <bar/> </foo> Vyacheslav Zholudev XQuery Tutorial September 17, 2010 27
  • 28. XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type System Basics Tricks Pitfalls Typical Pitfalls XQuery in TNTBase Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 28
  • 29. Pitfalls XQuery Tutorial Vyacheslav Zholudev $seq1 != $seq2, but meant: not($seq1 == $seq2) Introduction XQuery Type foo[bar is null ] instead of foo[empty(bar)] System Basics // foo[bar < 5 and bar > 1] vs. // foo[bar [. < 5 and . > 1]] Tricks In IF clause you always need ELSE Pitfalls There are no reserved words “true” and “false”. Use XQuery in TNTBase built-in functions fn:true() and fn:false(). Conclusion One can’t return <foo/> <bar/>. Should return a sequence: (<foo/>, <bar/>) Vyacheslav Zholudev XQuery Tutorial September 17, 2010 29
  • 30. Document collections XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type Document from HEAD: System tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’) Basics Tricks Collection from HEAD: Pitfalls tnt:collection ( ’ / slides // en/∗sets∗.omdoc’) XQuery in Document from previous revisions: TNTBase tnt:doc ( ’ / slides /dmath/en/sets−operations.omdoc’, 1397) Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 30
  • 31. Repository information XQuery Tutorial Vyacheslav Zholudev Introduction The last revision number: XQuery Type tnt:last −revnum() as xs:integer System Basics Revisions in which the file has been changed: Tricks tnt:file −revs($path as xs:string) as xs:integer∗ Pitfalls Get a document in the uncommitted revision: XQuery in tnt:txn −doc($path as xs:string, $svntxn as xs:string ) as document−node()? TNTBase Conclusion Get a file path to which an element belongs: tnt:get −doc−paths($docs as node()∗) as xs:string∗ Vyacheslav Zholudev XQuery Tutorial September 17, 2010 31
  • 32. VDoc functions XQuery Tutorial Vyacheslav Zholudev Test a VDoc Spec: tnt:vd −skel($path as xs:string) Introduction XQuery Type Get an underlying VDoc Spec path of the VDoc: System tnt:skel −path($path as xs:string) as xs:string ? Basics Tricks Get a VDoc: Pitfalls tnt:vdoc($path as xs:string ) as item() XQuery in Insert a parameter for the VDoc: TNTBase tnt:ins −vd−param($path as xs:string, $par as xs:string, $vals as xs:string +) Conclusion Delete a parameter of the VDoc: tnt:delete −vd−param($path as xs:string, $name as xs:string) ... some others Vyacheslav Zholudev XQuery Tutorial September 17, 2010 32
  • 33. Dynamic evaluation XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type System Query: Basics tnt:execute($xquery as xs:string ) Tricks Pitfalls Example: XQuery in count(tnt:execute( " tnt:collection (//∗. omdoc)")) TNTBase Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 33
  • 34. Some other functions XQuery Tutorial Vyacheslav Commit documents (dangerous): Zholudev tnt:commit($paths as xs:string ∗, $docs as element()∗, Introduction $msg as xs:string, $revnum as xs:integer) as xs:string XQuery Type System Basics XML-diff: Tricks tnt:xml −diff($ differ −name as xs:string, $l as element(), $r as element()) Pitfalls Get an auxiliary document produced by a plugin: XQuery in TNTBase tnt:typed −doc($path as xs:string, $type as xs:string +) Conclusion Substring before last occurrence of some string: tnt:substring −before−last($arg as xs:string?, $delim as xs:string ) Delete a parameter of the VDoc: tnt:delete −vd−param($path as xs:string, $name as xs:string) Vyacheslav Zholudev XQuery Tutorial September 17, 2010 34
  • 35. XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type System Basics Tricks Pitfalls Wrapping Up XQuery in TNTBase Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 35
  • 36. Non-covered topics XQuery Tutorial Vyacheslav Zholudev Introduction Type casting in XQuery XQuery Type System Stronger typing using XML Schema Basics Node construction Tricks XQuery Update facility Pitfalls XQuery in EXQuery TNTBase What’s new in XQuery 1.1? Conclusion XQuery Scripting and side-effects Vyacheslav Zholudev XQuery Tutorial September 17, 2010 36
  • 37. Advantages of XQuery XQuery Tutorial Vyacheslav Zholudev Introduction In context of XML DBs when benefiting from indexes XQuery Type System Processing big documents that do not fit into memory Basics (XSLT streaming doesn’t count here) Tricks Querying is better, then XSLT for formatting and Pitfalls presentation XQuery in TNTBase XQuery is easier to explain Conclusion Less verbose Vyacheslav Zholudev XQuery Tutorial September 17, 2010 37
  • 38. Advantages of XSLT XQuery Tutorial Vyacheslav Zholudev Introduction Recurring over trees is much easier XQuery Type System Better in formatting numbers Basics More mature technology Tricks Well-standardized library of extensions (EXSLT) Pitfalls XQuery in XML-language TNTBase XQuery is a subset of XSLT in terms of functionality Conclusion Has a form of polymorphism to overwrite templates Vyacheslav Zholudev XQuery Tutorial September 17, 2010 38
  • 39. Conclusion XQuery Tutorial Vyacheslav Zholudev Introduction XQuery Type System Basics You should pick out the tool Tricks Pitfalls that suits your task best! XQuery in TNTBase Conclusion Vyacheslav Zholudev XQuery Tutorial September 17, 2010 39