Hub Nexus
更新

作者暂无作者认领

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

赞助

Motivation

One problem that keeps popping up when creating forms with XForms is the lack of sorting. This is an example for how it is possible to sort an instance using a XSLT stylesheet in Firefox.

Firefox XForms Example

The following model contains two instances. The first instance is the one we will sort and the second instance contains the XSLT stylesheet we will use to perform the sort.

id="default-instance">
xmlns="">

B-item
2001-05-03

A-item
2005-05-03

Z-item
2003-05-03

D-item
2002-05-03

id="stylesheet">

xmlns=""
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
match="/">

select="data/item">
type="string" select="name"/>

match="|@|text()">

select="|text()|@"/>

To perform the sort we will use the following JavaScript which will load the second instance into the Firefox stylesheet engine, then take the first instance make the transformation which will produce the sorted instance. Then the transformed instance is inserted into the model and a rebuild, recalculate, revalidate and refresh is performed.

function sort_instance(id) {
// We get the instance element
var instanceElement = document.getElementById(id);
if (instanceElement!=null) {

// XForms exposes the retrival of the instance document from the model element which should
// be the parent for the instance element.
var instance = instanceElement.parentNode.getInstanceDocument(id);

if (instance!=null) {
// Now creating the stylesheet, for this example the stylesheet document is also an instance
// but it can be loaded from many difference sources
var xslDom = instanceElement.parentNode.getInstanceDocument('stylesheet');

// create an XSLTProcessor and attach the stylesheet
var processor = new XSLTProcessor()
processor.importStylesheet(xslDom);

// now we do the sorting transformation
var resultDom = processor.transformToDocument(instance, instance);

// we then move the result info the instance dom
instance.removeChild(instance.documentElement);
instance.appendChild(resultDom.documentElement);

// and performs the updates for XForms
instanceElement.parentNode.rebuild();
instanceElement.parentNode.recalculate();
instanceElement.parentNode.revalidate();
instanceElement.parentNode.refresh();
}
}
}

Discussion

The idea of having the stylesheet in an instance makes it possible to change the behavior of the sorting by binding controls to it and change stuff like XPath expressions and other XSLT syntax.

References

This example is based upon the sample on [[1]http://landwehr.dk/blog/]

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