Bindings

We can add more triples to the previous example to show more information. For example, a description could be added to a photo. To do this all we need to do is add the necessary data to the RDF datasource and add another <triple> element to the template's statements.

<query>
  <content uri="?start"/>
  <member container="?start" child="?photo"/>
  <triple subject="?photo"
          predicate="http://purl.org/dc/elements/1.1/title"
          object="?title"/>
  <triple subject="?photo"
          predicate="http://purl.org/dc/elements/1.1/description"
          object="?description"/>
</query>

It works similarly to the previous triple. The ?photo variable is filled in with the known value and then the arc is looked up the datasource, filling in the value for the ?description variable. The ?description variable would then be used in the action body. Let assume though, that we are only going to add a description to one of the photos in the datasource; the other two photos will not have a description. This may mean the description is not known, or that it wasn't filled in by the user.

  <rdf:Description rdf:about="http://www.xulplanet.com/ndeakin/images/t/palace.jpg"
                   dc:title="Palace from Above"/>
                   dc:description="View from the top of the tower looking east of the Doges Palace"/>

If you try a full example based on this data, you will notice that only one result has been generated. This is because only one item has a description. Here is the data after the first <triple> has been evaluated.

(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/palace.jpg,
 ?title = 'Palace from Above')
(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/canal.jpg,
 ?title = 'Canal')
(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/obelisk.jpg,
 ?title = 'Obelisk')

The second triple will add a ?description for the first photo, adding a fourth variable-value pair to the existing data. For the second photo, the datasource doesn't have any matches for the description, so that potential result will be removed. The same happens for the third photo. In the end, only one photo remains.

(?start = http://www.xulplanet.com/rdf/myphotos,
 ?photo = http://www.xulplanet.com/ndeakin/images/t/palace.jpg,
 ?title = 'Palace from Above',
 ?description = 'View from the top of the tower looking east of the Doges Palace')

Thus, only one match exists, so only one set of content is generated. Sometimes, it would be useful to have triples that match conditionally; that is, a triple that doesn't cause a result to be removed as a possible match. This is done with a binding element. This tag doesn't go in the query block but instead inside a rule element. The syntax is as follows:

<template>
  <query>
    ...
  </query>
  <rule>
    <binding subject="?photo"
             predicate="http://purl.org/dc/elements/1.1/description"
             object="?description"/>
    <action>
      ...
    </action>
  </rule>
</template>

The description triple has been changed to a binding element, but the attributes are the same. This allows a match to occur even if the description is not specified. Note that when bindings are used, the bindings and the action must be enclosed inside a rule element. The template builder examines bindings after the query has generated results. It has already determined, in this example, that three matches have been found. The builder continues by filling in the values for any bindings. They are evaluated in a similar though simpler manner than the triples. The value of the ?photo variable is known for each match, the datasource is examined for the description, and the value of the ?description variable is filled in. The effect is three matches, only one of which will display a description. The photos that don't have a description will be treated as if the description were blank. That is, the ?description variable will be replaced with an empty string when analyzing the attributes in the action body.

Here is this example.

You can have more than one binding if you wish. The others will be evaluated in a similar way. For instance, the title triple could be moved to a binding such that it could be optional also.