logo
Query Context < TMQL Introduction < < Home 

PrevUpNext

Query Context

You may have wondered how the query processor knows which map to query. One way is that the map itself is hard-coded into the query expression itself:

select $album
from file:mymap.xtm ~ >>
where
   ...
Assuming that we have stored an XTM topic map in the file mymap.xtm, the path expression file:mymap.xtm ~ >> makes several steps: First, we have implicitely indicated a subject, namely the map stored in the file. In a TM sense, we also temporarily create a topic for the map. Then we zoom into the map, i.e. ask the processor to parse the file, interpret the content as a collection of topics and assocation; and, finally, we switch our focus of attention to that map, so that the rest of the query expression is interpreted in the context of this map.

While hard-coding such information into a query may make sense in some cases (query processors may want to analyse the map before query time in order to optimize), generally one would like to pass this map information at query time to the TMQL processor. This is done via a mechanism we already have seen: variable bindings where a value (string, integer, but also whole maps) are bound to variables. Applications will build these binding sets and pass them somehow to the processor.

For our example, our application could have already loaded the map into memory, possibly having done some modifications already to it. When that map is bound to, say, %m, then inside the query we can refer to this variable, blindly trusting that it contains a map:

select $album
from %m
where
   ...
As this scheme―passing in a map to be used in a FROM clause― is expected to be used often, it can be abridged. First, we can make use of the special variable %_; whatever map is assigned to it, that map will be the one queried. Which implies that the clause from %_ is default anyway and can be omitted.

The query context can contain other variables as well. Slightly paraphrasing an earlier query, we could parameterize it with a URL which we additionally pass in bound to $url:

select $album
where
   is-produced-by (production: $album, producer: $url ~)


PrevUpNext