Mozilla also supports the use of XML datasources. An XML document will be loaded and XPath expressions may be used to take portions of the XML document and generate content from these. When an XML source is desired, specify a
querytype attribute on the root node of the template to the value
xml. This indicates to the template builder that an XML source is being used as that the queries use syntax that is specific to XML.
datasources attribute should be set to the URL of an XML document, either a local file or a remote web site. The
ref attribute isn't currently used for XML sources, as the root of the document is always the starting point for XML queries; you should just set the
ref attribute to a dummy value, for example '*' which is typically used.
<listbox datasources="people.xml" ref="*" querytype="xml"> <template> ... </template> <listbox>
The URL in the
datasources attribute may be a relative URL or an absolute URL.
For this and the following examples, we are going to use the following XML document containing a list of people:
<people> <person name="Napoleon Bonaparte" gender="male"/> <person name="Cleopatra" gender="female"/> <person name="Julius Caesar" gender="male"/> <person name="Ferdinand Magellan" gender="male"/> <person name="Laura Secord" gender="female"/> </people>
XML Query Syntax
<listbox datasources="people.xml" ref="*" querytype="xml"> <template> <query expr="person"/> <action> <listitem uri="?" label="?name"/> </action> </template> </listbox>
expr attribute is a very simple XPath expression which simply retrieves the
person elements from within the datasource. As there are five in the XML document, five results will be generated. The
action element defines the content to generate for each result. The content of the
action element will be generated and repeated for each result, which in this case, is a
The child of the
action element has to have a
uri attribute, although it does not have to be a direct child. The element with the
uri attribute and its children will be copied once for each result. Any elements between the
action element and this element are only copied once. This is useful when recursive generation or multiple rules are used, and we'll see examples of why this is useful later. For an RDF query, the
uri attribute specifies the member variable; for an XML query the member variable is always '?' so the value of the
uri attribute for XML templates should always be the single question mark character as in this example.
The label attribute has been set to a special value
?name. When a question mark character followed by a string appears in an attribute value, the label value is not directly, but instead the value of an attribute for the source XML is used. For each node generated by the XPath expression used in the query, the attribute on the generated XML node is taken and substituted for the question mark and following string.
In the above example, the use of the
?name value, causes the name attribute on each result node to be used as the value of the label. You can see the name attributes in the original XML data above. This substitution occurs for any attribute within the action body, so you can also set other attributes with this technique.
<vbox datasources="people.xml" ref="*" querytype="xml"> <template> <query expr="person"/> <action> <hbox uri="?" align="center"> <button label="?name"/> <label value="?gender"/> </hbox> </action> </template> </vbox>
Note how two attributes are substituted here, the label on the button and the value on the label.
The query does not have to be as simple as the one used here. Any XPath expression may be used. For instance, the following query returns only those results for female people.
As long as the expression returns one or more nodes, it can be used in an query expression.
Using Inline XML
It's also possible to embed the XML data directly within the XUL document if desired. Instead of using a URL for the
datasources attribute, you can use an anchor reference to refer to a node within the same document. The effect is the same, but the data isn't stored in a separate file.
<people id="famouspeople" xmlns=""> <person name="Napoleon Bonaparte" gender="male"/> <person name="Cleopatra" gender="female"/> <person name="Julius Caesar" gender="male"/> <person name="Ferdinand Magellan" gender="male"/> <person name="Laura Secord" gender="female"/> </people> <listbox datasources="#famouspeople" ref="*" querytype="xml"> <template> <query expr="person"/> <action> <listitem uri="?" label="?name"/> </action> </template> </listbox>
Here, an anchor reference, starting with a number sign (#) is used for the
datasources attribute. It refers to an element with that id (without the number sign) and uses that node as the source XML node instead of a separate file. Otherwise the data included inline and the template are the same. Note the only change is that the addition of the
xmlns attribute on the data; this is used to clear the namespace used for the elements in the data, otherwise they become XUL elements. You can also specify a namespace. If so, you will need to ensure that prefixes are defined within the template as well, so that they can be used in the XPath expressions. The namespace prefixes that may be used in an XPath expression are those that are defined on the
assign element where the expression is used.
Be aware that when XML data is included inline as in this example, the content may be displayed and may affect the layout of other parts of the window. You may wish to hide the inline XML by placing it inside a hidden box.