Skip to content
Phil Fearon edited this page May 18, 2014 · 29 revisions

The XMLSpectrum project was started with the primary goal of providing syntax-highlighting for XPath-family source code (XSLT, XQuery etc.) and leave the formatting exactly as in the source. However, when testing XMLSpectrum against many XSLT samples in the public domain, a significant number of formatting issues were found in the original source.

Issues in code formatting can severely impact the readability of the code - to the extent where syntax-highlighting becomes largely irrelevant. This is why a significant number of XML/XSLT formatting features are included in XMLSpectrum - along with the option to preserve the original formatting 'as-is'.

This page summarises the XSLT parameters used to control formatting of XML/XSLT by highlight-file.xsl.

auto-trim off

Preserve formatting

XSLT Parameter: auto-trim=no (default) no formatting The above screenshot shows the highlighted result with auto-trim set to 'no'. Note that to ensure the original formatting is kept intact, the following settings are required - these are the defaults:

  • XSLT Parameter: format-mixed-content=no
  • XSLT Parameter: indent=-1
  • XSLT Parameter: force-newline=no

The following settings are also required if the XSLT is not well formed (e.g. namespace declarations are missing).

  • XSLT Parameter: document-type=xslt
  • XSLT Parameter: document-type-prefix=xsl (in this case it is inferred because it is on the first element)

auto-trim on

Remove formatting

XSLT Parameter: auto-trim=yes no formatting

Here we can see that all indentation proportional to the XML nesting level has been identified and removed. Note that the indentation of both the select attribute name and the XPath expression value is still intact, this is a key feature in any tool that attempts to reformat XSLT.

indent 3

Adjust Original Indentation

XSLT Parameter: indent=3 no formatting In this screenshot, the XML formatting of 2 spaces per nesting-level has been removed (because auto-trim is 'on') and then replaced with 3 spaces per nesting level - as specified by the indent=3 parameter value.

indent 2

Add Indentation

XSLT Parameter: indent=3 no formatting

In this example, there's no indentation at all in the original source, so all indentation must be inferred from the XML nesting level and the position of linefeeds before attribute names and inside attribute values. The formatting of the XPath could be improved manually, but XMLSpectrum still manages to align the elements, attributes namespace declarations and multi-line attribute values fairly neatly.

force-newline yes (source has no line-feeds)

Add formatting to non-formatted XML

XSLT Parameter: indent=3 no formatting

Whilst XSLT is itself always likely to contain newlines, the XML input or output of an XSLT transform frequently does not. This parameter inserts new lines to allow indentation of such XML (using the indent parameter).

Here, line-feed characters are not inserted where mixed content is encountered, this minimises the risk of significant whitepsace being affected.

format-mixed-content yes

Add formatting to mixed content - for readability purposes only

XSLT Parameter: indent=3 no formatting

The previous example showed how whitespace is preserved for mixed content. This can however make complex XML harder to 'parse' by eye - especially if annotations have been inserted into text nodes by another process. The format-mixed-content parameter inserts extra line feeds within nested XML of mixed content to improve readability. This practice is not advised in a processing pipeline, but may still be useful to produce help analyze XML content in a standalone view.

color-theme css-inline css-path document-type document-type-prefix font-name format-mixed-content indent link-names output-method output-path sourcepath w3c-xpath-functions-uri

Clone this wiki locally