xslt的优雅示例? [英] Elegant examples of xslt?
问题描述
在通过XAML进行了漫长的学习循环之后,我回到了HTML和javascript,并意识到声明性代码的概念(就转换规则而言)是一个非常强大的概念.
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.
尽管XSLT语法过多,但XML的处理却是声明式转换编程的基石.但是,我总是很难理解如何将XSLT用于日常任务(使用XML).
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).
除了生成HTML之外,XSLT还可以通过哪些优雅的示例很好地解决编程问题?
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...
我希望有一些实际的例子-使我无法使用完整的xslt的原因之一是代码的视觉复杂性.
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.
推荐答案
人们经常可以找到漂亮的XSLT代码示例,尤其是当XSLT用作功能性编程语言时.
有关示例,请参见 此> FXSL 2.0 上的文章 -函数式编程XSLT 2.0的库.
For examples see this article on FXSL 2.0 -- the Functional Programming library for XSLT 2.0.
作为FP语言,XSLT也是 声明性语言 .除其他外,这意味着一个人声明了现有的关系.
As an FP language XSLT is also a declarative language. This, among other things means that one declares, specifies existing relationships.
这样的定义通常不需要任何其他代码即可产生结果-它本身是其自己的实现或可执行的定义或可执行的规范.
这是一个小例子.
此XPath 2.0表达式定义了"最大 Prime Factor 的自然数:
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)))
要用英语发音,如果 pNum
为质数,否则,如果 vDiv1
和 vDiv2
是 pNum
的两个因数,则最大质数为 pNum
是 vDiv1
和 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
.
我们如何使用它来实际计算 XSLT中的最大素数? 我们只需将上面的定义包裹在一个<xsl:function>
中,然后得到结果!
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>
然后,我们可以计算任何自然数的MPF ,例如:
We can, then, calculate the MPF for any natural number, for example:
f:maxPrimeFactor(600851475143)
= 6857
关于效率,此转换仅需0.109秒.
简洁高效的XSLT代码的其他示例:
- Tim Bray 的 此处 .
- 级联 删除
- 传递闭包
- 查找所有字谜 一个单词
- > 语料库(旧约)的一致性
- 拼写检查 (莎士比亚的《奥赛罗》)
- 数独求解器
- 通用的编译器系统- JSON解析器 和XPath2 .0.
- 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.
这篇关于xslt的优雅示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!