Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 2 min read

Support

Motivation

Sometimes you want to change a single label on a form without loading the form. For example, after you enter the country of an address you may want to ask for a "Province" in Canada or a "State" in the US. This can also be used to load alternative labels based on the user's language.

Screen Image

Examples of XForms Dynamic Labels (Image: The original uploader was Dmccreary at English Wikibooks ., CC BY-SA 3.0)

XForms Example of Dynamic Labels

Sample Program

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms">
<head>
<title>Example of Dynamic Label Lookup</title>

<style type="text/css">
@namespace xf url("http://www.w3.org/2002/xforms");
body {font-family: Ariel, Helvetica, san-serif}
/* Input Control labels are right-aligned in a 200px wide column that floats to the left. */

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

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

/* Put a black border and background color around all specified XForms groups and gives them both margin and padding */
xf|group {border: solid black 1px; margin:15px 5px; padding:5px; background-color:Lavender;}
.group-label {text-align:left;font-weight:bold;font-size:12pt;}
</style>
<xf:model>
<xf:instance id="data" xmlns="">
<Data>
<PersonName>
<PersonGivenName>John</PersonGivenName>
<PersonMiddleName>Paul</PersonMiddleName>
<PersonSurName>Doe</PersonSurName>
</PersonName>
<Address>
<LocationStreetFull>123 Main St.</LocationStreetFull>
<LocationCityName>Anytown</LocationCityName>
<LocationStateName>MN</LocationStateName>
<LocationPostalID>55123</LocationPostalID>
</Address>
</Data>
</xf:instance>
<xf:instance id="selected-country" xmlns="">
<data>
<country-id>usa</country-id>
</data>
</xf:instance>
<xf:bind id="country" nodeset="instance('selected-country')/country-id" />
<xf:instance id="label-lookup" xmlns="">
<label-lookup>
<country>
<country-id>usa</country-id>
<data-element name="record" label="Record" />
<data-element name="PersonName" label="Person Name" />
<data-element name="PersonGivenName" label="First (Given) Name" />
<data-element name="PersonSurName" label="Last (Family) Name" />
<data-element name="PersonMiddleName" label="Middle Name" />
<data-element name="Address" label="Address" />
<data-element name="LocationStreetFull" label="Street" />
<data-element name="LocationCityName" label="City" />
<data-element name="LocationStateName" label="State" />
<data-element name="LocationCountryName" label="Country" />
<data-element name="LocationPostalID" label="Zip" />
</country>
<country>
<country-id>canada</country-id>
<data-element name="record" label="Record" />
<data-element name="PersonName" label="Person Name" />
<data-element name="PersonGivenName" label="First Name" />
<data-element name="PersonSurName" label="Last Name" />
<data-element name="PersonMiddleName" label="Middle Name" />
<data-element name="Address" label="Address" />
<data-element name="LocationStreetFull" label="Street" />
<data-element name="LocationCityName" label="City" />
<data-element name="LocationStateName" label="Province" />
<data-element name="LocationCountryName" label="Country" />
<data-element name="LocationPostalID" label="Postal Code" />
</country>
<country>
<country-id>uk</country-id>
<data-element name="record" label="Record" />
<data-element name="PersonName" label="Person Name" />
<data-element name="PersonGivenName" label="First Name" />
<data-element name="PersonSurName" label="Sur Name" />
<data-element name="PersonMiddleName" label="Middle Name" />
<data-element name="Address" label="Address" />
<data-element name="LocationStreetFull" label="Street" />
<data-element name="LocationCityName" label="City" />
<data-element name="LocationStateName" label="County" />
<data-element name="LocationCountryName" label="Country" />
<data-element name="LocationPostalID" label="Postal Code" />
</country>
</label-lookup>
</xf:instance>
</xf:model>
</head>
<body>
<p>Demonstration of Dynamic Labels</p>
<xf:select1 bind="country" incremental="true">
<xf:label>Country: </xf:label>
<xf:item>
<xf:label>USA</xf:label>
<xf:value>usa</xf:value>
</xf:item>
<xf:item>
<xf:label>Canada</xf:label>
<xf:value>canada</xf:value>
</xf:item>
<xf:item>
<xf:label>UK</xf:label>
<xf:value>uk</xf:value>
</xf:item>
</xf:select1>
<xf:repeat nodeset="*">
<xf:group ref=".">

<xf:label class="group-label">
<xf:output value="concat(instance('label-lookup')/country[country-id=instance('selected-country')/country-id]/data-element[string(@name)=string(name(current()))]/@label,':')" />
</xf:label>
<xf:repeat nodeset="*">
<xf:input ref=".">
<xf:label>
<xf:output value="concat(instance('label-lookup')/country[country-id=instance('selected-country')/country-id]/data-element[string(@name)=string(name(current()))]/@label,':')" />
</xf:label>
</xf:input>
</xf:repeat>
</xf:group>
</xf:repeat>

</body>
</html>

Discussion

This example shows a nice clean elegant way to iterate over elements in a instance for groups and elements. The structure is:

nodeset="*">
ref=".">
nodeset="*">
ref=".">

This examples shows how the output tag can be buried inside the input labels.

ref="">

value=""/>

Credits

This example was originally inspired by Kurt Cagle.

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.

Get each new page of

XForms

Following means one email when a page is added, nothing when a page changes.

Following means one email when a page is added, nothing when a page changes.

Comments

Spinner Logo

Next in XForms

CC BY-SA 4.0
Use the **xforms-value-changed** event to trigger a submission that puts possible values in a list. When the user select a value from the suggestion list it will replace the text value in the field. We will also add a bind rule so that the selection view will only be visible if there is at least one
CC BY-SA 4.0
The second select1 uses an **xf:itemset** to only select the relevant items from the second instance. In effect we are adding a "where" clause to the list of items selected in the second list. The presented form shows an example where the user can select a season (Spring, Summer,...) and in a second
CC BY-SA 4.0
The people will automatically be loaded into the form when the form loads. We will create triggers that will incrementally load the other portions of the model when they are needed. Each trigger will use a submission event that does a separate HTTP get to incrementally load data into a separate inst
CC BY-SA 4.0
விபரங்களை சரிப்பார்க்கவும்" sbutton="சேமி" cbutton="மீளமை" e1msg="கடவுச்சொல் தவறு!மீண்டும் உறுதிப்படுத்துக"> விண்ணப்பதாரர் பெயர் பிறந்த தேதி முகவரி விருப்பப்படும் நுழைவு பெயர் கைத் தொலைபேசி எண் தொலைபேசி எண் மின்னஞ்சல் முகவரி கடவுச்சொல் கடவுச்சொல் உறுதிப் படுத்து name="हिन्दी" rname="पंजीकरण फार्म" s
CC BY-SA 4.0
When you press the "Enter" key the search will automatically be executed. We will also discuss a way to have a single form display both simple search and complex search functions. ## Method We will first create a simple search input field and give it an id of "search-field". We will also add an acti
CC BY-SA 4.0
XForms, Advanced Search