Enter Query


How do I use XQuery?


A helpful guide on XQuery provided by w3schools
Comprehensive examples provided by wikibooks

XQuery examples

Search for element

XPath can be used to return all elements of with specific language

//ns:div[@xml:lang="lat"]
  • // Selects nodes that match the selection no matter where they are.
  • ns: Is used to specify the HisTEI namespace.
  • div Selects all div elements.
  • @xml:lang Specifies the the requirement to have an attribute named xml:lang (language).
  • ="lat" Specifies language as Latin.

Specify date in query

XQuery expression to filter the results by criteria such as date

for $i in //ns:div[@xml:lang="lat"]
where $i/ancestor::ns:div//ns:date[@when="1501-10-20"]
return $i
  • for $i in //ns:div[@xml:lang="lat"] Starts a loop which iterates trough each result from the Search for element query.
  • where $i/ancestor::ns:div//ns:date[@when="1501-10-20"] the elements can be filtered by specifing criteria. In this instance the date being "1501-10-20"
  • return $i if a element matches the date 1501-10-20, it will shown as result on the result page.

Specify year ranges

Search through time period by using regular expressions

for $i in //ns:div[@xml:lang="lat"]
where $i/ancestor::ns:div//ns:date[@when >"1501-1-1"][@when <"1502-12-31"]
return $i
  • Similar to Specify a year in query where the results are filtered by only a specific date in this instance the date is filtered using date range from 1501-1-1 until 1501-12-31.

Count number of words in a particular language

XQuery expression which counts the number of words in Scots and Latin

for $lang in ("lat", "sco")
return (
  concat("Words annotated as ", $lang, ": ",
    count(for $j in (
        for $i in //ns:div[@xml:lang = $lang ][@xml:id >"ARO-5-0730-01"][@xml:id <"ARO-5-0730-03"]//*
        return if (not($i/@xml:lang) or $i/@xml:lang = $lang) then (
                      tokenize(  normalize-space(  string-join( $i/text(), ' ')  ), ' '  )
                   ) else ()
         )
         where matches($j, '[0-9a-zA-Z]')
         return $j
    )
    +
    count(for $j in (
        for $i in //ns:div[@xml:lang != $lang][@xml:id >"ARO-5-0730-01"][@xml:id <"ARO-5-0730-03"]//*
        return if ($i/@xml:lang = $lang) then (
                      tokenize(  normalize-space(  string-join( $i/text(), ' ')  ), ' '  )
                   ) else ()
         )
         where matches($j, '[0-9a-zA-Z]')
         return $j
    )
  )
)
  • This rather complex example which aim is to demonstrate how the number of words in Scots and Latin language within different pages could be counted.
  • for $lang in ("lat", "sco") We perform the same opertaion for both languages codes "lat" (Latin) and "sco" (Scots)
  • [@xml:id >"ARO-5-0730-01"][@xml:id <"ARO-5-0730-03"]Search for pages with xml:id between ARO-5-0730-01 and ARO-5-0730-03
  • Here we handle two different cases and calculate the sum from both results:
    • 1. Count all space separated words which are not annotated with language different than the rest of the Page. For instance, count all words on a page annotated as Scots which are not explicitly annotated with other language code.
    • 2. Count all space separated words which are annotated with language different than the rest of the Page. For instance, count all words annotated as Scots within pages annotated as Latin or any other language different than Scots.