XUL:Templates with XML

From MozillaWiki
Jump to: navigation, search

Templates will allow generating content from XML sources as well as RDF. To use an XML source set the querytype attribute on the root element to 'xml', as in the following example:

 <vbox id="list" datasources="xml-url" ref="." querytype="xml">

The datasources attribute should be the URL of an XML file. It may also be a fragment reference such as '#data' to refer to the node in the same XUL document with the id 'data'.

The ref attribute doesn't do anything currently, but may be expanded upon in the future to allow one to specify the starting point in the xml tree. Currently, just set the value to '.'.

An XML template by default simply generates one result for each child of the root node within the XML source document. For example:

 <vbox datasources="xml-url" ref="." querytype="xml" flex="1">
   <template>
     <button uri="?" label="?name"/>
   </template>
 </vbox>

If the xml document was that below, three buttons would be generated:

 <people>
   <person name="Neil"/>
   <person name="Samantha"/>
   <person name="Chris"/>
 </people>

The variable '?name' refers to an attribute set on the XML node. In this case '?name' retrieves the name attribute from the <person> nodes and sets the button's label to that value.

In many cases, you will want to display the XML data filtered in some way. To do this, an XPath expression may be used to generate the results to display. The expr attribute may be set to this expression. In this example, the expression filters out a node such that only the second and third person nodes are used to generate content.

 <vbox datasources="xml-url" ref="." querytype="xml" flex="1">
   <template expr="person[position()>1]">
     <button uri="?" label="?name"/>
   </template>
 </vbox>

Multiple rules and conditions may also be used as with RDF templates.

 <vbox datasources="xml-url" ref="." querytype="xml" flex="1">
   <template>
     <query expr="person[position()>1]"/>
     <rule>
       <where subject="?name" rel="equals" value="Samantha"/>
       <action>
         <button uri="?" label="?name is a female."/>
       </action>
     </rule>
     <rule>
       <action>
         <button uri="?" label="?name is a male."/>
       </action>
     </rule>
   </template>
 </vbox>

This example shows the full syntax for XML templates, where the expr attribute is used on the <query> element.

The assign element allows the assignment of additional values to variables, in case you want the values to be calculated. If a variable is referred to that does not have an assignment, it is assumed to refer to the name of an attribute. This latter case is used in the earlier examples.

 <query expr="person">
   <assign var="?number" expr="position()"/>
 </query>
 <action>
   <hbox uri="?">
     <label value="?number"/>
     <button label="?name"/>
   </hbox>
 </action>

In this example, the ?number variable is bound to the result of the 'position()' XPath expression. However, the ?name variable is not bound to a value, so it is used to retrieve the name attribute instead. Note that the context for the expression in an assign element is each result, whereas the context for the expr expression on the query is the root of the XML source document.

This technique works similar to how the <binding> element is used, however the <assign> element requires a value to be set in order to generate a result. In the above example, the expression 'position()' will always return a value, but if it didn't, no output would be generated for that result. As with RDF templates, the <binding> element may be used for optional value.

XML templates also support recursion in the same way as RDF templates. The result of each iteration will be used as the context for the next iteration.