INFORMATIO
xquerynotes_2.pdf

Each parameter has a unique name and optionally a

Info icon This preview shows pages 30–32. Sign up to view the full content.

Each parameter has a unique name, and optionally a type Name is expressed as a variable name, preceded by $ When a function is called, the variable specified is bound to the value that is passed to it If no type is specified for a particular parameter, it allows any argument However, it is best to specify a type, for error checking and clarity When the function is called, each argument value is converted to the appropriate type according to the function conversion rules 119 Arguments: Nodes versus Atomic Values Accept a node that contains an atomic value, or accept the atomic value itself? In the declaration of local:discountPrice , the price and discountPct ele- ment could be accepted instead of accepting their xs:decimal and xs:integer values In some cases it is advantageous to pass the entire element as an argument 3 Need to access its attributes, e.g., currency attribute of price 3 Need to access its parent or siblings It is generally better to accept the atomic value 3 It is more flexible: a node can be passed to a function that expects an atomic value, but an atomic value cannot be passed to a function that expects a node 3 Can be more specific about the desired type of the value, e.g., to ensure that it is an xs:integer 3 Don’t have to cast untyped values to the desired type; this will happen au- tomatically 120 XQuery – Functions – 60
Image of page 30

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

Accepting Arguments that are the Empty Sequence Given previous local:discountPrice function, suppose $prod has no discount child $discount is bound to the empty sequence The function returns the empty sequence all arithmetic operations on the empty sequence return the empty sequence The function should return the original price if no discount amount is provided declare function local:discountPrice( $price as xs:decimal?, $discount as xs:decimal?, $maxDiscountPct as xs:integer?) as xs:double? { let $newDiscount := if ($discount) then $discount else 0 let $maxDiscount := if ($maxDiscountPct) then ($price * $maxDiscountPct) div 100 else 0 let $actualDiscount := min(($maxDiscount, $discount)) return ($price - $actualDiscount) }; local:discountPrice($prod/price, $prod/discount, 15) 121 Recursive Functions Functions can recursively call themselves Count the number of descendant elements of an element, not just the immediate children, but all the descendants declare namespace functx = " "; declare function functx:num-descendant-elements ($el as element( )) as xs:integer { sum(for $child in $el/* return functx:num-descendant-elements($child) + 1) }; There must be a level at which the function stops calling itself In this case, it will eventually reach an element that has no children: the for clause will not be evaluated returning an empty sequence, and sum(()) yields 0 Following function declare function local:addItUp () { 1 + local:addItUp( ) }; results in an infinite loop, which will possibly end with an “out of memory” or “stack full” error 122 XQuery – Functions – 61 Transitive Closure (1) employees.xml : Supervision relationship between employees
Image of page 31
Image of page 32
This is the end of the preview. Sign up to access the rest of the document.
  • Winter '15
  • MAhmoudali

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern