gpt4 book ai didi

xslt - 寻找(伪)XSLT 预处理器/模板器以使其不那么冗长

转载 作者:行者123 更新时间:2023-11-30 23:46:22 25 4
gpt4 key购买 nike

关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

6年前关闭。




Improve this question




有人知道 XSLT 的预处理器可以让它不那么冗长吗?有点像什么 SASS是 CSS,一个将采用轻语法的小程序:

"/":
{
<html>
<head>
<title>My book collection</title>
</head>
<body>
{@ "//media"}
{@ quantity = calc_abs_value("//total_quantity")}
Price : {@ multiply(price:"//item[@selected='true'][@price]",qty :$quantity) }
</body>
</html>
}

"media[@type='book']":
{
<div id="{@id}">
{title} by {author} published in {published_date}
</div>
}

function calc_abs_value(value)
{
if ($value < 0) {- $value} else {$value}
}

function multiply(price,qty:"1")
{
switch
{
"$qty = 1" : $price * $qty
"$qty < 5" : $price * $qty * 0.95
"$price * $qty < 0" : 0
default : 0
}
}

并将其转换为可读性极低的 XSLT:
    <xsl:template match="/">
<html>
<head>
<title>My book collection</title>
</head>
<body>
<xsl:apply-templates select="//media" />
<xsl:variable name="quantity"><xsl:call-template name="calc_abs_value">
<xsl:with-param name="value"><xsl:value-of select="//total_quantity" /></xsl:with-param>
</xsl:call-template></xsl:variable>
Price : <xsl:call-template name="multiply">
<xsl:with-param name="price"><xsl:value-of select="//item[@selected='true'][@price]" /></xsl:with-param>
<xsl:with-param name="qty"><xsl:value-of select="$quantity" /></xsl:with-param>
</xsl:call-template>
</body>
</html>
</xsl:template>


<xsl:template match="media[@type='book']">
{
<div id="{@id}">
<xsl:value-of select="title" /> by <xsl:value-of select="author" /> published in <xsl:value-of select="published_date" />
</div>
}
</xsl:template>


<xsl:template name="calc_abs_value">
<xsl:param name="value" />
<xsl:choose>
<xsl:when test="$value < 0">
- <xsl:value-of select="$value" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

我懒得写乘法的 XSLT 形式,但我相信你已经明白我的意思了。

或者是否有比其他人更了解 XSLT 的 IDE,例如它显示 {author}但扩展为:
<xsl:value-of select="author" />

当你点击它?

最佳答案

你最好问:如何 DSL XSLT?

您可能对一些实际示例感兴趣,例如 YSLT基于 YML概念。

注意 最好不要转换/预处理代码以获得 XSLT 转换;相反,只需模拟处理器解析输入文档的方式并使用您自己的 DSL。

您也可以编写自己的 DSL。你应该使用一些真正让 DSL 概念变得简单的脚本语言,例如, ruby .在 ruby ,你可以实现一些非常漂亮和干净的东西,比如:

match "/" do
value-of :foo
end

这可以作为:
<xsl:template match="/">
<xsl:value-of select="foo"/>
</xsl:template>

另外值得一提的是 模板语言 喜欢 erb .这是我认为一般来说唯一可行的 XSLT 替代方案(即使这里的替代方案是一个很大的妥协,并且您很快就会因节点的程序操作而迷失方向)。

备注

我喜欢 XSLT 冗长 而且,由于这种冗长,我认为 XSLT 代码 更具可读性 然后是任何其他语言(一旦您了解模板的工作原理)。

不要建议搜索一些将一些不同的标记转换为 XSLT 代码的东西。您最终的 XSLT 代码将 可读性差 及其代码 不会是最优的 根本。

关于xslt - 寻找(伪)XSLT 预处理器/模板器以使其不那么冗长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6816770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com