Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><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> <li>A general compiler-compiler system -- <a href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/func-lrParse.xsl?revision=1.7&amp;view=markup" rel="nofollow noreferrer"><strong>the LR-Parsing Framework of FXSL</strong></a>, used successfully for <a href="http://fxsl.cvs.sourceforge.net/viewvc/fxsl/fxsl-xslt2/f/func-json-document.xsl?revision=1.11&amp;view=markup" rel="nofollow noreferrer"><strong>a parser of JSON</strong></a> and XPath2.0.</li> </ul>
    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. 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.
 

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