German English

XMach-1 Appendix

XMach-1 Appendix

Queries

The following queries are the query operations defined in the XMach-1 specification formulated in the XQuery language according to the working draft from 7 June 2001. All queries are tested with the XQuery demo from X-Hive.

Q1 Description: Get document with given URL.
Parameter: URL = /ahost1.bhost2.chost3/001_loader1.xml
LET $a := /directory/host[@name="chost3"]/host[@name="bhost2"]/host[@name="ahost1"]
             /path[@name="001_loader1.xml"]/doc_info/@doc_id,
    $b := /*[@doc_id = $a]
RETURN $b
Q2 Description: Get doc_id from documents containing a given phrase in paragraph elements. *
Parameter: phrase = "test pattern"
FOR $a IN /*[@doc_id]
WHERE SOME $p IN $a//paragraph SATISFIES contains($p, "test pattern")
RETURN distinct($a/@doc_id)
Q3 Description: Start with first chapter element and recursively follow first section element. Return last section element. *
Parameter: doc_id = "d1"
suffix = "10"
DEFINE FUNCTION deepestFirstSection(ELEMENT $e) RETURNS ELEMENT
{
  IF (empty($e/section10)) THEN $e
  ELSE deepestFirstSection($e/section10[1])
}
          
deepestFirstSection(/document10[@doc_id="d1"]/chapter10[1]/section10[1])
Q4 Description: Return flat list of head elements which are children of section elements. *
Parameter: doc_id = "d1"
suffix = "10"
FOR $a IN /document10[@doc_id="d1"]//section10/head10
RETURN $a
Q5 Description: Get document name (last path element in directory structure) from all documents which are below a given URL fragment.
Parameter: URL = "/ahost1.bhost2.chost3/"
FOR $a IN /directory/host[@name="chost3"]/host[@name="bhost2"]/host[@name="ahost1"]//path[doc_info]
RETURN $a/@name
Q6 Description: Get doc_id and id of parent element of author element with given content.
Parameter: Author = "Karl May"
FOR $a IN /*[@doc_id]
WHERE $a//*/author="Karl May"
RETURN
  <document>
    <doc_id>{string($a/@doc_id)}</doc_id>
    {
      FOR $e IN $a//*[author="Karl May"]
      RETURN
        <parent_id>{string($e/@id)}</parent_id>
    }
  </document>
Q7 Description: Get doc_id from documents which are referenced by at least four other documents.
NAMESPACE xlink='http://www.w3.org/1999/xlink'
FOR $refID IN distinct(/*//link/@xlink:href)
LET $refDocs := distinct(/*[.//link/@xlink:href=$projekte/XML/refID]/@doc_id)
WHERE count($refDocs) >= 4
RETURN
  <docid>{string($refID)}</docid>
Q8 Description: Get doc_id from the last 100 updated documents having an author attribute.
LET $b :=
  (FOR $a IN /directory//doc_info[@update_time] SORTBY (./@update_time DESCENDING)
   WHERE not(empty(/*[@doc_id=$a/@doc_id]/@author))
   RETURN
     <docid>{string($a/@doc_id)}</docid>
  )
RETURN
  $b[1 TO 100]
*The query was slightly changed compared to the specification document in order to focus on more specific tasks.