Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>I. Elegant XSLT code</h1> <p><br /> <strong>One can often find examples of beautiful XSLT code, especially when XSLT is used as a functional programming language</strong>. </p> <p>For examples see <a href="http://www.idealliance.org/papers/extreme/proceedings/xslfo-pdf/2006/Novatchev01/EML2006Novatchev01.pdf" rel="nofollow noreferrer"><strong>this article</strong></a> on <a href="http://fxsl.sf.net" rel="nofollow noreferrer"><strong>FXSL 2.0</strong></a> -- the Functional Programming library for XSLT 2.0.</p> <p>As an FP language XSLT is also a <em><a href="http://en.wikipedia.org/wiki/Declarative_programming" rel="nofollow noreferrer"><strong>declarative language</strong></a></em>. This, among other things means that one declares, specifies existing relationships. </p> <p>Such <strong>a definition often does not need any additional code to produce a result -- it itself is its own implementation, or an executable definition or executable specification</strong>.</p> <p><strong>Here is a small example</strong>.</p> <p><strong>This XPath 2.0 expression defines</strong> the "<em>Maximum <a href="http://en.wikipedia.org/wiki/Prime_factor" rel="nofollow noreferrer">Prime Factor</a> of a natural number</em>":</p> <pre><code>if(f:isPrime($pNum)) then $pNum else for $vEnd in xs:integer(floor(f:sqrt($pNum, 0.1E0))), $vDiv1 in (2 to $vEnd)[$pNum mod . = 0][1], $vDiv2 in $pNum idiv $vDiv1 return max((f:maxPrimeFactor($vDiv1),f:maxPrimeFactor($vDiv2))) </code></pre> <p><strong>To pronounce it in English</strong>, the maximum prime factor of a number <strong><code>pNum</code></strong> is the number itself, if <strong><code>pNum</code></strong> is prime, otherwise if <strong><code>vDiv1</code></strong> and <strong><code>vDiv2</code></strong> are two factors of <strong><code>pNum</code></strong>, then the maximum prime factor of <strong><code>pNum</code></strong> is the bigger of the maximum prime factors of <strong><code>vDiv1</code></strong> and <strong><code>vDiv2</code></strong>.</p> <p><strong>How do we use this to actually calculate</strong> the Maximum Prime Factor in XSLT? <strong>We simply wrap up the definition above</strong> in an <code>&lt;xsl:function&gt;</code> and ... get the result!</p> <pre><code> &lt;xsl:function name="f:maxPrimeFactor" as="xs:integer"&gt; &lt;xsl:param name="pNum" as="xs:integer"/&gt; &lt;xsl:sequence select= "if(f:isPrime($pNum)) then $pNum else for $vEnd in xs:integer(floor(f:sqrt($pNum, 0.1E0))), $vDiv1 in (2 to $vEnd)[$pNum mod . = 0][1], $vDiv2 in $pNum idiv $vDiv1 return max((f:maxPrimeFactor($vDiv1),f:maxPrimeFactor($vDiv2))) "/&gt; &lt;/xsl:function&gt; </code></pre> <p><strong>We can, then, <a href="https://stackoverflow.com/questions/439814#445858">calculate the MPF for any natural number</a></strong>, for example:</p> <p><code>f:maxPrimeFactor(600851475143)</code> = 6857 </p> <p>As for efficiency, well, <strong>this transformation takes just 0.109 sec</strong>.</p> <p><strong>Other examples of both ellegant and efficient XSLT code</strong>:</p> <ul> <li><a href="http://www.tbray.org/ongoing/" rel="nofollow noreferrer"><strong>Tim Bray</strong></a>'s <a href="http://www.tbray.org/ongoing/When/200x/2007/09/20/Wide-Finder" rel="nofollow noreferrer"><strong>Wide Finder</strong></a>, as solved <a href="http://dnovatchev.spaces.live.com/Blog/cns!44B0A32C2CCF7488!385.entry" rel="nofollow noreferrer"><strong>here</strong></a>.</li> <li><a href="http://dnovatchev.spaces.live.com/blog/cns!44B0A32C2CCF7488!341.entry" rel="nofollow noreferrer"><strong>Cascade</strong></a> <a href="http://dnovatchev.spaces.live.com/blog/cns!44B0A32C2CCF7488!342.entry" rel="nofollow noreferrer"><strong>deletions</strong></a></li> <li><a href="http://dnovatchev.spaces.live.com/blog/cns!44B0A32C2CCF7488!384.entry" rel="nofollow noreferrer"><strong>Transitive closure</strong></a></li> <li><a href="http://dnovatchev.spaces.live.com/blog/cns!44B0A32C2CCF7488!357.entry" rel="nofollow noreferrer"><strong>Finding all anagrams</strong></a> of a word</li> <li><a href="http://fxsl.cvs.sourceforge.net/fxsl/fxsl-xslt2/Tests/testConcordance.xsl?revision=1.1&amp;view=markup" rel="nofollow noreferrer"><strong>Concordance</strong></a> of a text corpus (the Old Testament)</li> <li><a href="http://fxsl.cvs.sourceforge.net/fxsl/fxsl-xslt2/Tests/testFunc-spell.xsl?revision=1.2&amp;view=markup" rel="nofollow noreferrer"><strong>Spelling checking</strong></a> (Shakespear's Othello)</li> <li><a href="http://ajwelch.blogspot.com/2006/03/dimitres-tuned-sudoku-solution.html" rel="nofollow noreferrer"><strong>Sudoku solver</strong></a></li> </ul> <h1>II. Some rules</h1> <p><br /></p> <p><strong>Here are some rules for writing "quality XSLT code", as taken from <a href="http://gandhimukul.tripod.com/xslt/xslquality.html" rel="nofollow noreferrer">Mukul Ghandi's blog</a></strong>.</p> <p><strong>They can be checked/enforced using a <a href="http://gandhimukul.tripod.com/xslt/xslqual.zip" rel="nofollow noreferrer">tool developed by Mukul</a></strong>:</p> <ol> <li><p>DontUseDoubleSlashOperatorNearRoot: Avoid using the operator // near the root of a large tree.</p></li> <li><p>DontUseDoubleSlashOperator: Avoid using the operator // in XPath expressions.</p></li> <li><p>SettingValueOfVariableIncorrectly: Assign value to a variable using the 'select' syntax if assigning a string value.</p></li> <li><p>EmptyContentInInstructions: Don't use empty content for instructions like 'xsl:for-each' 'xsl:if' 'xsl:when' etc. </p></li> <li><p>DontUseNodeSetExtension: Don't use node-set extension function if using XSLT 2.0.</p></li> <li><p>RedundantNamespaceDeclarations: There are redundant namespace declarations in the xsl:stylesheet element.</p></li> <li><p>UnusedFunction: Stylesheet functions are unused.</p></li> <li><p>UnusedNamedTemplate: Named templates in stylesheet are unused.</p></li> <li><p>UnusedVariable: Variable is unused in the stylesheet.</p></li> <li><p>UnusedFunctionTemplateParameter: Function or template parameter is unused in the function/template body.</p></li> <li><p>TooManySmallTemplates: Too many low granular templates in the stylesheet (10 or more).</p></li> <li><p>MonolithicDesign: Using a single template/function in the stylesheet. You can modularize the code.</p></li> <li><p>OutputMethodXml: Using the output method 'xml' when generating HTML code.</p></li> <li><p>NotUsingSchemaTypes: The stylesheet is not using any of the built-in Schema types (xs:string etc.), when working in XSLT 2.0 mode.</p></li> <li><p>UsingNameOrLocalNameFunction: Using name() function when local-name() could be appropriate (and vice-versa).</p></li> <li><p>FunctionTemplateComplexity: The function or template's size/complexity is high. There is need for refactoring the code.</p></li> <li><p>NullOutputFromStylesheet: The stylesheet is not generating any useful output. Please relook at the stylesheet logic.</p></li> <li><p>UsingNamespaceAxis: Using the deprecated namespace axis, when working in XSLT 2.0 mode.</p></li> <li><p>CanUseAbbreviatedAxisSpecifier: Using the lengthy axis specifiers like child::, attribute:: or parent::node().</p></li> <li><p>UsingDisableOutputEscaping: Have set the disable-output-escaping attribute to 'yes'. Please relook at the stylesheet logic. </p></li> <li><p>NotCreatingElementCorrectly: Creating an element node using the xsl:element instruction when could have been possible directly.</p></li> <li><p>AreYouConfusingVariableAndNode: You might be confusing a variable reference with a node reference. (contributed by, Alain Benedetti)</p></li> <li><p>IncorrectUseOfBooleanConstants: Incorrectly using the boolean constants as 'true' or 'false'. (contributed by, Tony Lavinio)</p></li> <li><p>ShortNames: Using a single character name for variable/function/template. Use meaningful names for these features.</p></li> <li><p>NameStartsWithNumeric: The variable/function/template name starts with a numeric character </p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload