Archive:Inline queries 0.7


This page contains outdated information and is thus OBSOLETE!
The revision id is not annotated properly in the master page!
The template {{Docinfo}} may not be embedded in: Inline queries
This documentation page applies to all SMW versions from 0.7 to 1.0RC2.

Inline queries en 0.7 1.0RC2

Semantic MediaWiki includes a simple query language for semantic search, so that users can directly request certain information from the wiki. Readers who do not wish to learn the query syntax can still profit from this feature: inline queries dynamically include query results into pages. So queries once formulated by a few editors can then be consumed by many readers.

Inline queries are very similar to other semantic search features, and can also be restricted on a site in order to ensure sufficient performance. Since inline queries exploit the existing caching mechanisms of MediaWiki, most requests for a page with such dynamic contents can be served without any performance impact whatsoever.


Inline queries are written in the wiki-text of a page by writing text of the form

<ask ...>...</ask>

where the … describes the query. The part between the two ask-tags can be any query, as described in the help page to semantic search. For example, one can write

<ask>[[Category:Country]] [[located in::Africa]] [[Population:=*]]</ask>

to obtain a list of all African countries and show their population number. At the place where this query is inserted into the wiki-text, a simple list of all requested pages will be displayed within the page. Besides this, everything between <ask> and </ask> is ignored, so that the above example will not add the current page to the category Country. In a similar way, all kinds of queries can be embedded inline.

In general, an inline query is a request to find a number of pages that satisfy certain requirements. The query must answer three questions:

  1. Which pages are requested?
  2. What information should be displayed about those pages?
  3. How should the results be formated within the page?

The first two points are mostly part of the query, and have been explained at Semantic search. The third point is important to be able to smoothly include query results in many pages. In our above example, we might wish to get a bulleted list of countries with population printed in parentheses after each country. This can be achieved by choosing the format "ol":

<ask format="ol">
  [[located in::Africa]] 

This third step is rather independent from the other two, and an increasing number of output formats is provided for inline queries. In addition to "format," inline queries accept a number of further parameters, e.g. for sorting the results or for limiting the number of returned items.

Standard settings[edit]

A number of standard parameters are always available for customising the result of inline queries. They are:

Parameter Possible values Description
limit non-negative number maximal number of pages selected (in the case of a table: rows)
sort attribute name name of attribute to use for sorting queries; this attribute must occur in the query in a statement without "*"
order "ascending"/"asc", "descending"/"desc"/"reverse" defines how results should be ordered, only used if sort is used, "ascending" is the default
headers "show", "hide" shows or hides the labels/headers used in the output, "hide" is default
mainlabel plain text title of the first column (the one with the page titles in it)
link "none", "subject", "all" defines which article names in the result are hyperlinked, "subject" normally is the default
default plain text if, for any reason, no result is being created, this will be returned
intro plain text initial text that is prepended the output, if at least some results exist
debug "true" [only SMW≤0.6] gives an SQL statement for debugging instead of the query results
format a format name (see below) selected output format; some formats allow further parameters (see #Output formats)

Result limits and further results[edit]

The parameter limit can be used to restrict the maximum number of results that are returned. For example, the query

<ask limit="3">
  [[located in::Africa]] 

returns 3 countries in Africa. Even if no value for limit is given, there is a default limit that will be used. Depending on a site's settings, it might be possible to increase the number of displayed results by specifying a higher value for limit. However, there is usually a maximum limit that cannot be exceeded. Its value is specified by the site administrators based on performance considerations.

If not all results of a query have been displayed due to a restricted limit, there will often be a link to "further results" that is displayed below the query. The text of this link can be modified by setting the parameter searchlabel. If the value of searchlabel is "", then the link to further results will no be shown. Some output formats (see below) do never display the search link, or display it only if a searchlabel was specified.

An interesting application of limit and searchlabel is to display only a link to the results of a search, without showing any result inline. This is done by selecting a limit of "0". For instance, the query

<ask limit="0" searchlabel="Browse list of countries">[[Category:Country]]</ask>

displays a sole link entitled "<ask limit="0" searchlabel="Browse list of countries" default="Browse list of countries">[[Category:Country]]</ask>" if any country is found. Otherwise, nothing is shown.

Sorting results[edit]

By default, semantic search results are in page title order, ascending. The Special:Ask input form has additional input fields to specify a different sort column and ordering. In inline queries, you specify those sort column with the parameter sort, and the order with the parameter order; the value of order should be "ascending" or "descending" (or the short forms "asc" and "desc"). For example, the inline query

<ask sort="population" order="descending">
  [[located in::Africa]]

returns a list of countries ordered by population, largest first. (The actual search is for countries located in Africa that have a value for population, and it prints this population; see Semantic search.)

Configuring labels/table headers[edit]

Queries that return more than just the selected articles (e.g. the population in the above example), will use labels to describe the various output fields. In the standard tabular view, the labels are used as headers for columns. In other cases, labels might appear right before the output fields.

By default, the labels just display the name of the selected attribute or relation, or the text "Categories" if categories are displayed (using the statement [[Category:*]]). This can be changed by using the "|"-notation for alternative labels, as it is known for links in MediaWiki. For example, the query

 [[Area:=*km²|Size in km²]] 
 [[borders::*|Next to]]
 [[Category:*|Category memberships]]

returns a table with five columns: the selected articles (unlabeled), the population (labeled "Inhabitants"), the area in km² (labeled "Size in km²"), bordering countries (labeled "Next to"), and page categories (labeled "Category memberships"). Labels that refer to attributes or relations will additionally link to the respective pages in the Attribute: and Relation: namespaces.

<ask limit="1" searchlabel="..."> [[Category:Country]] [[Population:=*|Inhabitants]] [[Area:=*km²|Size in km²]] [[borders::*|next to]] [[Area:=+]] [[Population:=+]] [[Category:*|Category memberships]]</ask>

To change the label of the first column, one currently needs to use the parameter mainlabel which takes an arbitrary text. This might change in future versions to provide an additional simpler method with the |-syntax.

The display of labels can be controlled with the parameter headers which currently can take one of two values:

  • "show": display labels (default)
  • "hide": hide all labels or table headers

Introduction and default text[edit]

Certain texts should be shown or not shown depending on whether the query has results or not. For example, one may want the query to show an output of the following form:

Upcoming conferences: IJCAI2007, ICCS2007, …

where the list of conferences is generated by a suitable query. If the query (for whatever reason) would not return any results, the page would look as follows

Upcoming conferences:

which is not desirable. Two parameters exist to prevent this.

  • default: this parameter can be set to a default text that should be returned when no results are obtained. In the above example, one would probably write something like
Upcoming conferences: <ask default="none">...</ask>
so that, if no result is obtained, the article will display
Upcoming conferences: none
  • intro: this parameter specifies a text that should be prepended to the output of a query, but only if one or more results exist. In the above example, one could write
<ask intro="Upcoming conferences:_">...</ask>
so that, if no result is obtained, nothing will be printed at all. Note that we use "_" to encode the final space. This is needed for initial and final spaces in any parameter, since those are otherwise removed internally (not by SMW).

Both of the above solutions will show the intended output if results are found. It is also possible to combine both parameters if desired. Note that the above parameters only accept simple texts: neither wiki-markup nor HTML-elements are supported at the moment.

Also note that if the set of pages selected in a query is empty, no header row or blank line, not even any blank space, is produced. This can also be useful to "hide" queries that are not applicable. However, it is not recommended to insert great amounts of queries into every page, based on the assumption that this can do no harm since no output is generated. Indeed, answering queries requires much computational resources and should not be done without a purpose.

Output formats[edit]

The parameter format determines how the results of a query are displayed in the article. If it is omitted, all queries are displayed as tables (format table), unless there would be only one column, in which case the results are displayed as a comma-sepearated list (format list). The following formats are available:

Format Description Additional parameters
list Comma-separated list, with additional outputs shown in parentheses sep
ol Ordered list, with additional outputs shown in parentheses
ul Bulleted list, with additional outputs shown in parentheses
table Tabular output
broadtable Tabular output, where the table is as wide as the article.
timeline Use dates in the output to print a timeline. timlinelinestart, timlinelineend, timlinelinesize, timlinelinebands, timlinelineposition
eventline Unstable. Use dates in the output to print a timeline that shows more than two dates per article. timlinelinestart, timlinelineend, timlinelinesize, timlinelinebands, timlinelineposition
embedded Unstable. Embed selected articles. titleformat
template Unstable. Print results by passing result fields as parameters to a given template. template (mandatory)
count Just the number of results, instead of the results themselves
debug Debugging information for analysing problems in query answering.

Using templates[edit]

In an inline query, when format=template or format=list, wiki templates may be used to format the output of the query, using the following syntax (SMW 0.7):

<ask format=template template=templatename> ... </ask> or
<ask format=list template=templatename> ... </ask>.

The template should use numbered parameters; they refer to the results that would be produced by the inline query for each selected page (including that page itself if that would normally be produced) in the same order. Thus, if the selected page itself would normally be produced, this selected page is referred to as {{{1}}}, and the other results are subsequently referred to as {{{2}}}, {{{3}}}, .... See also below.

The template feature allows greater flexibility in the output of the query, including:

  • Changing the order in which output is displayed, or omitting or duplicating output;
  • Displaying images depending on query results;
  • Creating links for attributes;
  • Using CSS styles to vary font-size, alignment, background-color, etc. by column in tables.


  • When output is limited, the template format will not be carried forward to the continuation link.
  • Parser functions applied to parameters (e.g. for computations and branching) do not work.
  • If the selected page itself would normally be produced, this selected page is referred to as {{{1}}}, and the other results are subsequently referred to as {{{2}}}, {{{3}}}, .... If the selected page itself is normally not produced, {{{1}}} refers to the first result about the page. Thus typically different format templates are needed for both cases. To avoid that, when enumerating pages in the query (or specifying a particular one), use parameter "mainlabel" in the query to assign {{{1}}} to the selected page, so that {{{2}}} etc. refer to the same results as when pages are not enumerated in the query.


<ask limit="3" format=template template=query_output_demo> [[Category:Country]]
[[Population:=*|Inhabitants]] [[Area:=*km²|Size in km²]] 
[[borders::*|next to]]  
[[Area:=+]] [[Population:=+]]  [[Category:*|Category memberships]]</ask>


<ask limit="3" format=template template=query_output_demo> [[Category:Country]] [[Population:=*|Inhabitants]] [[Area:=*km²|Size in km²]] [[borders::*|next to]] [[Area:=+]] [[Population:=+]] [[Category:*|Category memberships]]</ask>

<ask limit="3" format=template template=query_output_demo mainlabel=country> [[Angola]] 
[[Population:=*|Inhabitants]] [[Area:=*km²|Size in km²]]
[[borders::*|next to]]  [[Area:=+]] [[Population:=+]]
[[Category:*|Category memberships]]</ask>


<ask limit="3" format=template template=query_output_demo mainlabel=country> [[Angola]] [[Population:=*|Inhabitants]] [[Area:=*km²|Size in km²]] [[borders::*|next to]] [[Area:=+]] [[Population:=+]] [[Category:*|Category memberships]]</ask>

Using a query to produce wikitext for annotations[edit]

When querying for a property, SMW does not (currently) automatically query for transitive or inverse properties. Pages must be ensured to have the properties in the query being made. For example, if there are many pages with the property located in:=California, if a query is made for its inverse [[California]][[Location of:=*]], SMW will not find these pages.

However, put an inline query on a scratch page that will generate a list of these properties that looks like wiki text, and then paste it into the edit box of the page. In this example,

[[location of:=<ask sep="| ]][[location of:=">[[located in:=California]]</ask>| ]]


<tt>[[location of:=<ask sep="| ]][[location of:=">[[located in:=California]]</ask>| ]]</tt>

and copy this generated wikitext to the edit box of the Demo:California page. (Remove the space between the first two brackets after pasting it in.) Also user pages are either removed, or the prefix "user:" is provided.