Saving a notice in XML

Your user has filled in their notice on the screen. It is now time to write the data of the notice into an XML file, which you will submit for publication.

The first thing to get right is the XML. The XML file that you generate must comply with the eForms schema. This is not easy, but once you get it right, future changes of the schema will no longer cause you work. The rest of this page shows how to do it.

The data of the notice is still in a data structure of your application. You have designed this data structure, and it probably matches the needs of your user interface. This page calls this data structure the visual model (see Definitions).

Your aim is to take the data out of the visual model, and to change its form so that you can easily write it in XML. In other words, you want to move your data to another data structure, closer to the physical form that your data will take in the XML file. This page calls this target data structure the physical model (see Definitions).

So saving the data of your notice becomes a transformation from a visual model to a physical model.

You could try to do this transformation in one step, but it would probably be too complicated to get right, because the visual and physical models are too different. One matches how the user sees a notice on their screen. The other matches the physical structure of the notice in an XML file. Fields that appear together on the screen can be stored in completely different parts of the XML. So, to make things simpler, break this transformation into two steps:

  1. Transform the data of your notice from your visual model to an intermediate model. This model leaves the visual structure behind, but keeps all the information needed to build the physical model.

  2. Transform your data from this intermediate model to the physical model.

This page calls this intermediate model the conceptual model.

Definitions

Visual model

The data model of the notice data while the user edits the notice. This data model contains visual items, such as sections of the notice, groups of controls and input controls, with the values that the user entered. It is called a visual model because its structure follows the structure of the notice on the screen of the user.

Conceptual model

A data model that follows the way the eForms SDK sees a notice. This data model contains nodes and fields, and follows the logical structure of a notice as the eForms SDK defines it (in fields/fields.json).

Physical model

A data model that follows the physical structure that the notice data will have in the XML file. This model contains XML elements and XML attributes, and follows the structure of the notice that the eForms schema defines.

Items of the conceptual model

node

A node is a structural item of the conceptual model. Every node, except the root node, has a parent node. Every node has an XPath relative to the absolute XPath of its parent node. The relative XPath of a node can contain more than one step, each of which stands for an XML element. A forward slash / always separates XPath steps. A node can contain child nodes and fields. A node can be repeatable or not.

Repeatable node

A repeatable node can repeat inside its parent node. When a node is repeatable, the XML element that repeats in the XML document is the first XML element (the first step) of its relative XPath. A repeatable node can have several other repeatable nodes in its node-path.

node-path

The node-path of a node is the list of all the ancestors of the node, in the order of the hierarchy: from the root node down to the node itself. The node-path is similar to an XPath, but each step is the identifier of a node, not an XML element. The node-path of a field is the node-path of its parent node, followed by the identifier of the field.

field

A field is a data item of the conceptual model. Every field has a parent node. Every field has an XPath relative to the absolute XPath of its parent node. The relative XPath of a field can contain more than one step, each of which stands for an XML element or an XML attribute. A forward slash / always separates XPath steps. The relative XPath of a field can point to an XML element or to an XML attribute. A field can be repeatable or not.

Repeatable field

A repeatable field can repeat inside its parent node. When a field is repeatable, the XML element that repeats in the XML document is the first XML element (the first step) of its relative XPath. A repeatable field can have several repeatable nodes in its node-path.

Multilingual field

A multilingual field is never repeatable. However, the user can create several instances of a multilingual field, to store its text in different languages. All the instances of a multilingual field belong next to each other, inside the parent node of the field.

Items of the visual model

display-group

A display-group is a structural item of the visual model. It stands for a section of the notice, or for a group of input-fields. Every display-group, except the root display-group, has a parent display-group. A display-group can contain several other display-groups, and several input-fields. A display-group can be repeatable or not.

Repeatable display-group

A repeatable display-group can occur more than once inside its parent display-group. A repeatable display-group always matches a repeatable node of the conceptual model.

input-field

An input-field is a data item of the visual model. It stands for an input control of the form, in which the user enters the data of a specific field. An input-field always matches a field of the conceptual model. An input-field can be repeatable or not.

Repeatable input-field

A repeatable input-field can occur more than once inside its parent display-group. A repeatable input-field always matches a repeatable field of the conceptual model.

Step 1: from the visual model to the conceptual model

When you built your visual model, you used the visual structure of the notice type definition files of the eForms SDK. To transform the notice data to the conceptual model, go through all the items of the visual model, one by one, and follow the steps described here.

You can go through the tree of the visual model in several ways. Choose the way that makes most sense to you. The Publications Office goes through the visual model depth first, that is, it goes down each branch to its end before it moves to the next branch. This page assumes that you use the visitor design pattern, or a similar approach, in which a method "visits" each item of the tree in turn.

Visiting the items of your visual model

First, create all the repeatable nodes needed in the conceptual model. This is the most difficult part, and the most important one to get right.

It works as follows. For every repeatable node that needs to appear in the conceptual model, your visual model always has a repeatable display-group that matches that node. So go through all the display-groups of your visual model, and check whether each of them matches a node.

Go through the tree depth first, starting from the display-groups at the root level of your visual model. For this, your visual model must keep its hierarchy, so that you can move to the parent item and to the child items of any visual item that you visit.

If a display-group is not repeatable, continue to its first child display-group. As soon as you find a repeatable display-group, create one or more nodes for it in the conceptual model: as many nodes as the number of times that the user has repeated the display-group in the notice data.

When a node is marked as repeatable, the node itself repeats inside its parent node.

Before you create a node in the conceptual model, always create its parent node first. In this way, you always create the root node of the conceptual model first, and there is always a parent node to which you can attach your new node. Once the parent node exists, create one node for each instance of the repeatable display-group, and attach it to its parent node in the conceptual model.

When you create a node

When you add a node to your conceptual model, give it an instance identifier that no other node has. Give the same instance identifier to the matching display-group of your visual model. You do this so that you can later find the instance of the display-group that matches a given instance of a node, and the other way round.

You may also manage without instance identifiers, depending on how your recursive algorithm is built. What matters is that you can match an instance of a display-group with the matching instance of a node. If you can do this without instance identifiers, that is fine too. The Publications Office recommends instance identifiers, because with them you always match each instance of a display-group with the right instance of a node.

Continue the search, depth first, until you find the next repeatable display-group.

Nested repeatable display-groups

What if a repeatable display-group is nested inside one or more repeatable display-groups? In such a case, you can safely assume that the visual and the conceptual models always nest in the same way.

For example, suppose that you visit the repeatable display-group-B, which has a repeatable ancestor display-group-A in the visual model. node-A is the repeatable node that matches display-group-A, and node-B the one that matches display-group-B. You can then assume that the repeatable node-A is also an ancestor of the repeatable node-B in the conceptual model.

Here you see why you go through the tree depth first. You have already visited and processed all the repeatable ancestors of the display-group, so their matching nodes already exist in the conceptual model. You only need to find the right instance of the repeatable ancestor node, to which you attach the new node.

For this, you need the instance identifiers. You are going through the visual model depth first, and you are visiting a repeatable display-group. Find its first ancestor that is also repeatable, and take its instance identifier. Look for the node of the conceptual model with the same instance identifier: this is the node that you need. From that node, rebuild the node-path down to the node that you want to attach.

Continue the search, depth first, until the end. You now have a conceptual model that contains all the nodes needed to build your physical model.

Visiting input-fields

When you visit an input-field, add the matching field to your conceptual model.

Much of what applies when you visit an input-field is the same as when you visit a repeatable display-group. This description is short, because it assumes that you have just read the previous part of this page, and know the concepts involved.
As with nodes, before you add a field to the conceptual model, always add its parent node first.

An input-field never has a repeatable ancestor in the visual model, unless that ancestor matches a repeatable node that is also an ancestor of the matching field. So, whenever you visit an input-field, you can safely assume that you have already visited its repeatable ancestors, and that their matching nodes already exist in the conceptual model.

If the input-field has repeatable ancestors in the visual model, follow the same steps as for nested repeatable display-groups. Find the first repeatable ancestor in the visual model, and go to the matching node in the conceptual model. From there, rebuild all the nodes of the node-path of the field that are needed, until you can add the field itself.

If the input-field is repeatable, add as many instances of the matching field to the conceptual model as the number of times that the user repeated the input-field in the notice form.

If the input-field has no repeatable ancestors, create the ancestor nodes that the conceptual model does not contain yet. A node that has no repeatable ancestors appears only once.

Step 2: from the conceptual model to the physical model

You now have all the data of the notice in a new data structure, the conceptual model, which is much closer to the final form of the notice data in the XML file. Your aim is now to transform the notice data from the conceptual model to the physical model, so that you can then write it in XML.

The conceptual model contains nodes and fields. The physical model contains XML elements and attributes. Each node and field has an XPath relative to its parent node. This relative XPath contains one or more steps, separated by a forward slash '/'. Each of these steps is an XML element or an XML attribute.

To build your physical model, you need to create the XML elements that the relative XPath of each node and field of your conceptual model describes. For this, go through the conceptual model item by item. Go through it depth first again, starting from the root node.

Visiting a node

When you visit a node of the conceptual model, take its relative XPath and extract its steps. Add an XML element to the physical model for each step that you extracted.

If the node is repeatable, repeat only the first step that you extracted from its relative XPath.

Visiting a field

As for a node, extract all the steps of the relative XPath of the field that you are visiting, and add them to the physical model.

If the field is repeatable, repeat only the XML element that matches the first step that you extracted.
Some fields point to XML attributes. Make sure that your physical model shows this, so that you can take it into account when you write the XML.

Once you have visited all the nodes and fields of the conceptual model, you have a complete physical model, which you can write directly in XML.

Getting the XML elements

You have to extract the XML elements and XML attributes that you add to your physical model from the relative XPaths in fields.json. These XPaths are not always simple: many of them contain one or more predicates.

Splitting the XPath text at each '/' does not work. You can use regular expressions to extract the steps. The Publications Office uses an XPath parser, to be safe.
For now, you need to extract the steps from the XPath yourself. The Publications Office intends to give this information directly in fields.json, in a future release of the SDK.

Getting the order of the XML elements right

In fields/fields.json, a property called xsdSequenceOrder is available for every field that does not point to an XML attribute, and for every node except ND-Root. For more information, see the field metadata documentation.

A simplified example of a field in fields.json:

{
    "id" : "BT-13713-LotResult",
    "parentNodeId" : "ND-LotResult",
    "xpathRelative" : "efac:TenderLot/cbc:ID",
    "xsdSequenceOrder" : [ { "efac:TenderLot" : 16 }, { "cbc:ID" : 1 } ]

Here, the xpathRelative has two parts. For each part, xsdSequenceOrder gives the position of the element among the children of the same parent element:

  • efac:TenderLot has the position 16 among the children of the element of the parent node ND-LotResult. So up to 15 elements can come before it.

  • cbc:ID has the position 1 among the children of efac:TenderLot. So, if it is present, it must be the first child.

Order of repeated elements

If a field or node is repeatable, the repeated elements must be next to each other in the XML. Keep the original order of the repeated items.

Order of attributes

XML does not set an order for attributes, but you can sort them in alphabetical order. In this way, the order of the attributes does not vary each time that you generate the XML of the same notice.

Example sorting algorithm

The Publications Office recommends that you first build the whole XML of the notice, and sort it at the end. The editor demo contains an algorithm that sorts the XML of a notice. Before SDK 1.8, the sorting algorithm of the editor demo relied only on information found in the schemas (XSD) of the SDK.