INFORMATIO
xquerynotes_2.pdf

# Let items allitemsdept d and num n where

• 39

This preview shows pages 27–29. Sign up to view the full content.

let \$items := \$allItems[@dept = \$d and @num = \$n] where sum(\$items/@quantity) > 1 order by count(\$items) return if (exists(\$items)) then <group dept="{\$d}" num="{\$n}" numItems="{count(\$items)}" totQuant="{sum(\$items/@quantity)}"/> else ( ) Results <group dept="ACC" num="443" numItems="1" totQuant="2"/> <group dept="WMN" num="557" numItems="2" totQuant="2"/> 106 XQuery – Sorting and Grouping – 53 Constraining and Sorting on Aggregated Values with group by Clause Similar to previous query, but display only groups whose total quantity ( totQuant ) is greater than 1, and sort results by the number of items ( numItems ) for \$items := doc("order.xml")//item group by \$items/@dept, \$items/@num where sum(\$items/@quantity) > 1 order by count(\$items) return if (exists(\$items)) then <group dept="{\$d}" num="{\$n}" numItems="{count(\$items)}" totQuant="{sum(\$items/@quantity)}"/> else ( ) Results <group dept="ACC" num="443" numItems="1" totQuant="2"/> <group dept="WMN" num="557" numItems="2" totQuant="2"/> 107 XQuery: Contents Introduction to XQuery XQuery Foundations XQuery Expressions Navigating Input Documents Using Paths Adding Elements and Attributes to Results Selecting and Joining Using FLWORs Sorting and Grouping Functions Updates 108 XQuery – Functions – 54

This preview has intentionally blurred sections. Sign up to view the full version.

Functions XQuery has a standard set of built-in functions Some string functions 3 string(arg) : Returns the string value of the argument 3 concat(string,string,...) : Returns the concatenation of the strings 3 string-join((string,string,...),sep) : Returns a string created by con- catenating the string arguments and using the sep argument as the separator 3 substring(string,start,len) , substring(string,start) : Returns the substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end 3 . . . Function calls can be included anywhere an expression is permitted, as in 3 let \$name := substring(\$prodName,1,5) 3 <name>{substring(\$prodName,1,5)}</name> 3 doc("catalog.xml")/catalog/product[substring(name,1,5) = ’Short’] 109 Function Names Functions have namespace-qualified names Built-in functions : in namespace fn prefix for these functions is unnecessary User-defined functions must be called by its prefixed name Functions declared in the same query module can be called using the same prefixed name found in the declaration local : built-in prefix for locally declared functions declare function local:return2 ( ) as xs:integer {2}; <size>{local:return2( )}</size> Functions in a separate query module have a different namespace that needs to be declared Call a function discountPrice in the namespace : import module namespace prod = " " at " "; <price>{prod:discountPrice( )}</price> 110 XQuery – Functions – 55 Function Signatures Describe the inputs and outputs of a function Signature of the built-in upper-case function upper-case(\$arg as xs:string?) as xs:string Signature indicates 3 Name of the function: upper-case 3 List of parameters: only one, whose name is \$arg
This is the end of the preview. Sign up to access the rest of the document.
• Winter '15
• MAhmoudali

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern