Sequences in XQuery Core data structure of XQuery Date: 8/25/2009 M D Metadata Solutions Dan McCreary President Dan McCreary & Associates [email protected] (952) 931-9198 Agenda/Objective • • • • • • • What are sequences in XQuery? Why are they so important? Comparison with RDBMS/SQL Why are they critical to understanding XQuery? Common operations on sequences A Comparison with other List Processing Languages Examples D Copyright 2008 Dan McCreary & Associates M 2 Two Ways to Represent Data Tables Trees • Tabular data assumes that every column has homogeneous data – Each item has the same data type • Sequences are collections of any types of data Note that the third way is a graph or RDF. M D Copyright 2008 Dan McCreary & Associates 3 Table Columns Hold Similar Data string date decimal float blob integer M • Columns must all have the same data type that is predefined in the Data Definition Language (DDL) using CREATE TABLE statements. D Copyright 2008 Dan McCreary & Associates 4 Compare • RDBMS: int int int int int int int int str str str str str str str str date date date date date date date date • General List Processing Languages: int str int date date str int str xml date xml str int str • XQuery: int str M D Copyright 2008 Dan McCreary & Associates 5 Sequences of XML items sequence XQuery is based on considering each group of XML documents as a sequence of items A sequences is of type item()* M D Copyright 2008 Dan McCreary & Associates 6 Sequences of Heterogeneous Items sequence A sequence can hold any number of items and each item can be a different type of “object”. M D Copyright 2008 Dan McCreary & Associates 7 Search Result Example A sequence of search “hits” book blog web play web • Each document in a “hit” returns an XML node • The data in each “hit” can be different M D Copyright 2008 Dan McCreary & Associates 8 Sequence Position item position 1 2 3 4 5 6 7 • Each “item” in a sequence is numbered • Numbering starts at “1” (not zero) • You can get the Nth item by adding the position as a “predicate” (square brackets) let $third-item := $sequence[3] M D Copyright 2008 Dan McCreary & Associates 9 Counting Items let $seq := (‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’) let $number-of-items := count($seq) • You can use the count function to count the number of items in any sequence • Example: – $number-of-items would be 7 M D Copyright 2008 Dan McCreary & Associates 10 Subsequences let $new-seq := subsequence($seq, $start, $num) • You can always create a new sequence using any range of items in any other sequence • Two arguments: – Number to start – Number of items you want in the new sequence M D Copyright 2008 Dan McCreary & Associates 11 Subsequence Operations 1 2 3 4 5 6 7 subsequence($seq, 1, 3) subsequence($seq, 2, 4) • Note that the second argument is the number of items returned M D Copyright 2008 Dan McCreary & Associates 12 Last() 1 2 3 4 5 6 7 Let $last-item := $seq[last()] • Returns the position of the last item in a sequence M D Copyright 2008 Dan McCreary & Associates 13 FLOWR Expressions on Sequences for $item in $sequence let $tmp := $item/path order by $tmp where $item/value = ' x ' return $item M D Copyright 2008 Dan McCreary & Associates 14 Common Sequence Functions 1. 2. 3. 4. 5. 6. 7. 8. 9. count subsequence distinct-values index-of insert-before last position remove reverse M D Copyright 2008 Dan McCreary & Associates 15 Arguments 1. 2. 3. 4. 5. 6. 7. 8. 9. count($seq as item()*) distinct-values($seq as item()*) index-of ($seq as anyAtomicType()*, $targetanyAtomicType()) insert-before($seq as item()*, $position as int, $inserts as item()*) last ($seq as item()*) position() as xs:nonNegativeInteger remove ($seq as item()*, $position as xs:nonNegativeInteger) reverse ($seq as item()*) subsequence ($seq as item()*, $start as xs:nonNegativeInteger, $num as xs:nonNegativeInteger) M D Copyright 2008 Dan McCreary & Associates 16 First Sort Then Get Top Items Always sort first Then get the top items let $sorted-seq := for $item in $seq order by $item/value return $item M let $top-items := subsequence($sorted-seq, 1, 10) • It is very common to need to get a sorted list of items from a large sequence • The trick is to sort the items first and then get the first N items D Copyright 2008 Dan McCreary & Associates 17 Exercises • Create a sample sequences using letters – let $input := (‘a’, ‘b’, ‘c’) • Create a simple XQuery to the common operations on a sequence. M D Copyright 2008 Dan McCreary & Associates 18 Thank You! Please contact me for more information: • • • • • • Native XML Databases Metadata Management Metadata Registries Service Oriented Architectures Business Intelligence and Data Warehouse Semantic Web Dan McCreary, President Dan McCreary & Associates Metadata Strategy Development [email protected] (952) 931-9198 M D Copyright 2008 Dan McCreary & Associates 19
© Copyright 2026 Paperzz