Friday, 19 August 2011

Virtual Formatting

Writing an XSLT Editor - Part 2


For more than 6 months now I've been developing the concept of virtual formatting for the XML Editor now integrated into CoherentWeb. This concept is simply where XML text is indented by adjusting the left-margin, rather than using tab or space characters for the same end. There's nothing new in this idea, web browsers use virtual formatting each time they're used to view an XML file, they don't go to the trouble of modifying the content, just the way it's rendered.

What works well as an XML viewing concept, however, is an entirely different beast when its used in an environment where, as in an XML editor, the text is potentially changed as quickly as someone can type. The rewards though are huge, bringing a whole new editing experience that is immediate and compelling, as this 5 minute video hopefully demonstrates:

Video: Imagine an XML Editor that could...

I should probably explain something about the formatting rules used by CoherentWeb:

First, XML formatting is not standardised in any way, but CoherentWeb follows a set of conventions in common use, its these that I've attempted to isolate and describe here. Below is some XML text with no indentation (a screenshot of the XML Editor in left-justified mode), the blocks where different formatting rules apply have been highlighted and annotated:


Let's just see how this XML is formatted when in indented mode before going on to look at each block:

The (background-highlighted) screenshot above shows the same XML as the first screenshot, but this time indented. The block coloring is to associate the indented blocks with their non-indented equivalents that are also annotated.

1. Namespace Declarations

These occur within the element start tag and the same indentation convention is applied as for attributes.

2a. Attribute Names - where the first attribute is on the same line as the element tag

In this case, if subsequent attributes start on new lines then they are aligned with the start of the first attribute name.

2b. Attribute Names - where the first attribute is on a new line

The first attribute is indented by the same fixed amount as if it were element content. Any subsequent attributes on new lines are indented likewise, so they are all left-aligned.

3. Element Tags and content

We're all very familiar with the basic formatting rules for elements and their content, but to reiterate: XML element open and close tags establish the hierarchy of an XML document. Each new open tag marks a new, higher level, of nesting; the entire contents between the open and close tags is therefore indented by a fixed amount to indicate each new level of nesting.

4. Attribute Values

When attribute values extend over several lines, the start of each new line is aligned with the very start of the attribute value. Attribute values may have their own indentation (as shown) outside of the XML context. Unless schema rules indicate other wise, the whitespace used for indentation of attribute values is normalized, it's therefore mainly used to assist in the readability of the XML text.

5. Mixed Content

If an element contains mixed content, then the contents of 'inline' elements is not indented any further than the container element warrants, allowing the text, complete with inline elements to be viewed as a single block.

6. Preformatted Element Content

An XML vocabulary may require that all whitespace content within specific elements is preserved, this is either indicated by the xml:space="preserve" attribute value setting, or declared in a schema defining the element. With virtual formatting, the content can still be rendered indented in the normal way because no characters have to be added to acheive this.

Virtual formatting content with padding characters

Currently, most hand-crafted XML has tab or space characters inserted for formatting, even XML processors such as XSLT (optionally) add whitespace for indentation. For virtual formatting, these characters are removed on loading a file or on clipboard operations.

Editing Benefits

The screenshot below shows the XML Editor with the same contents as the previous screenshot, but with all characters including whitespace highlighted in yellow:


This screenshot helps illustrate how, by dispensing with padding characters, virtual formatting gives absolute control to the user over all content of an XML file and how it's rendered.

0 comments:

Post a Comment

On Twitter...

    follow me on Twitter