Best Practices and Coding standards for Using XSLT Transformations

The following XSLT guildelines should be followed for the development of XSL stylesheets.

First, and most obvious, indent (and do it with tabs, not spaces). The standard practice is to add one tab of indent for each enclosing tag.


          For example:
        <xsl:stylesheet>    
        <xsl:template match=”/system-index-block”>
        <h1>xsl:value-of select=”heading”/></h1>    
            <p><xsl:value-ofselect=”paragraph”/></h1>    
        </xsl:template>
      

  Comment the code liberally. If you feel like you’re putting in too many comments,it’s probably just enough. Specifically, be sure to give explanations of the purpose of any variables you use, and the function of each template.

  Try to keep your XPath statements simple. These are the statements that appear with select=”…” sections. When these statements get too long they can be very hard to decipher, so it’s best to simplify as much as possible.

  Avoid XSLT named templates; Use Template Match instead as it gives procedural feel to the code. When you have a stylesheet with lots of template matches, (and modes) it gets a lot easier to read, and cuts a LOT of code, especially when calling/applying these templates.

Use XSLT named templates for utility functions.

Use the element name itself rather than 

               xsl:element for e.g.
          
            <xsl:element name=”name”>
                <xsl:value-of select=”$product/name”>
            <xsl:element>

    
        Instead use:

            <name>
                       <xsl:value-of select=”$product/name”>
         <name>
        </name>

                      </xsl:value-of>

       </name>

      </xsl:element>

                    </xsl:value-of>

      </xsl:element>


Leave a Comment