Hub Nexus
更新

作者暂无作者认领

发现可以改进的地方?提出一个改动吧。

赞助

Motivation

You have a list of items that you want to display inside of a repeat loop. You want some items to have different styling based on the content of items.

Method

We will use the XForms group element to conditionally display an item inside of the repeat statement.

Sample XML Fragement

Assume your form has the following data in an instance:

true
Element2 Value

false
Element2 Value

true
Element2 Value

Conditional Display of Items

The following text will only output element2 when element1 has a value of true.

nodeset="//my-node">
ref=".[element1='true']">
ref="element2"/>

The syntax .[a='b'] says that from the current node if the element a equals the value b then output the elements within the group.

Conditional Formatting of Items

This same strategy can be used to enclose output in different div or span elements.

nodeset="my-node">
ref=".[element1='true']">
ref="element2" class="strong"/>

ref=".[element1='false']">
ref="element2"/>

In the above example all nodes with element1='true' will have a class="strong" in the output and any element with element1='false' will not have the attribute in the output.

Sample XForms Appliction

Load XForms Application

Source Code

In this example we need to display a list of synonyms for a specific term. The Synonyms are all listed and a preferred term is displayed with a bold font.

<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xf="http://www.w3.org/2002/xforms">;
<head>
<title>Conditional Display</title>
<style type="text/css">
@namespace xf url("http://www.w3.org/2002/xforms");
body {font-family:Helvetica, sans-serif}
.strong {font-weight:bold;}
/* this puts everything under the repeat element into a single line */
xf|repeat * {display:inline;}
</style>
<xf:model>
<xf:instance xmlns="" id="current-synset">
<data>
<synset-id>3</synset-id>
<synonym>
<preferred>false</preferred>
<syn-name>Boolean-Value</syn-name>
</synonym>
<synonym>
<preferred>false</preferred>
<syn-name>Conditional-Value</syn-name>
</synonym>
<synonym>
<preferred>false</preferred>
<syn-name>Flag</syn-name>
</synonym>
<synonym>
<preferred>true</preferred>
<syn-name>Indicator</syn-name>
</synonym>
<synonym>
<preferred>false</preferred>
<syn-name>Yes/No-Value</syn-name>
</synonym>
</data>
</xf:instance>
</xf:model>
</head>
<body>
<h3>Synonym Set</h3>
<span>(preferred term is bold)</span><br/>
<xf:label>Synonyms: </xf:label>
<xf:repeat nodeset="instance('current-synset')/synonym" id="repeat">
<xf:group ref=".[preferred='true']">
<xf:output ref="syn-name" class="strong"/>
</xf:group>
<xf:group ref=".[preferred='false']">
<xf:output ref="syn-name"/>
</xf:group>
</xf:repeat>
</body>
</html>

Discussion

XForms 1.1 also includes the if attribute. Some XForms can conditionally use this for display of elements.

Where this page came from

This page was imported from Wikibooks. From “XForms” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

收录于这些专栏XForms

语言English

许可协议: CC BY-SA 4.0 · 改编自 en.wikibooks.org

1

0

0

0

Spinner Logo

留言

Spinner Logo
版本: 2CC0 1.0 — public domain
The runaway star that left the Tarantula Nebula
版本: 2CC0 1.0 — public domain
The Blackwell School, where segregation had no law behind it
版本: 2CC0 1.0 — public domain
The Eagle Nebula, seen in the infrared
版本: 2CC0 1.0 — public domain
The house where the Equal Rights Amendment was written
版本: 2CC0 1.0 — public domain
The Aleutians, the forgotten front of the Second World War
版本: 2CC0 1.0 — public domain
The Cosmic Cliffs are not cliffs