XQuery Labs Basic Reporting Date: 9/29/2008 Dan McCreary President Dan McCreary & Associates...

Post on 19-Jan-2016

212 views 0 download

transcript

XQuery LabsBasic ReportingDate: 9/29/2008

Dan McCrearyPresidentDan McCreary & Associatesdan@danmccreary.com(952) 931-9198

M

D

Metadata Solutions

Copyright 2008 Dan McCreary & Associates 2

M

D

Overview

• Hello World• XML Report• HTML List• Limiting Results with subsequence• HTML Table• HTML Table with Header• HTML Table with CSS• HTML Table with Row Count• HTML Table with Alternate Rows Shaded• HTML Table with Links• Getting Next N Rows

Copyright 2008 Dan McCreary & Associates 3

M

D

Term Manager Home Page

/db/apps/terms/index.xhtml

Copyright 2008 Dan McCreary & Associates 4

M

D

The following lines may be omitted…

xquery version "1.0";declare option exist:serialize "method=xhtml media-

type=text/html omit-xml-declaration=yes indent=yes";

let $collection := ‘/db/apps/terms/data’

or

let $get-uri := request:get-uri()let $app-base := substring-before(substring-after($get-

uri, '/exist/rest'), '/views/')let $collection := concat($app-base, '/data')

Copyright 2008 Dan McCreary & Associates 5

M

D

Basic Structure of XQuery

xquery version "1.0";

let $collection := ‘/db/apps/terms/data’

return<terms>{ for $term in collection($collection)/term return $term }</terms>

Copyright 2008 Dan McCreary & Associates 6

M

D

HTML Ordered List…return<html> <head> <title>Terms</title> </head> <body> <ol>{ for $term in collection($collection)/term return <li>{$term/name/text()}</li> }</ol> </body></html>

Copyright 2008 Dan McCreary & Associates 7

M

D

Returning Just the First 30 Terms<html> <head> <title>Terms</title> </head> <body> <ol>{ for $term in subsequence(collection($collection)/term, 1, 30) return <li>{$term/name/text()} - {$term/definition[1]/text()}</li> }</ol> </body></html>

startcount

Copyright 2008 Dan McCreary & Associates 8

M

D

HTML Table Output <body> <h1>Terms and Defintions</h1> <table border="1"> <thead> <th>Term</th> <th>Definition</th> </thead> <tbody>{ for $term in subsequence(collection($collection)/term, 1, 30) return <tr> <td>{$term/name/text()}</td> <td>{$term/definition[1]/text()}</td> </tr> }</tbody> </table> </body>

Copyright 2008 Dan McCreary & Associates 9

M

D

Order and First Definition<tbody>{ for $term in subsequence(collection($collection)/term, 1, 30) order by $term/name/text() return <tr> <td>{$term/name/text()}</td> <td>{$term/definition[1]/text()}</td> </tr>}</tbody>

only return the first definition

Copyright 2008 Dan McCreary & Associates 10

M

D

Using Cascading Style Sheetsreturn<html> <head> <title>HTML Table</title> <link rel="stylesheet" href="06-table.css" type="text/css"/> </head> <body> <div id="wrapper"> <div id="banner"> <img src="../hobanner.gif" width="900" height="130" alt="Office of the

Historian banner"/> </div> <div class="content"> <h4>Terms and Defintions</h4> <table>

Copyright 2008 Dan McCreary & Associates 11

M

D

Adding “at $count” to Query

<tbody>{ for $term at $count in subsequence(collection($collection)/term, 1, 30) return <tr> <td>{$count}</td> <td>{$term/name/text()}</td> <td>{$term/definition[1]/text()}</td> </tr>}</tbody>

Copyright 2008 Dan McCreary & Associates 12

M

D

Conditional Styling

<tbody>{ for $term at $count in

subsequence(collection($collection)/term, 1, 30) return <tr> {if ($count mod 2) then (attribute {'bgcolor'} {'Lavender'})

else ()} <td>{$count}</td> <td>{$term/name/text()}</td> <td>{$term/definition[1]/text()}</td> </tr>}</tbody>

Copyright 2008 Dan McCreary & Associates 13

M

D

Adding a Link to the Name <tbody>{ for $term at $count in subsequence(collection($collection)/term, 1, 30) return <tr> {if ($count mod 2) then (attribute {'bgcolor'} {'Lavender'}) else ()} <td><a href="view-item.xq?id={$term/name/@id}">{$term/name/text()}

</a> </td> <td>{$term/definition[1]/text()}</td> </tr>}</tbody>

This will allow the use to click on a term name and jumpdirectly to the item viewer for the terms.

Copyright 2008 Dan McCreary & Associates 14

M

D

Getting N More Recordslet $start := xs:integer(request:get-parameter("start", "1"))let $records := xs:integer(request:get-parameter("records", "20"))let $query-base := request:get-url()… for $term at $count in subsequence(collection($collection)/term, $start, $records) return… <input type="button" onClick="parent.location='{$query-base}?start={$start -

$records}&amp;records={$records}'" value="Previous"/> <input type="button" onClick="parent.location='{$query-base}?start={$start +

$records}&amp;records={$records}'" value="Next"/>

Copyright 2008 Dan McCreary & Associates 15

M

D

View Item

let $id := request:get-parameter('id', '')… <h1>View Item</h1> {let $item := collection($collection)/term[name/@id = $id] return <table border="1"> <tr><th>ID:</th><td>{string($item/name/@id)}</td></tr> <tr><th>Term:</th><td>{$item/name/text()}</td></tr> { for $def at $count in $item/definition return <tr> <td>Definition {$count}:</td> <td>{$def/text()} volume={string($def/@volume)}</td> </tr> } </table> }

/db/apps/terms/views/view-item.xq?id=123

Copyright 2008 Dan McCreary & Associates 16

M

D

eXist Keyword Extensions

• All Keywords selects context nodes containing ALL of the keywords in the right-hand argument in any order. The default tokenizer is used to split the right-hand argument into single tokens, i.e. any punctuation or white spaces are used to separate the keywords and, after which, are omitted. Note also that wildcards are allowed, and keyword comparison is NOT case-sensitive.

• Any Keywords Similar to above, this operator selects context nodes containing ANY of the keywords in the right-hand argument.

&=

|=

Copyright 2008 Dan McCreary & Associates 17

M

D

Search

(: the search query string :)let $q := request:get-parameter('q', "")

(: put the search results into memory :)let $search-results := collection($collection)//term[*/text() &= $q]let $count := count($search-results)

/db/apps/terms/search/search.xq?q=test Match All Keywords

All sub elements of term

Copyright 2008 Dan McCreary & Associates 18

M

D

Search Results List

<ol>{

for $term in $search-results

let $id := $term/name/@id

let $term-name := $term/name/text()

order by upper-case($term-name)

return

<li>

<a href="../views/view-item.xq?id={$id}">{$term-name}</a>

</li>

}</ol>

Copyright 2008 Dan McCreary & Associates 19

M

D

Search.xhtml <xf:model> <xf:instance xmlns=""> <data> <q/> </data> </xf:instance> <xf:submission id="search" method="get" action="search.xq" replace="all"/>… <xf:input ref="q" id="search-input" incremental="true"> <xf:label>Search string:</xf:label> <xf:action ev:event="DOMActivate"> <xf:send submission="search"/> </xf:action></xf:input>

<xf:submit submission="search"> <xf:label>Search</xf:label></xf:submit>

Copyright 2008 Dan McCreary & Associates 20

M

D

Search Results

Copyright 2008 Dan McCreary & Associates 21

M

D

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, PresidentDan McCreary & Associates

Metadata Strategy Developmentdan@danmccreary.com

(952) 931-9198