Post on 03-Jan-2016
transcript
Recursive FunctionsCreating Hierarchical Reports
Date: 9/30/2008
Dan McCrearyPresidentDan McCreary & Associatesdan@danmccreary.com(952) 931-9198
M
D
Metadata Solutions
Copyright 2008 Dan McCreary & Associates 2
M
D
Module Outline
• Why use recursive functions?
• Sitemap example
• Taxonomy example
Copyright 2008 Dan McCreary & Associates 3
M
D
Why Use Recursive Functions?
• Allows developers to create very simple data structures
• Allows hierarchical views of data
• Recursive functions are usually very short
• Allows developers to put different types of data at any level of a document
Copyright 2008 Dan McCreary & Associates 4
M
D
What is Recursion?
• Functions that call themselves• Ideal for:
– Containers that contain containers– Folders that contain sub-folders– Collections that contain sub-collections– Groupings that have sub-groupings– Classification systems that have sub-
classification systems (aka Taxonomies)
Copyright 2008 Dan McCreary & Associates 5
M
D
For Loop vs. Recursion
root
row
row
row
row
row
root
branchbranch
branch
for-loop
branch
branch
branch
recursion
Copyright 2008 Dan McCreary & Associates 6
M
D
branch
branch
branch
branch
How Does It Work?
• Imagine a bug crawling up a tree. When it hits a branch it spawns a new bug for each branch.
branch
branch
branch
Copyright 2008 Dan McCreary & Associates 7
M
D
Site Map Example
• Sitemaps are web pages that present a summary of the structure of a web site
• Each collection can be associated with a content area
• Collections can be moved at any time so you don’t want to have to manually build a site map
Copyright 2008 Dan McCreary & Associates 8
M
D
Site Maps with eXist
• Each collection will be a content area• Create a function that will list all the child-
collections of any given collection• Allow that function to call itself• Start at the site root node and get all child
collections• Use HTML nested lists to display the results
Copyright 2008 Dan McCreary & Associates 9
M
D
Sample Web Site Hierarchy
/db
webroot
faqs products support about
xquery
training
xformstei
Start here
eXist rest
Copyright 2008 Dan McCreary & Associates 10
M
D
Using oXygen to Create Collections
right clickover collection
Copyright 2008 Dan McCreary & Associates 11
M
D
get-child-collections()
• This function takes a single input collection path returns a a set of strings, each string is a child of the current collection
xmldb:get-child-collections($collection as xs:string) as xs:string*
let $children := xmldb:get-child-collections(‘/db/webroot’)
From documentation:
Example:
Copyright 2008 Dan McCreary & Associates 12
M
D
Testing get-child-collections
xquery version "1.0";
let $children := xmldb:get-child-collections('/db/webroot')
return<results> <children>{$children}</children></results>
<results> <children>about faqs training products support</children></results>
Output:
XQuery:
Copyright 2008 Dan McCreary & Associates 13
M
D
For Loop
<results> <child>about</child> <child>faqs</child> <child>training</child> <child>products</child> <child>support</child></results>
xquery version "1.0";
<results>{ for $child in xmldb:get-child-collections('/db/webroot') return <child>{$child}</child>}</results>
XQuery:
Output:
Copyright 2008 Dan McCreary & Associates 14
M
D
Declare Sitemap Function
declare function local:sitemap($collection as xs:string) as node()* {
for $child in xmldb:get-child-collections($collection) return <ul> <li>{$child}</li> {local:sitemap(concat($collection, '/', $child))} </ul>};
Copyright 2008 Dan McCreary & Associates 15
M
D
Test Function
xquery version "1.0";
declare function local:sitemap($collection as xs:string) as node()* { for $child in xmldb:get-child-collections($collection) return <ul> <li>{$child}</li> {local:sitemap(concat($collection, '/', $child))} </ul>}; <site>{local:sitemap('/db/webroot')}</site>
Copyright 2008 Dan McCreary & Associates 16
M
D
Results
<site> <ul> <li>about</li> </ul> <ul> <li>faqs</li> </ul> <ul> <li>training</li> <ul> <li>xforms</li> </ul> <ul> <li>rest</li> </ul>
<ul> <li>xquery</li> </ul> </ul> <ul> <li>products</li> </ul> <ul> <li>support</li> </ul></site>
Copyright 2008 Dan McCreary & Associates 17
M
D
HTML Output
<html> <head> <title>Sitemap</title> </head> <body> {local:sitemap('/db/webroot')} </body></html>
Copyright 2008 Dan McCreary & Associates 18
M
D
HTML UL Output
Copyright 2008 Dan McCreary & Associates 19
M
D
Adding HTML Links
declare function local:sitemap($collection as xs:string) as node()* {
for $child in xmldb:get-child-collections($collection) return <ul> <li><a href="{concat('/exist/rest', $collection, '/', $child)}">{$child}</a></li> {local:sitemap(concat($collection, '/', $child))} </ul>};
Copyright 2008 Dan McCreary & Associates 20
M
D
Output
http://localhost:8080/exist/rest/db/webroot/about
Copyright 2008 Dan McCreary & Associates 21
M
D
Adding Level
• Add another parameter called “level” to the sitemap function that is of type integer. Start the root with level 0 and increment the level each time you call yourself
<body> {local:sitemap('/db/webroot')}</body>
Copyright 2008 Dan McCreary & Associates 22
M
D
Adding Titles
• Most Sitemaps create a “user friendly” title that might include mixed case and spaces.
• Create a lookup table that stores a label or title for each collection. The title might include spaces and other special characters that are not used in collection names.
<code-table> <item> <path>/db/webroot/training</path> <title>Training</title> </item> <item> <path>/db/webroot/faqs</path> <title>Frequently Asked Questions</title> </item></code-table>
$title := $code-table/item[$path=path]/title
Copyright 2008 Dan McCreary & Associates 23
M
D
Output
Copyright 2008 Dan McCreary & Associates 24
M
D
Taxonomy
• A hieratical classification system based on subject areas
Thing
PersonLocationEvent Publication
Book BlogArticle
Copyright 2008 Dan McCreary & Associates 25
M
D
Taxonomy Lab
• Create a simple hierarchical taxonomy management system similar to the Dewey decimal system for libraries.
• Put individual items in XML files.• Call the root item “Thing”. Create narrower terms for “Person”,
“Event”, “Organization”, “Place” and “Publication”• Each “item” has one-and-only-one broader term.• Create functions for get-narrower-items(), and get-hierarchy()
<item> <id>3</id> <label>Person</label> <definition>An individual Human Being</definition> <broader-id>1</broader-id></item>
Copyright 2008 Dan McCreary & Associates 26
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