Web-based XML Editing with W3C XML Schema and XSLT, Part 2
In an earlier article we talked about an approach to automatic, form-based GUI generation based on XML Schema -- an approach which uses a single XSLT stylesheet, through which editing XML instance documents is made possible. Open issues remained as how to add new elements to an instance document and how to create an initial instance of our schema.
One of the most common uses for schemas is to verify that an XML document is valid according to a set of rules. A schema can be used to give the order in which elements must appear or their type. We are also able to define the cardinality of the elements in the schema. This information can be used to insert (or delete) elements to (or from) an instance document while keeping the instance document valid.
This article describes a concept in which elements can be inserted into an XML instance document through an automatically created form-based GUI, based on the XML Schema of the instance document.
Editing an XML instance document
In "Web-based XML Editing with W3C XML Schema and XSLT" the concept of editing existing instance documents, using the corresponding schema, was described. The following figure represents the concept.
Figure 1.Editing an XML instance document
In this concept the value of the elements in the instance document can be edited, but it does not provide a way to insert (or delete) elements into the instance document.
We take a look at our previous example (with a few changes marked as bold) of a schema (further labeled as XSD) called Person.xsd in which we define the elements a person can have plus the cardinality of each element:
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="person"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="phone" type="xsd:string" maxOccurs="3"/> <xsd:element name="date_of_birth" type="xsd:date"/> <xsd:element name="course" minOccurs="0" maxOccurs="3"> <xsd:complexType> <xsd:sequence> <xsd:element name="course_name" type="xsd:string"/> <xsd:element name="course_code" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
<?xml version="1.0" encoding="UTF-8"?> <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Person.xsd"> <name>Marc</name> <phone> 0153816546 </phone> <phone> 0630458920 </phone> <date_of_birth> 1978-01-21 </date_of_birth> <course> <course_name> TCP/IP </course_name> <course_code> T465 </course_code> </course> <course> <course_name> Java Programming </course_name> <course_code> J867 </course_code> </course> </person>
Inserting elements which appear in the instance document
Imagine you have an instance document which has a certain number of an element type. You want to insert an extra element of the element type into your document. The general idea for inserting elements through our GUI is as follows:
- The GUI places a plus (+) sign after the element if the XSD allows it
- The user can click on the plus sign requesting the insertion of that element
- A request is sent to the servlet with the name of the element and the location where the new element has to be inserted
- The servlet inserts the element and validates the document
- If the instance document (with the new element) is valid, it will be transformed into the GUI
- The user can now fill the value of the inserted element and save the document.
Now, using an example, we will work out the way the concept can be approached using XSLT and XML Schema.
Having this cardinality information in our schema, we would like to find a way to insert elements to our XML file through the GUI while keeping the instance document valid.
To be able to insert elements, we will use the cardinality
maxOccurs) defined in the XSD
document and put a plus (+) sign after each element which has a
cardinality higher than the number of elements present in the XML instance
document. For instance, for Marc.xml, because Marc has two
courses and the
course is defined
to be 3 the GUI will get a plus sign after each course.