!!! Listings zum Artikel "Triangulaer"
!!! von Christoph Leisegang
!!! in iX 5/2009, S. 168
!!! Listing 1:
cl
Christoph Leisegang
Heise News
ticker
it
news
!!! Listing 2:
declare option exist:serialize "method=xml media-type=application/xml";
declare variable $bookmarks as node()* := collection('/db/bookmarks/data');
declare variable $user as xs:string := request:get-parameter('user', '');
declare variable $tag as xs:string := request:get-parameter('tag', '');
declare variable $hash as xs:string := request:get-parameter('hash', '');
declare function local:create-bookmark($b as element(),
$count as xs:integer, $tags as element()*) as element()
{
{ $b/@*, attribute count { $count }, $b/title }
{
for $distinct-tags in distinct-values($tags)
return
{ $distinct-tags }
}
};
{
(: Finde alle Bookmarks zu einem Benutzer :)
if( not($user eq '')) then
for $b in $bookmarks/user[name=$user]//bookmark
order by max($b/xs:dateTime(@date)) descending
return
local:create-bookmark($b, count($b), $b/tags/tag)
(: Finde alle Bookmarks zu einem 'tag' :)
else if(not($tag eq '')) then
let $b-same-tag := $bookmarks//bookmark[tags/tag = $tag]
for $distinct-hash in distinct-values($b-same-tag/@hash)
let $b-same-hash := $bookmarks/user/bookmarks/bookmark[@hash = $distinct-hash]
let $b-first := $b-same-hash[1]
order by max($b-same-hash/xs:dateTime(@date)) descending
return
local:create-bookmark($b-first, count($b-same-hash), $b-same-hash/tags/tag)
(: Finde alle Bookmarks aller Benutzer absteigend sortiert nach Datum :)
else
for $distinct-hash in distinct-values($bookmarks//bookmark/@hash)
let $b-same-hash := $bookmarks/user/bookmarks/bookmark[@hash = $distinct-hash]
let $b-first := $b-same-hash[1]
order by max($b-same-hash/xs:dateTime(@date)) descending
return
local:create-bookmark($b-first, count($b-same-hash), $b-same-hash/tags/tag)
}
--> XQuery leicht gekürzt
!!! Listing 3:
...
!!! Listing 4:
!!! Listing 5:
!!! Listing 6:
declare variable $data as element()? := request:get-data();
declare variable $bookmark := $data//bookmark;
declare variable $bookmarks as document-node()? := doc(concat('/db/bookmarks/data/', $data/name, '.xml'));
let $isLoggedIn := xdb:login("xmldb:exist:///db", "admin", "xxxx")
let $hash as xs:string := util:md5( concat( $bookmark/@href, $bookmark/title) )
let $old-bookmark := $bookmarks//bookmark[@hash = $hash]
return
(
update replace $data//bookmark/@hash with $hash,
if (count($bookmarks) > 0) then (
(: update existing bookmark with new tags:)
update replace $bookmarks/long-name with $data/user/long-name,
if(count($old-bookmark) > 0) then (
update replace $old-bookmark/tags with (
{
for $distinct-tag in distinct-values($data//bookmark[@hash = $hash]/tags/tag | $old-bookmark/tags/tag)
return
{ $distinct-tag }
}
) )
else
(: update existing document with new bookmark :)
update insert $data//bookmark preceding $bookmarks//bookmark[1] )
else
(: create new dokument :)
let $ret := xdb:store("xmldb:exist:///db/bookmarks/data", concat($data/name, ".xml"), $data )
return '' )
--> XQuery leicht gekürzt