ns_xml 2.0 Requirements and Design

by John Mileham

Motivation

ns_xml as it exists currently is an integral part of OpenACS 4.x, but its implementation fails to expose several important aspects of libxml's tree manipulation API. As a result, it is impossible to create some legal XML documents using ns_xml. The shortcoming lies in ns_xml's inability to create node content through the use of text nodes. In ns_xml, the only way to manipulate the content of a node is to either initialize the node's content at creation time (e.g. through a node new_child call) or by setting it explicitly through node setcontent. That overlooks the case of this simple XML snippet:

<sentence>Here is <keyword>my</keyword> sentence</sentence>

That is only part of the challenge, however. ns_xml is also limited to the linear creation of a document. This is great if your goal is to programmatically serialize data in a database, which is the obvious use of ns_xml within OpenACS. If you hope to mutate the structure of an existing document or create a document in a random-access fashion (e.g. through a user interface), however, you're in trouble. The following shortcomings exist:

The Requirements

  1. Support for all legacy (ns_xml 1.x) Tcl code.
  2. Support for text node creation.
  3. Support for "insert as previous" node creation for both text and element nodes.
  4. Support for node deletion (recursive).
  5. Support for node relocation, including cross-document relocation. Nodes moved between documents of different persistence states will inherit the persistence of the new document (i.e. a node moved from a persistent document to a transient document will vanish at the end of the Tcl interpreter's session).
  6. Support for in-place node duplication (recursive). The duplicate will be instantiated as the sibling following the cloned node.
  7. Support for bottom-up tree traversal, from child to parent.
  8. Support for unsetting attributes
And from the "While we're at it" department:
  1. Support rendering of any node as a stand-alone XML document (recursive)
  2. Get rid of the ns_xml doc free naming convention. This is Tcl. We don't want to think about freeing memory. We're just deleting persistent documents here. :)

The Design

Creating this functionality means adding several commands to the ns_xml API. The naming scheme of ns_xml's existing calls is very intuitive to the Tcl developer. However, it quickly became apparent that the naming scheme wasn't general enough to encompass the new functionality cleanly. If an attempt were made to add the functionality described above within the existing naming scheme, all transparency would be lost. Tcl developers would find themselves referring to reference material constantly. Luckily, the naming scheme proposed below does not conflict with the existing scheme (except in cases where the commands are identical between the two schemes). Thus, all the deprecated calls can be mapped to their 2.0 equivalents, and a user can run legacy Tcl code unmodified.

In the new scheme, commands are divided into four functional buckets which largely coincide with their first word. All calls follow the grammatical convention:

subject verb [object]
with the sole exception of ns_xml create xml, which is unique in that its job is to conjure up an object with no relationship to anything that exists.

Note that the Node Interaction bucket is subdivided into several categories due to its complexity.

The 2.0 API

Why'd you do that? (a.k.a. Design Considerations)


John Mileham