XQuery/Timelines of Resource

From Wikibooks, open books for an open world
Jump to: navigation, search

Motivation[edit]

You want to create a timeline using the creation and modification dates of a collection.

Method[edit]

Many file systems and XML databases such as the eXist database automatically keep two dates associated with each resource. One is the creation date and the other is the date the resource was last modified. These dates are required for many systems that preform incremental backups of resources.

We can use these dates to automatically create a timeline report of one or more collections. These reports can server as audit trails and can help you find out who changed what and when.

Here are two of the functions that we will use in these examples:

  xmldb:last-modified($collection as item(), $resource as xs:string) xs:dateTime?
  xmldb:created($collection as xs:string, $resource as xs:string) xs:dateTime

Note that you can also use the created function with a single parameter to see when a collection was created.

Our query will take a single parameter, the database path expression to the collection we wish to create a timeline for. Our timeline will then display the creation and modification dates for this collection.

Here is a sample query fragment that lists all child of the resources in a collection and formats the data according to the timeline event XML structure.

let $collection := '/db/test'
 
return
<data date-time-format="iso8601">{
   for $child in xmldb:get-child-resources($collection)
   return (
     <event start="{xmldb:created($collection, $child)}" isDuration="false">{$child} created</event>,
     <event start="{xmldb:last-modified($collection, $child)}" isDuration="false">{$child} last-modified</event>
     )
}</data>

This will return a file of in the following format:

<data date-time-format="iso8601">
    <event start="2009-02-17T12:50:55.992-06:00" isDuration="false">foo.xq created</event>
    <event start="2009-02-18T15:12:47.529-06:00" isDuration="false">foo.xq last-modified</event>
    <event start="2008-11-25T13:53:23.877-06:00" isDuration="false">bar.xq created</event>
    <event start="2008-11-25T14:22:27.798-06:00" isDuration="false">bar.xq last-modified</event>
    <event start="2008-11-25T15:39:40.445-06:00" isDuration="false">foo.xhtml created</event>
    <event start="2008-11-25T15:41:51.547-06:00" isDuration="false">bar.xhtml last-modified</event>
    <event start="2009-02-06T14:24:34.74-06:00" isDuration="false">hello-world.xml created</event>
    <event start="2009-02-06T15:13:24.251-06:00" isDuration="false">hello-world.xml last-modified</event>
    <event start="2008-11-25T14:07:00.273-06:00" isDuration="false">test.xml created</event>
    <event start="2008-11-25T14:07:00.273-06:00" isDuration="false">test.xml last-modified</event>
</data>