Re: [xsl] XSLT-driven syntax highlighting for XPath 2.0, XSLT 2.0 and XSD 1.1

Subject: Re: [xsl] XSLT-driven syntax highlighting for XPath 2.0, XSLT 2.0 and XSD 1.1
From: Philip Fearon <pgfearo@xxxxxxxxxxxxxx>
Date: Tue, 4 Dec 2012 11:02:20 +0000
I've refactored xmlspectrum.xsl to provide better support for use
cases such as XMLspec where many different XSLT, XPath or XSD samples
are included in a single source file (note that the
xmlspec/xmlspectrum naming similarity was totally unintentional,
'xmlrainbow' didn't sound quite right - I hope no one objects).

Previously there was an annoying dependency on XSLT parameters for
setting values such as the target-language XML namespace prefix, this
only worked well for one-off transforms on a whole file.

The core xmlspectrum.xsl file has just two 'interface' functions:
'render()' for processing XML extracts containing XPath and
'showXPath()' for plain-text XPath.

This core is now much more suitable for including/importing into a
'front-end' XSLT stylesheet. Two implementations that import
xmlspectrum.xsl are now also included on GitHub. These are
'highlight-inline.xsl' for syntax-highlighting multiple samples
embedded in a single document, and 'highlight-file' for
syntax-highlighting a whole file.

To underline what was said previously on this thread, the XML 'parser'
built into xmlspectrum.xsl is simplistic by design. No attempt is made
to resolve entity references or namespaces so sample extracts that are
missing namespace or DTD declarations should still be rendered
properly by the 'render()' function, the namespace prefix used for
XSLT or XSD is passed in as a parameter of the 'render()' function.

Links:

Sample HTML output from an XHTML input file containing 3 embedded
XSLT, XSD and XPath code samples and transformed using
'highlight-inline.xsl':

http://qutoric.com/xslt/xmlspectrum/html/highlighted-inline.html

The changes have been checked into GitHub at:

https://github.com/pgfearo/xmlspectrum

A note on the XSLT source formatting:
------------------------------------------
You may notice on GitHub that the XSLT source code for xmlspectrum.xsl
is formatted only for XPath, not for XML - this is down to my own
'virtual formatting' XSLT editor which makes XPath editing easier (at
least for myself) by not injecting XML formatting characters into
XPath expressions . If you reformat the source in a conventional XML
editor everything will be fine except for the XPath formatting which
will need to be formatted slightly to be aligned with its containing
XML.

Phil

On Fri, Nov 30, 2012 at 10:23 AM, Philip Fearon <pgfearo@xxxxxxxxxxxxxx> wrote:
> Excellent, it would be good to see this.
>
> A minor change to xmlspectrum.xsl would be required to identify partial code
> examples by something other than the namespace where this is omitted from
> examples for brevity sake (currently if the XML parse to extract the
> namespace fails, the input is assumed to be plain-text XPath).
>
> The 'xsl' prefix used for the XSLT namespace would also need to be supplied.
> I'm thinking now that partial examples with missing namespaces are probably
> the most common use case, so I will put a code change in place for this.
>
> Phil
>
>
> On Fri, Nov 30, 2012 at 9:54 AM, Michael Kay <mike@xxxxxxxxxxxx> wrote:
>>
>> Looks good. I might experiment by producing a version of the XSLT spec
>> with the examples in this form, to see what people think of it.
>>
>> Michael Kay
>> Saxonica
>>
>>
>>
>> On 30/11/2012 09:39, Philip Fearon wrote:
>>>
>>> I frequently need to publish documentation in HTML form that has code
>>> samples
>>> which include XPath 2.0 expressions, either standalone or embedded in
>>> XSLT 2.0.
>>> But has anyone else noticed that there aren't many syntax highlighters
>>> for standalone
>>> or XML-embedded XPath on the web?
>>>
>>> To improve this choice, I've developed an XSLT 2.0 stylesheet that's a
>>> syntax highlighter
>>> specially for XPath 2.0, XSLT 2.0, XSD 1.1 (the assert element) and
>>> also suitable for
>>> generic XML. The input to the stylesheet (specfied in an XSL
>>> parameter) is either XML
>>> or plain-text, the output is an HTML document and a CSS file.
>>>
>>> I'm sharing this in the hope that others may also find it useful. Note
>>> that this is not a
>>> 'pretty-print' formatter, it is assumed that whitespace formatting is
>>> in-place and this is
>>> all preserved in the output (including inter/intra-attribute whitespace
>>> etc.).
>>>
>>> One main concern with embedded languages (like XPath within XSLT) is
>>> not to overwhelm
>>> the reader with highlighted code colors. To alleviate this I've used
>>> the Solarized[1] color
>>> theme which has 8 highlight colors (+ 8 base colors) of relatively low
>>> contrast. Cooler
>>> colors are used for XML-based syntax and warmer colors for the XPath.
>>> There's some
>>> crossover though: for example, to show that an xsl:function 'name'
>>> attribute is referenced
>>> from XPath, a warmer color is used.
>>>
>>> Features List:
>>> --------------
>>> - Open source - maintained on GitHub
>>> - Processes plain-text or XML files
>>> - Identifies XML coloring scheme from the root namespace (if XML)
>>> - Just 1300 or so lines of XSLT in one file - no dependencies
>>> - Standard XSLT 2.0. No extensions required (developed/tested on
>>> Saxon-HE and Saxon-CE)
>>> - Uses 'Solarized' color theme
>>> - Light or dark color themes
>>> - Generates the required CSS file also - depending on theme specified
>>>
>>> - XPath 2.0 Highlighting:
>>>            - Supports XPath Comments
>>>            - All whitespace formatting preserved
>>>            - No dependency on reserved keywords
>>>            - Standalone files supported - or embedded in XML
>>>            - Designed to be extensible to support XPath 3.0
>>> - XML Highlighting
>>>            - Built-in XML parser (coded in XSLT) keeps all text, as-is
>>>            - CDATA preserved intact and highlighted
>>> - XSLT 2.0 Highlighting
>>>            - Scheme colors help separate instructions from expressions
>>>            - Literal Result Elements have different coloring
>>>            - AVTs or native XPath attributes supported
>>>            - All whitespace formatting preserved
>>> - XSD 1.1 Highlighting
>>>             - Element and Attribute definition elements highlighted
>>>             - XPath coloring for *test* attribute in *assert* element
>>>
>>> Links:
>>>
>>> Sample screenshot of XSLT 2.0 using dark color theme:
>>>
>>> http://qutoric.com/xslt/xmlspectrum/images/xsl-dark.PNG
>>>
>>> The stylesheet 'xmlspectrum.xsl' is on the GitHub project site along
>>> with samples etc. at:
>>>
>>> https://github.com/pgfearo/xmlspectrum
>>>
>>> Acknowledgements:
>>>
>>> [1 Solarized] http://ethanschoonover.com/solarized
>>>
>>> As ever, any feedback on color themes, missing features, issues etc
>>> would be greatly appreciated. Details on other XSLT stylesheets that
>>> do the same thing that would also
>>> be useful to know.
>>>
>>> Many thanks,
>>>
>>> Phil Fearon
>>> http://qutoric.com

Current Thread