XForms/Repeat
外觀
< XForms
以下是一個簡單的示例,展示如何將重複資料元素列表輸出到螢幕上。這是使用 repeat 標籤和 nodeset 屬性來實現的。使用 nodeset 指定要在模型中開始列出的位置。在本例中,我們只是使用嵌入在頁面中的模型。
以下是該程式在 FireFox 瀏覽器下的輸出
請注意,所有名稱都會顯示,而不僅僅是第一個名稱。請注意,我們在 body 中混合了 HTML 標籤和 XForms 標籤。
<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>
這裡關鍵的語句是 repeat 語句
<xf:repeat nodeset="Person">
repeat 元素有一個名為 nodeset 的屬性。它告訴你在你的模型中獲取資料的位置。在本例中,我們將迭代所有 Person 記錄並輸出每個 Person 的 Name 和 Phone。
你也可以使用絕對路徑引用
<xf:repeat nodeset="/PhoneList/Person">
作為替代,你也可以使用例項引用
<xf:repeat nodeset="instance('phonebook')/Person">
請注意,每個 repeat 都會為每個 Person 記錄開始新的一行。這可以透過一個簡單的樣式錶轉換成整潔的表格佈局。你可以透過將第二個輸出與名為 "column2" 的類關聯,然後在樣式表中新增 column2 格式化規則來實現這一點。
