I know we're not the first who create a parser in xslt. However I still want to share our implementation, as I think it's beautiful.
In our project, which is conversion from a some legacy language to java, we're dealing with dynamic expressions. For example in the legacy language one can filter a collection using an expression defined by a string: collection.filter("a > 0 and b = 7");
collection.filter("a > 0 and b = 7");
Whenever expression string is calculated there is nothing to do except to parse such string at runtime and perform filtering dynamically. On the other hand we have found that in the majority of cases literal strings are used. Thus we have decided to optimize this route like this:
collection.filter( new Filter<T>() { boolean filter(T value) { return (value.getA() > 0) and (value.getB() = 7); } });
This means that we're converting that expression string into java code on the generation stage.
In the xslt - our generator engine - this means that we have to convert a string into expression tree like this:
(a > 7 or a= 3) and c * d = 2.2
to
<and> <or> <gt> <identifier>a</identifier> <integer>7</integer> </gt> <eq> <identifier>a</identifier> <integer>3</integer> </eq> </or> <eq> <mul> <identifier>c</identifier> <identifier>d</identifier> </mul> <decimal>2.2</decimal> </eq> </and>
Our parser fits naturally to the world of parsers: it uses xsl:analyze-string instruction to tokenize input and parses tokens according to an expression grammar. During implementation I've found some new to me things. I think they worth mentioning:
xsl:analyze-string
regex
flag="x"
The flags attribute may be used to control the interpretation of the regular expression... If it contains the letter x, then whitespace within the regular expression is ignored.
/s
Use following link to see the xslt: expression-parser.xslt. To see how to generate java from an xml follow this link: Xslt for the jxom (Java xml object model), jxom.zip.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u