H1 classitemhdrproduct catalogh1 ul li depwmnnumber

Info icon This preview shows pages 18–20. Sign up to view the full content.

<h1 class="itemHdr">Product Catalog</h1> <ul> <li dep="WMN">number: 557, name: Fleece Pullover</li> <li dep="ACC">number: 563, name: Floppy Sun Hat</li> <li dep="ACC">number: 443, name: Deluxe Travel Bag</li> <li dep="MEN">number: 784, name: Cotton Dress Shirt</li> </ul> </html> If the $prod element has no dept attribute, its value will be a zero-length string In previous example li will not have a dept attribute in this case 69 Declaring Namespaces in Direct Constructors Query <xhtml:html xmlns:xhtml=" "> <xhtml:h1 class="itemHdr">Product Catalog</xhtml:h1> <xhtml:ul>{ for $prod in doc("catalog.xml")/catalog/product return <xhtml:li class="{$prod/@dept}">number: { data($prod/number)}</xhtml:li> }</xhtml:ul> </xhtml:html> Results <xhtml:html xmlns:xhtml=" "> <xhtml:h1 class="itemHdr">Product Catalog</xhtml:h1> <xhtml:ul> <xhtml:li class="WMN">number: 557</xhtml:li> <xhtml:li class="ACC">number: 563</xhtml:li> <xhtml:li class="ACC">number: 443</xhtml:li> <xhtml:li class="MEN">number: 784</xhtml:li> </xhtml:ul> </xhtml:html> 70 XQuery – Adding Elements and Attributes to Results – 35 Modifying an Element from the Input Document Add to product elements an attribute id equal to P concatened with the product number Query for $prod in doc("catalog.xml")/catalog/product[@dept = ’ACC’] return <product id="P{$prod/number}"> {$prod/(@*, *)} </product> Results <product dept="ACC" id="P563"> <number>563</number> <name language="en">Floppy Sun Hat</name> </product> <product dept="ACC" id="P443"> <number>443</number> <name language="en">Deluxe Travel Bag</name> </product> {$prod/(@*, *)} copies all attributes and child elements from product element {$prod/(@*, * except number)} would copy all attributes and all child ele- ments but number 71 Computed Element Constructors (1) Uses the keyword element , followed by a name and some content in curly braces Query element html { element h1 { "Product Catalog" }, element ul { for $prod in doc("catalog.xml")/catalog/product return element li {"number:",data($prod/number),", name:",data($prod/name)} } } Results <html> <h1>Product Catalog</h1> <ul> <li>number: 557, name: Fleece Pullover</li> <li>number: 563, name: Floppy Sun Hat</li> <li>number: 443, name: Deluxe Travel Bag</li> <li>number: 784, name: Cotton Dress Shirt</li> </ul> </html> 72 XQuery – Adding Elements and Attributes to Results – 36
Image of page 18

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

Computed Element Constructors (2) Name can be an enclosed expression that evaluates to a qualified name as in element {concat("h",$level)} { "Product Catalog" } element {node-name($myNode)} { "contents" } will give the new element the same name as the node that is bound to the variable If expression is a node, it is atomized to extract its typed value (not its name) Content : enclosed expression for attributes, child elements, and character data <li>number: {data($prod/number)}, name: {data($prod/name)}</li> element li {"number:",data($prod/number),", name:",data($prod/name)} Values of four expressions above will be separated by spaces in the results element li {concat("number:", data($prod/number), ", name:", data($prod/name))} remove these spaces If the constructed element is to be empty, then put { } 73 Computed Attribute Constructors Same syntax as a computed element constructor, but uses
Image of page 19
Image of page 20
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