Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Functions Creating Hierarchical Reports Date: 9/30/2008 Dan McCreary President Dan McCreary & Associates (952) 931-9198 M.

Similar presentations


Presentation on theme: "Recursive Functions Creating Hierarchical Reports Date: 9/30/2008 Dan McCreary President Dan McCreary & Associates (952) 931-9198 M."— Presentation transcript:

1 Recursive Functions Creating Hierarchical Reports Date: 9/30/2008 Dan McCreary President Dan McCreary & Associates dan@danmccreary.com (952) 931-9198 M D Metadata Solutions

2 M D Copyright 2008 Dan McCreary & Associates2 Module Outline Why use recursive functions? Sitemap example Taxonomy example

3 M D Copyright 2008 Dan McCreary & Associates3 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

4 M D Copyright 2008 Dan McCreary & Associates4 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)

5 M D Copyright 2008 Dan McCreary & Associates5 For Loop vs. Recursion root row root branch for-loop branch recursion

6 M D Copyright 2008 Dan McCreary & Associates6 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

7 M D Copyright 2008 Dan McCreary & Associates7 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

8 M D Copyright 2008 Dan McCreary & Associates8 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

9 M D Copyright 2008 Dan McCreary & Associates9 Sample Web Site Hierarchy /db webroot faqsproductssupportabout xquery training xforms tei Start here eXistrest

10 M D Copyright 2008 Dan McCreary & Associates10 Using oXygen to Create Collections right click over collection

11 M D Copyright 2008 Dan McCreary & Associates11 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:

12 M D Copyright 2008 Dan McCreary & Associates12 Testing get-child-collections xquery version "1.0"; let $children := xmldb:get-child-collections('/db/webroot') return {$children} about faqs training products support Output: XQuery:

13 M D Copyright 2008 Dan McCreary & Associates13 For Loop about faqs training products support xquery version "1.0"; { for $child in xmldb:get-child-collections('/db/webroot') return {$child} } XQuery: Output:

14 M D Copyright 2008 Dan McCreary & Associates14 Declare Sitemap Function declare function local:sitemap($collection as xs:string) as node()* { for $child in xmldb:get-child-collections($collection) return {$child} {local:sitemap(concat($collection, '/', $child))} };

15 M D Copyright 2008 Dan McCreary & Associates15 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 {$child} {local:sitemap(concat($collection, '/', $child))} }; {local:sitemap('/db/webroot')}

16 M D Copyright 2008 Dan McCreary & Associates16 Results about faqs training xforms rest xquery products support

17 M D Copyright 2008 Dan McCreary & Associates17 HTML Output Sitemap {local:sitemap('/db/webroot')}

18 M D Copyright 2008 Dan McCreary & Associates18 HTML UL Output

19 M D Copyright 2008 Dan McCreary & Associates19 Adding HTML Links declare function local:sitemap($collection as xs:string) as node()* { for $child in xmldb:get-child-collections($collection) return <a href="{concat('/exist/rest', $collection, '/', $child)}">{$child} {local:sitemap(concat($collection, '/', $child))} };

20 M D Copyright 2008 Dan McCreary & Associates20 Output http://localhost:8080/exist/rest/db/webroot/about

21 M D Copyright 2008 Dan McCreary & Associates21 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 {local:sitemap('/db/webroot')}

22 M D Copyright 2008 Dan McCreary & Associates22 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. /db/webroot/training Training /db/webroot/faqs Frequently Asked Questions $title := $code-table/item[$path=path]/title

23 M D Copyright 2008 Dan McCreary & Associates23 Output

24 M D Copyright 2008 Dan McCreary & Associates24 Taxonomy A hieratical classification system based on subject areas Thing PersonLocationEventPublication BookBlogArticle

25 M D Copyright 2008 Dan McCreary & Associates25 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() 3 Person An individual Human Being 1

26 M D Copyright 2008 Dan McCreary & Associates26 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, President Dan McCreary & Associates Metadata Strategy Development dan@danmccreary.com (952) 931-9198


Download ppt "Recursive Functions Creating Hierarchical Reports Date: 9/30/2008 Dan McCreary President Dan McCreary & Associates (952) 931-9198 M."

Similar presentations


Ads by Google