Query 1 Randomly generate 5 numbers in the range of AtomicPart's MyID, then return the AtomicPart according to the 5 numbers.
In XQuery FOR $a IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart/Connection/AtomicPart
          [@MyID = 221 or @MyID = 1000 or @MyID = 535 or @MyID = 13 or @MyID = 2000]
RETURN $a
Query 2 Randomly generate 5 titles for Documents, then return the first paragraph of the Document by lookup on these titles.
In XQuery FOR $d IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart/Document
          [  @title = "Composite Part 00000009"
          or @title = "Composite Part 00000050"
          or @title = "Composite Part 00000034"
          or @title = "Composite Part 00000022"
          or @title = "Composite Part 00000080"]
RETURN $d/para[1]
Query 3 Select 5% of AtomicParts via buildDate (in a certain period).
In XQuery FOR $a IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart/Connection/AtomicPart
          [@buildDate .>=. 1900 and @buildDate .<. 1950]
Return $a
Query 4 Find the CompositePart if it is later than BaseAssembly it is using (comparing the buildDate attribute).
In XQuery FOR $b IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly/BaseAssembly,
    $c IN $b/CompositePart[@buildDate .>. $b/@buildDate]
RETURN $c
Query 5 Within the same BaseAssembly, return the AtomicParts once finding a Document that has MyID equals to its docId.
In XQuery FOR $b IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly/BaseAssembly,
    $d IN $b/CompositePart/Document
LET $a := $b/CompositePart/Connection/AtomicPart
WHERE  $d/@MyID = $a/@docId
RETURN $a
Query 6 Select all BaseAssemblies with earlier buildDate from one XML database where it has the same "type" attributes as the BaseAssemblies in another database.
In XQuery FOR $b1 IN document("small31.xml")
           /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly/BaseAssembly,
    $b2 IN document("/export/home/liyg/genxml/small32.xml")
           /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly/BaseAssembly
Where   $b1/@type = $b2/@type
and     $b1/@buildDate .<. $b2/@buildDate
RETURN  $b1
Query 7 Randomly generate two phrases among all phrases in Documents. Select those documents containing the 2 phrases.
In XQuery FOR $d IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart/Document
          [contains(., "00000010") and contains(., "document")]
Return $d
Query 8 (to be changed)
In XQuery กก
Query 9 Select all AtomicParts with corresponding CompositeParts as their sub-elements.
In XQuery FOR $a IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart/Connection/AtomicPart
Return
  <AtomicPart $a/@*>
    shallow($a/../..)
  </AtomicPart>
Query 10 Select all ComplexAssembly with type "type008" without the knowledge of the path.
In XQuery FOR $ca IN document("small31.xml")//ComplexAssembly[./@type = "type008"]
RETURN $ca
Query 11 Among the first 5 Connections of each CompositePart,
select those with length greater than "len".
In XQuery FOR $c IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart
RETURN
   $c/Connection[position() .<=. 5][@length .>. 60000]
Query 12 For each CompositePart, select the first 5 Connections with length greater than "len".
In XQuery FOR $c IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart
RETURN
   $c/Connection[@length .>. 60000][position() .<=. 5]
Query 13 For each BaseAssembly count the number of documents.
In XQuery FOR $b IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly/BaseAssembly
LET $d := $b/CompositePart/Document
RETURN count($d)
Query 14 Sort CompositePart in descending order where buildDate is within a year from current year.
In XQuery FUNCTION year()
{
  "2002"
}

FOR $c IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart
Where  $c/@buildDate .>=. (year()-1)
RETURN
  <result>
    $c
  </result>
sortby (buildDate DESCENDING)
Query 15 Find BaseAssembly of not type "type008".
In XQuery FOR $b IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly/BaseAssembly
          [not(@type='type008')]
RETURN $b
Query 16 Return all BaseAssembly of type "type008" without any child nodes.
In XQuery FOR $b IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly[./@type="type008"]
Return shallow($b)
Query 17 Return all CompositePart having Connection elements with length greater than Avg(length) within the same CompositePart without child elements.
In XQuery FOR $c IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly           /BaseAssembly/CompositePart,
     $con IN $c/Connection[./@length .>. avg($c/Connection/@length)]
Return shallow($con)
Query 18 For CompositePart of type "type008", give 'Result' containing ID of CompositePart and Document.
In XQuery FOR $c IN document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart
Return
  <Result $c/@MyID>
    $c/Document
  </Result>
Query 19 Select all of CompositePart, Document and AtomicPart.
In XQuery <Result>
  Let $m := document("small31.xml")
  FILTER (self::CompositePart OR self::Document OR self::AtomicPart)
  return $m
</Result>
Query 20 Select the last connection of each CompositePart.
In XQuery For $c in document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart
return $c/Connection[position() = last()]
Query 21 Select the third connection's AtomicParts of each CompositePart.
In XQuery for $c in document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart,
   $cn in $c/Connection[position() = 3]
return $cn/AtomicPart
Query 22 Select the AtomicPart whose MyID is smaller than its sibling's and it occurs before that sibling.
In XQuery for $c in document("small31.xml")
          /ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
          /BaseAssembly/CompositePart/Connection,
   $a1 in $c/AtomicPart
return $c/AtomicPart[(. BEFORE $a1) AND (./@MyID .<. $a1/@MyID)]
Query 23 Select all Document after the Document with MyID = 25.
In XQuery FOR $doc in document("small31.xml")
LET $d   := $doc/ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
            /BaseAssembly/CompositePart/Document[@MyID = 25]
return
  <After_DOC>
    $doc/ComplexAssembly/ComplexAssembly/ComplexAssembly/ComplexAssembly
    /BaseAssembly/CompositePart/Document AFTER $d
  </After_DOC>