Summary of the XSLT Coding Guidelines - ΩJr. Coding Guidelines

This information lives on a webpage hosted at the following web address: 'https://www.omegajunior.net/code/guidelines/'.

XSLT Coding Guidelines are intended to make it easier to read our work, both for colleagues when we are away, and for ourselves in a year's time. The Guidelines also answer newbie questions, in order to reduce the learning curve.

This is the summary. It omits explanations. It contains the guidelines and the examples. For explanations, visit the annotated version.

Suggestions are welcome.

A word from a sponsor:


Check the XSLT versions the parser in your environment allows
XSLT 2 has been around for quite a while now, but not all environments allow us to use it. Smartsite for example, can create XSLT 2, but executes XSLT 1.0 only.


Use the xsl:transform element
Rather than the xsl:stylesheet element.
<?xml version="1.0"?>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
>
</xsl:transform>



Always add an xsl:output element
<xsl:output
method="html"
version="1"
encoding="iso-8859-1"
omit-xml-declaration="yes"
standalone="yes"
doctype-public="html"
cdata-section-elements=""
indent="yes"
media-type="text/html"
/>

Definition and Usage of xsl:output as described by W3Schools.com


Suppress unexpected text matches
<xsl:template match="/text()" />


Avoid xsl:for-each when a template match suffices
Unless you need the additional benefits of an xsl:for-each, simply use a template match instead.


Avoid named templates when a template match suffices
A named template has a semantical implication: there was no way this template could have been reached by simply following the input document structure using a straightforward XPath and / or XQuery instruction. If you need that: fine. Otherwise, perform a template match.


When naming templates, choose names based on functional intent
Don't choose a name based on data or XSLT execution. Instead, make clear why the template is used. Try to do so in as few words as possible. If you find your template names turning into sentences, try breaking the templates into smaller units.


If at all possible, generate new mark-up using xsl:element
Unfortunately, this also means that you need to add element attributes using xsl:attribute.
<xsl:template match="/products/link">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@url" /></xsl:attribute>
<xsl:attribute name="title"><xsl:value-of select="hint" /></xsl:attribute>
<xsl:value-of select="title" />
</xsl:element>
</se:template>



Block indentation
Separate templates by a blank line, and indent levels using 2 spaces. If you need to supply multiple properties per element, put each on its own line, indented. If you need to supply multiple sub elements, put each on its own line, indented. Put each element on a new line.

<?xml version="1.0"?>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
>

<xsl:template match="/text()" />

<xsl:template match="/products">
<xsl:apply-templates select="link" />
</se:template>

<xsl:template match="link">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@url" /></xsl:attribute>
<xsl:attribute name="title"><xsl:value-of select="hint" /></xsl:attribute>
<xsl:value-of select="title" />
</xsl:element>
</se:template>

</se:transform>



Javascript returned by XSLT
First tell the XSLT parser to ignore the Javascript code. Then tell the HTML parser to do the same:

<xsl:element name="script">
<xsl:attribute name="type">text/javascript</xsl:attribute>
<xsl:text><![CDATA[/* &#60;![CDATA[ */
//Your Javascript code here
/* ]]&#62; */]]></xsl:text>
</xsl:element>

Need problem solving?

Talk to me. Let's meet for coffee or over lunch. Mail me at “code at omegajunior dot net”.

Clicky Analytics