After a long learning loop via XAML, I've returned to HTML and javascript, and realised that the concept of declarative code - in terms of rules for transformation - is an incredibly powerful concept.
Despite its surfeit of syntax, XSLT processing of XML is the keystone of declarative transformation programming. I have, however, always found it hard to understand how XSLT would be used for everyday tasks (with XML).
What are some good examples of XSLT elegantly solving a programming problem, outside of generating HTML?
I'm guessing it's good at graph transformation and data reprocessing...
Edit: I'm hoping for some actual examples - one of the things that puts me off full-on xslt is the visual complexity of the code.
One can often find examples of beautiful XSLT code, especially when XSLT is used as a functional programming language.
For examples see this article on FXSL 2.0 -- the Functional Programming library for XSLT 2.0.
As an FP language XSLT is also a declarative language. This, among other things means that one declares, specifies existing relationships.
Such 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.
Here is a small example.
This XPath 2.0 expression defines the "Maximum Prime Factor of a natural number":
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)))
To pronounce it in English, the maximum prime factor of a number pNum
is the number itself, if pNum
is prime, otherwise if vDiv1
and vDiv2
are two factors of pNum
, then the maximum prime factor of pNum
is the bigger of the maximum prime factors of vDiv1
and vDiv2
.
How do we use this to actually calculate the Maximum Prime Factor in XSLT? We simply wrap up the definition above in an <xsl:function>
and ... get the result!
<xsl:function name="f:maxPrimeFactor" as="xs:integer">
<xsl:param name="pNum" as="xs:integer"/>
<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)))
"/>
</xsl:function>
We can, then, calculate the MPF for any natural number, for example:
f:maxPrimeFactor(600851475143)
= 6857
As for efficiency, well, this transformation takes just 0.109 sec.
Other examples of both ellegant and efficient XSLT code:
- Tim Bray's Wide Finder, as solved here.
- Cascade deletions
- Transitive closure
- Finding all anagrams of a word
- Concordance of a text corpus (the Old Testament)
- Spelling checking (Shakespear's Othello)
- Sudoku solver
- A general compiler-compiler system -- the LR-Parsing Framework of FXSL, used successfully for a parser of JSON and XPath2.0.
How about code generation? I use this in protobuf-net to create the .cs files from a .proto model (exported as xml) - like so. In .NET 4.0, T4 templates would be a more obvious choice, but the T4 engine isn't standalone (AFAIK) before then.
Xslt is also used extensively to transform between different xml layouts - for example, in middleware tools like biztalk.
I used XSLT the other day. I needed to provide a way to let the user save the contents of a tree control to a file so they could email it to their boss, show it to their grandchildren, etc.
It was requested that they be able to save it as an HTML file with a nice tidy look to it, or as XML so they could process it further however they wished.
So I suggested that we output XML with an XSLT linked to it, so that way they have the raw XML but it looks nice when they open it in the browser.
It's not ideal (if they move the HTML somewhere else without the XSL, IE refuses to display it at all!) but if we change plans, I can just modify the product so it will let the user choose the format, and if they choose HTML, all I have to do is run the XSLT on the XML to get the HTML before I save it.
Ideally there would be a way to make a self-contained HTML file that contained XML and XSLT, but I'm not aware of one... which is a question in itself.
来源:https://stackoverflow.com/questions/693596/elegant-examples-of-xslt