XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Web-based XML Editing with W3C XML Schema and XSLT, Part 2
Pages: 1, 2, 3

MetaXSL+

What we need is an XSLT stylesheet which takes our XSD and transforms it into the corresponding XSL+. The concept applied is the same as the concept used for the generation of XSLGUI. The stylesheet which generates the XSL+ is called MetaXSL+ here.

XML Schema to XSL+ processor
Figure 3. XML Schema to XSL+ processor

The basic task of the MetaXSL+ is as follows:

  1. For every element make an xsl:template with the match attribute equal to the name of the element.
  2. If the element is a datatype element, add it and the functions needed to produce the value of the element to this xsl:template
  3. If the element is complexType, add the xsl:apply-templates select="*" function
  4. For every element make an xsl:template with the match attribute equal to insert-elementName where elementName is the name of the element.
  5. If the element is complexType, add each child element (recursively) N times where N is the minOccurs of the child element

Creating new instance documents

We saw that the XSL+ is able to transform an insert-elementName tag into the corresponding element with the correct body. Now making an instance of Person.xsd is nothing else but a insert-person tag in a document (bootstrap). All we need is an XSLT which finds the first element of our XSD (person) and creates an XML+ document which consists of only one line namely:

<insert-person>

The following XSLT does the trick:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="xsd:element">
<xsl:variable name="inserttag" 
                 select="concat('insert-', @name)"/>
  <xsl:element name="{$inserttag}"/>
</xsl:template>
</xsl:stylesheet>

Using the same cycle as described above this new XML+ document will be transformed into an instance document using the XSL+. The result will be

<xsl:element name="person">
  <xsl:element name="name"/>
  <xsl:element name="date_of_birth"/>
  <xsl:element name="phone"/>
</xsl:element>

which is an initial instance of Person.xsd.

Inserting elements which do not appear in the instance document

The question now is how can we add an element which does not appear in the new instance document (an element with "minOccurs=0") such as the course in a new person instance or the course_code in a new course element.

There are different approaches possible:

  1. When making a new instance document, add all the elements to the document, including elements with a minOccurs equal to zero. This approach is an easy way out, and even though the created instance document is a valid document, it is not what we expect: an empty element is not equal to not having that element in the instance document.
  2. Adjust the XSLGUI so that it generates plus signs after each element. In our person example we would have a plus sign for course after name, one after date_of_birth and one after phone. This way if the user clicks on either of the first two plus signs the course element will be inserted at a place which is not allowed according to our XSD. An insert-course after the phone element will be valid. For an instance document with a huge number of elements this approach can be frustrating for the user as there will probably be lots of attempts before the user can find a valid location.
  3. Find a way through trial and error (validation) to know exactly where plus signs are meaningful and give that knowledge to the XSLGUI stylesheet. Conceptually it is an elegant solution but technically it is quite an effort to implement.
  4. Another solution and the one chosen in this article is to produce plus signs for the children of a complexType element at that level as follows. For all the children, if the child has a minOccurs equal to 0, and if the number of element occurrences in the document is equal to 0, produce a plus sign at the level of the complexType element containing the children.

In our example person is a complexType and its child course has a minOccurs equal to 0. The XSLGUI will be generated as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:strip-space elements="*"/>

  <xsl:template match="person">
    <xsl:param name="root">
      /person
    </xsl:param>
    <xsl:variable name="index">
      1
    </xsl:variable>
    <html>
    <head>
      <title>person</title>
    </head>
    <body>
    <form name="xmsForm" action="xms.InputPage"
                            method="post">
      
      <!-- The output of the following will look 
              like:
              <a href="javascript:submitForm(
                          '/person[1]/course', 
                          'insertfirstposition');"
                    onClick="submitForm(
                            '/person[1]/course',
                            'insertfirstposition'); 
                            return false;">
                            Add course +
                            </a>
      -->              
<xsl:variable name="courseCount" 
                          select="count(course)"/>
      <xsl:if test="$courseCount='0'">
        <a>
          <xsl:attribute name="href">
            <xsl:value-of 
              select="concat('javascript:submitForm(', 
                              "'",'/person[',
                              $index,']','/course',
                              "'",',',
                              "'",
                              'insertfirstposition',
                              "'",');')"/>
          </xsl:attribute>
          <xsl:attribute name="onClick">
            <xsl:value-of 
              select="concat('submitForm(',
                              "'",'/person[',
                              $index,']','/course',
                              "'",',',
                              "'",
                              'insertfirstposition',
                              "'",'); 
                              return false;')"/>
          </xsl:attribute>Add course +
        </a>
      </xsl:if>
      <xsl:apply-templates>
        <xsl:with-param name="path" 
                           select="$root"/>
      </xsl:apply-templates>
      <input type="submit" name="action" 
                              value="save"/>
    </form>
    </body>
    </html>
  </xsl:template>
  ...
</xsl:stylesheet>

Now at the person level (top level), if the user clicks on the plus sign to insert a course, a request will be sent to the server with the action insertfirstposition and the XPath parameter will be /person/course. We know that the new instance document does not contain any course elements (otherwise we would not get a plus sign for course), so there is no way we could know where to insert a course element. All we know is that it has to be inserted at /person. Then, beginning at the first position in /person, insert the course and validate the document. If valid, we will use the XSLGUI to produce the GUI; otherwise, we will insert the element at the next position till we find a valid position for the course. This approach inserts the element at the first allowed location in the original instance document. Note that it might be desirable to insert the element not at the first possible location but somewhere else. This could become an issue in very complex combinations of elements.

The XUpdate document used to insert the new element at different positions is as follows:

   <?xml version="1.0" encoding="ISO-8859-1"?>
  <xu:modifications 
       xmlns:xu="http://www.xmldb.org/xupdate">
    <xu:append select="/person[1]" 
                  child=index>
       <xu:element name="insert-course"/>
    </xu:append>
  </xu:modifications>

Where index starts at 0 and increases by one till there is a valid position found.

Conclusion

This article described a concept in which elements can be inserted into an XML instance document through an auto-generated, form-based GUI, based on the XML Schema of the instance document and XSLT. The capability of editing and inserting or removing elements using the corresponding XML Schema makes it a complete and functional approach for the implementation of an XML web-based editor.

Resources



1 to 5 of 5
  1. Support for choose option
    2006-04-03 14:12:02 Ram_Peri
  2. MetaXSLgui doesn't work?
    2003-08-19 16:35:46 Anthony Hope
  3. a generic solution
    2003-08-11 05:18:37 Ali Mesbah
  4. a generic solution
    2003-07-29 08:49:39 Erik Ostermueller
  5. very nice
    2003-06-27 16:07:13 Eric Hanson
1 to 5 of 5