Return elements and attributes as they appear in

Info icon This preview shows pages 3–5. Sign up to view the full content.

: return elements and attributes as they appear in input documents 3 Appear in the result with the same names, the same attributes and contents, and in the same order as in the input document 10 XQuery – Introduction – 5 FLWORs FLWOR expression: basic structure of many (but not all) queries Pronounced “flower”, it stands for for , let , where , order by , return , the keywords used in the expression FLWORs, unlike path expressions, allows one to manipulate , transform , and sort results Return the names of all products in the ACC department for $product in doc("catalog.xml")/catalog/product let $name := $product/name where $product/@dept = "ACC" order by $name return $name Equivalent to the following path expression doc("catalog.xml")/catalog/product[@dept = "ACC"]/name Results <name language="en">Deluxe Travel Bag</name> <name language="en">Floppy Sun Hat</name> 11 Clauses of a FLWOR Expression for 3 Iterates through the product nodes: the rest of the FLWOR expression is evaluated once for each of the four products 3 Each time, a variable named $prod is bound to a different product element let 3 Used to set the value of a variable using := 3 Unlike a for clause, it does not set up an iteration where 3 Selects only products in the ACC department 3 Has the same effect as a predicate [@dept = "ACC"] in a path expression order by 3 Sorts the results by product name, which is not possible with path expressions return 3 Indicates that the product element’s name children should be returned 12 XQuery – Introduction – 6
Image of page 3

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

Adding XML Elements (1) XML constructors can be used to create elements and attributes that appear in the query results Wrap the name elements in a ul element in XHTML <ul>{ for $product in doc("catalog.xml")/catalog/product where $product/@dept=’ACC’ order by $product/name return $product/name }</ul> Results <ul> <name language="en">Deluxe Travel Bag</name> <name language="en">Floppy Sun Hat</name> </ul> Everything between the ul start tag and end tag is an element constructor Enclosed expression : expression between { } that is to be evaluated Enclosed expression returns two elements, which become children of ul 13 Adding XML Elements (2) Content in an element constructor outside { } appears in the results as is Query <h1>There are {count(doc("catalog.xml")//product)} products.</h1> Results <h1>There are 4 products.</h1> Wrap each resulting name element in its own li element <ul>{ for $product in doc("catalog.xml")/catalog/product where $product/@dept=’ACC’ order by $product/name return <li>{$product/name}</li> }</ul> Results <ul> <li><name language="en">Deluxe Travel Bag</name></li> <li><name language="en">Floppy Sun Hat</name></li> </ul> 14 XQuery – Introduction – 7 Adding XML Elements (3) Built-in function data() can be used to extract the contents of an element Query <ul>{ for $product in doc("catalog.xml")/catalog/product where $product/@dept=’ACC’ order by $product/name return <li>{data($product/name)}</li> }</ul> Results <ul> <li>Deluxe Travel Bag</li> <li>Floppy Sun Hat</li> </ul> 15 Adding XML Attributes
Image of page 4
Image of page 5
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