Alternative SDL-like XSLT Syntax

The need to constantly write xsl: prefix, angle brackets, verbose instructions make XSLT syntax somewhat odd for a newbie. Though the fact that XSLT is also an XML is a big plus.

The question is, can XSLT syntax be refactored to resemble syntax of regular programming language, yet without losing compatibility w/ XML?

Designing The Syntax

XSLT is based on XML, and there are many, yet not compatible, alternative languages to XML: JSON, YAML, OGDL, SDL. One of particular interest is SDL, since its syntax resembles programming language syntax:

folder "myFiles" color="yellow" protection=on {
    folder "my images" {
        file "myHouse.jpg" color=true date=2005/11/05
        file "myCar.jpg" color=false date=2002/01/05
    }
    folder "my documents" {
        document "resume.pdf"
    }
}

However SDL is not one-to-one mappable w/ XML. To fix this, a list, map, matrix features of SDL are thrown out, only comment, element, attribute features are left:

folder name="myFiles" color="yellow" protection="on" {
    folder name="my images" {
        file name="myHouse.jpg" color=true date=2005/11/05;
        file name="myCar.jpg" color=false date=2002/01/05;
    }
    folder name="my documents" {
        document name="resume.pdf";
    }
}

XSLT is an XML, so it can be expressed in SDL:

xsl:variable name="var1" {
    xsl:apply-templates select="node()";
}

Since SDL curly brace and XML angle bracket syntaxes do not intersect, it is ok to mix SDL and XML:

xsl:variable name="var1" {
    <wrapper>
        xsl:apply-templates select="node()";
    </wrapper>
}

Next, it is quite reasonable to allow SDL and XML prefixes to be bound to different namespaces. This way it becomes possible to write XSLT without xsl: prefix:

variable name="var1" {
    <wrapper>
        apply-templates select="node()";
    </wrapper>
}

To simplify XSLT in SDL code even further, a concept of default attributes is introduced:

include "file.xsl"; // href is default attribute in xsl:include
...
variable name="var1" {
    <wrapper>
        if "$var0" { // test is default attribute in xsl:if
          apply-templates "node()"; // select is default attribute in apply-templates
        }
    </wrapper>
}

Finally, a special trick to handle name& select attribute pairs is needed:

// below is identical to xsl:variable name="var1" select="..."
variable $var1="...";
/* or */
call-template "templateName" { // name is default attribute in xsl:call-template
    with-param $arg1=""; // short form of xsl:with-param name="arg1" select="..."
    with-param $arg2="" tunnel="yes";
}

How real-life code would look after all? Check json2xml.txt that is attached below -- this is a translated version of my previous XSLT converting JSON to XML.

Implementing The Syntax

Of course, to support new syntax, XSLT processor does not need to be modified. What is needed, is a parser capable of reading a mix of SDL and XML markup, and generating SAX/DOM result that is totally transparent to XSLT processor.

Implementing such kind of parser is an enormous effort, much larger than implementing regular XML parser. Such kind of effort is not acceptable. 

Instead I have chosen to implement SAX ContentHandler that intercepts characters() event, parses the text, and generates XSLT instructions from SDL on the fly. xslt-sugar.zip that is attached below contains full source code to ready to work with any Java XSLT processor.

Of course, what is missing is IDE support w/ syntax highlighting, code assist, error detection. Theoretically IDE plugin(s) for such kind of XSLT can be easily derived from XQuery IDE plugins for Eclipse, IntelliJ IDEA, etc. — syntax of SDL-XSLT is somewhat close to XQuery syntax.

AttachmentSize
json2xml.txt3.93 KB
xslt-sugar.zip27.38 KB

Comments

Great stuff...

Not tried this yet: but I like the fact that you recognize the usefulness of XSLT, but also its fairly awkward syntax in its native-form....keep at it ! Cheers !

Post new comment

The content of this field is kept private and will not be shown publicly.