跳轉到內容

XRX/選擇列表生成器

來自華夏公益教科書,開放的書籍,開放的世界
< XRX

您希望將所有程式碼表儲存在中心位置,並讓每個表單動態地從這些程式碼表生成選擇列表。

我們將建立一個非常簡單的XML檔案格式來儲存所有程式碼。我們將每個檔案儲存一個程式碼表,然後編寫一個簡單的XQuery,它遍歷所有這些檔案以構建一個示例選擇列表。此選擇列表將在模型中建立一個例項以儲存每個選擇列表值。它還將使用列表中的第一個值填充該例項。

在我們的示例中,我們將假設一個XRX檔案命名標準,例如/db/apps/app-name/code-tables/my-code-table.xml,其中每個應用程式包含其自己的程式碼表,以最大限度地提高應用程式在系統之間的可移植性。共享程式碼表的應用程式可以將這些程式碼表儲存在以下位置:/db/shared/code-tables/my-code-table.xml

示例螢幕影像

[編輯 | 編輯原始碼]
選擇列表生成器的輸出

XML檔案格式

[編輯 | 編輯原始碼]
<code-table>
    <code-table-name>PublishStatusCode</code-table-name>
    <definition>A way to classify the publishing workflow of an item.</definition>
    <items>
        <item>
            <label>Draft</label>
            <value>draft</value>
        </item>
        <item>
            <label>Under Review</label>
            <value>under-review</value>
        </item>
        <item>
            <label>Published</label>
            <value>published</value>
        </item>
    </items>
</code-table>

示例XQuery

[編輯 | 編輯原始碼]

此查詢遍歷XRX應用程式code-tables集合中的所有檔案,並查詢以code-table為根元素的XML檔案。然後它建立一個包含所有選擇列表的報告,這些選擇列表位於一個正在執行的XForms應用程式中。

xquery version "1.0";
import module namespace style ='http://code.google.com/p/xrx/style' at '/db/xrx/modules/style.xqm';
declare namespace xhtml="http://www.w3.org/1999/xhtml";

(: XQuery to construct an XForm for either a new item or update item :)
declare option exist:serialize "method=xhtml media-type=application/xhtml+xml indent=yes"; 

let $app-collection := style:app-base-uri()
let $code-table-collection := concat($app-collection, '/code-tables')
let $code-tables := collection($code-table-collection)/code-table

return
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:xf="http://www.w3.org/2002/xforms" 
xmlns:ev="http://www.w3.org/2001/xml-events">
   <head>
      <title>XForms Selection List Tester</title>
      {style:import-css()}
      <link type="text/css" rel="stylesheet" href="block-form.css"/>
      <xf:model>
         <!-- This instance holds the value of each code -->
         <xf:instance xmlns="" id="save-data" src="">
            <data>
               {for $code-table in $code-tables
                 return
                    element {$code-table/code-table-name/text()} {$code-table/items/item[1]/value/text()}}
            </data>
         </xf:instance>
      </xf:model>
   </head>
   <body>
      {style:header()}
      {style:breadcrumb()}
      <h1>Sample with a Model in the XForm</h1>
      {for $code-table in $code-tables
         let $code-table-name := $code-table/*:code-table-name/text()
      return
         <xf:select1 ref="{$code-table-name}">
            <xf:label>{$code-table-name}: </xf:label>
            {for $item in $code-table/*:items/*:item
              return
                 <xf:item>
                    <xf:label>{$item/*:label/text()}</xf:label>
                    <xf:value>{$item/*:value/text()}</xf:value>
                 </xf:item>}
            <xf:hint>{$code-table/*:definition/text()}</xf:hint>
         </xf:select1>
      }
      {style:footer()}
   </body>
</html>

請注意,此XQuery使用元素構造器在例項中建立元素名稱。它還使用*:name表示法將來自空名稱空間的資料直接放入XForms名稱空間。

此檔案還將元素的定義直接放入XForms應用程式的命中資訊中。在某些XForms應用程式(如Firefox)中,提示資訊會以浮動短暫模式顯示在表單的左側邊距中。

示例CSS檔案

[編輯 | 編輯原始碼]

這是可以用來使每個選擇出現在單獨一行上的block-form.css檔案

@namespace xf url("http://www.w3.org/2002/xforms");
body {font-family: Arial, Helvetica; sans-serif;}

/* This line ensures all the separate input controls appear on their own lines */
xf|output, xf|input, xf|select, xf|select1, xf|textarea {display:block; margin:5px 0;}

/* Makes the labels right aligned in a column that floats to the left of the input controls. */
xf|select > xf|label,
xf|select1 > xf|label
{text-align:right; padding-right:10px; width:250px; float:left;}

上一步:檔案鎖定 下一步:術語編輯器

華夏公益教科書