Help:Displaying information 1.5.1 - 1.6.2
Displaying information en 1.5.1 1.6.2
Queries in Semantic MediaWiki return lists of pages. The default result simply lists the page titles. Additional information, such as a page's property values or categories, can be included into a query result by using additional printout statements.
There are different kinds of printout statements, but all of them can be recognized by the question mark ? that they start with in the query. The important difference between printout statements and query descriptions is that the former do not restrict the result set in any way, even if some printout has no values for a given page, an empty field will be printed, but the page is still part of the result.
Note, all the printout statements below are used within queries.
Printing property values
The most common form of printout statements are property printouts that display all the values assigned to a certain property. These are written simply as a question mark followed by the property name, e.g.
This prints all the values for the «population» property of the query results.
It is possible to change the label for a printout:
?population = Number of inhabitants
This still prints the values for the «population» property, but with the modified label.
As mentioned above, some properties may not be defined on some pages, e.g. if something does not have any population. Property conditions with wildcards can be used to ensure that all elements in a query result have some value for a given property, if this is desired.
There are two ways to print category information: either SMW prints all categories assigned to some page, or SMW checks for one particular category. The first case is achieved by the printout
where «Category» is the name of the Category namespace in the local language. This printout will show all categories that are directly used on a result page. The other option is to ask for one particular category, such as
The result then will contain a column «Actor» that contains X for all pages that directly belong to that category, and is empty otherwise. Again, one can change the label using equality:
?Category:Actor = A
will merely display an «A» as the header of the result column which might be more sensible given that the entries in that column are very short. It is also possible to change the way in which this kind of category queries are formatted, as described below.
The main result column
All queries by default display the main list of result pages in the first column. In some cases, it can be useful to move that to another position. This is not relevant for Special:Ask, but can be quite useful in inline queries. A special printout statment is available for this purpose:
This single question mark addresses the «unlabelled result column» that shows the main result list. As before, different labels can be assigned with the equality symbol, e.g.
? = Results
If you like to omit the main result column, you may use the parameter
mainlabel=- (see also here).
Many printout statements can be further customized by giving a printout format which can be given after a property name, separated by the symbol #. The available formats depend on the type of the printout and involved property.
Plain (unformatted) printouts
A general format that is supported by most types of printouts is the plain format (or empty format), available since SMW 1.4.3. Printouts with this format will avoid all forms of beautification or linking in their presentation, and return a plain value instead. This is particularly useful when results are further processed in templates or parser functions. To select the plain output format, a hyphen ("-") or simply nothing is used as a printout string, as in the following examples:
?population# - ?capital #
Both printouts select the plain format. Spaces do not matter and can be inserted to increase readability. For numerical properties like the population number, the plain format is a simple number string without commas to separate digits. For properties of type page, the plain output is simply the name of the page without any link.
Note that plain printouts for properties with units (see below) will still include the unit as part of the result (A value that is a (physical) dimension consists of a number multiplied by a quantity (unit), so leaving out the unit would change the value). For this reason, two special printout formats have been added (available since 1.5.1):
Given a value of "12,345.6 km":
?my value # - gives "12345.6 km" (as expected) ?my value # -n gives "12345.6" (just the number part of the value) ?my value # -u gives "km" (just the unit part of the value)
Formats for specific printout types
For properties that support units, queries can thus determine which unit should be used for the output. To print the height in cm, e.g., one would use the following:
this assumes that the property height is aware of the unit «cm».
The unit can be omitted all together, returning just a number (#-n), or queried on its own, returning just the unit (#-u):
For properties of type date, the output format "ISO" is available to obtain results in a technical format that conforms to the ISO 8601 standard. Other datatypes may have different printout formats. See the types documentation for details.
For printouts of the form ?Category:Actor, the display format can be used to modify what SMW will display for cases where a page is (or is not) in the category. The following is an example:
?Category:Actor#an actor, not an actor
This will show the text «an actor» for all pages that are actors, and the text «not an actor» otherwise. This can, for example, also be used in combination with small images to display icons for certain categories.