How are XForms used?

934 views Asked by At

I recently stumbled over XForms (W3C 1.1, ODK XForms) and I struggle to see how they are used (if they are still used).

The SO tag page says:

XForms is an XML format that specifies a data processing model and user interface for the XML data. Eg. web forms.

Now I looked at the W3C examples and I don't see any kind of user interface. It's just XML that is displayed there.

What I do

When I want to have a form in the web, then I have to sides to work on: The front-end and the back-end. The front-end is either writing directly using <form> / <input> /` HTML elements and CSS for styling or generating those with packages like flask-wtf.

The back-end listens to GET / POST HTTP-requests to receive the form.

Question

I have a lot of beginner questions. My main question is How are XForms used?. A minimal Python example would be of most value to me. Maybe something simple like a registration form: A username field, a password field and a password confirmation field could show this.

I would create HTML like this:

<form action="" method="POST">
    <label for="username">Username</label>
    <input type="text" name="username" id="username" />

    <label for="pw">Password</label>
    <input type="password" name="pw" id="pw" />

    <label for="pw2">Confirmation</label>
    <input type="password" name="pw2" id="pw2" />
    <input type="submit" />
</form>

And with Flask the backend could look like this:

# Third party modules
from flask import redirect, url_for, render_template
from flask_login import current_user
from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField, SubmitField

# First party modules
from my_db_models import User


class LoginForm(FlaskForm):
    email = StringField("Email")
    password = PasswordField("Password")
    submit = SubmitField("Log In")


@auth.route("/login", methods=["GET", "POST"])
def login():
    if current_user.is_authenticated:
        return redirect(url_for("main.index"))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash(INVALID_EMAIL_OR_PASSWORD, "error")
            return redirect(url_for("auth.login"))
        login_user(user, remember=form.remember_me.data)
        return redirect(url_for("index"))
    return render_template("login.html", form=form)

How would that look with XForms? Where exactly is a typical use-case for XForms? What are they comparable to? Are XForms only used in Java? (I have seen Python packages, but I have never heard of them)

4

There are 4 answers

0
Angel Salazar On

In my case, I had been using the ODK Ecosystem to provide Marketing Research solutions for Companies. It can handle complex logic, multimedia, GPS, between other. In mobile devices: Android with ODK Collect and iOS with GIC Collect, using the same XML file. And for the backend, you can use your servers with ODK Central or Google Drive, ONA, or Kobotoolbox. You can reduce your development time and cost significantly. Disclosure I am part of the development group of GIC Collect.

7
Alain Couthures On

XForms has been specified to be used within an host language such as XHTML or SVG. ODK is not a fully compliant implementation. My own XForms implementation (XSLTForms) is based on XSLT 1.0 (natively available in browsers) to generate HTML+Javascript runtime.

0
ebruchez On

I provide some answers to a series of your questions that appear above.

Where exactly is a typical use-case for XForms?

Anything that looks like a form.

which problem does it solve?

XForms provides a higher-level, declarative language to describe and validate forms.

What are they comparable to?

HTML forms as a lower bound, and frameworks like React and Vue and Angular. But in reality, there is nothing that does exactly what XForms does, as XForms has a strong focus on forms, while many of those frameworks don't. See also this answer I wrote a while back.

Are XForms only used in Java?

(Note that I prefer the singular for "XForms", as in "the XForms specification". There was an old article that preferred the plural back then and was popular at a time, but that doesn't make the plural correct in my opinion.)

No, as discussed in the comments, an XForms processor can be implemented in JavaScript or anything you want. Historically there have been browser plugins, client-side implementations in JavaScript, and mixed client-server implementations.

How are XForms used?

It depends on the processor implementation. You write XForms tags and attributes within your host language, like HTML. Then you serve that document to the browser while linking to a processor's JavaScript library or, for implementations that have a server component, you feed that to your server-side library.

Does that mean that XForms is an alternative to client-side form data evaluation with JavaScript? Is it only that or more?

It's more, and it may or may not have anything to client-side or server-side or JavaScript, although in original intent and in practice the idea is that it's a substitute and enhancement for built-in browser form processing.

I couldn't find a plugin for either Firefox or Chrome. Do you happen to know one?

No, plugins are a thing of the past.

0
Martijn van de Rijdt On

The ODK ecosystem uses a subset of XForms 1.1 and is widely used as it allows complex logic to be described in the form definition, and there are many compatible tools (form builders, data aggregators, data collection clients) available (e.g. KoBoToolbox, Ona, Survey123, ODK). As Alain mentioned, it is not XForms compliant and likely never will support the full spec, but they've been moving a little closer in the last few years. Some weird deviations have been in place for historic reasons (and will hopefully disappear eventually).

However, most ODK users have no idea their tools are powered by XForms, as this is abstracted away by form builders such as pyxform (see XLSForm.org), KoBo's form builder, and ODK Build.

There is one javascript implementation of a data collection client in the ODK ecosystem which is called Enketo (see enketo.org).