Jump to content

XForms/Repeat

From Wikibooks, open books for an open world

Here is a simple example of how to get a list of repeating data elements out to your screen. This is done using the repeat tag and the nodeset attribute. You use the nodeset to specify where you want to begin your listing in the model. In this case we are just using a model embedded into the page.

Output

[edit | edit source]

Here is the output for this program under the FireFox browser:

Note that all of the names are displayed, not just the first name. Note how we are mixing both HTML tags and XForms tags together in the body.

Program

[edit | edit source]
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms">
   <head>
      <title>Phone List</title>
      <xf:model>
         <xf:instance xmlns="" id="phonebook">
            <PhoneList>
               <Person>
                  <Name>Peggy</Name>
                  <Phone>123</Phone>
               </Person>
               <Person>
                  <Name>Dan</Name>
                  <Phone>456</Phone>
               </Person>
               <Person>
                  <Name>John</Name>
                  <Phone>789</Phone>
               </Person>
               <Person>
                  <Name>Sue</Name>
                  <Phone>234</Phone>
               </Person>
            </PhoneList>
         </xf:instance>
      </xf:model>
   </head>
   <body>
      <fieldset>
         <legend>Company Phone List</legend>
         <p><b>Name, Phone</b>
         <xf:repeat nodeset="Person">
            <xf:output ref="Name"/>,
            <xf:output ref="Phone"/>
         </xf:repeat>
         </p>
      </fieldset>
   </body>
</html>

Discussion

[edit | edit source]

The key line here is the repeat statement:

<xf:repeat nodeset="Person">

The repeat element has an attribute named nodeset. This tells you where in your model to get the data. In this case we will be iterating over all Person records and outputting the Name and Phone for each Person.

You can also use an absolute path reference:

<xf:repeat nodeset="/PhoneList/Person">

As an alternative you can also use an instance reference:

<xf:repeat nodeset="instance('phonebook')/Person">

Note that each repeat starts a new line for each person record. This can be turned into a clean tabular layout using a simple style sheet. You can do this by having the second output be associated with a class called "column2" and then adding a column2 formatting rule to the style sheet.

Next Page: Conditional Styling | Previous Page: Bind to ranges
Home: XForms