寻找一个(伪)XSLT 预处理器/模板以使其不那么冗长 [英] Looking for a (pseudo) XSLT preprocessor/templater to make it less verbose

查看:14
本文介绍了寻找一个(伪)XSLT 预处理器/模板以使其不那么冗长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道 XSLT 的预处理器可以使它不那么冗长?类似于 SASS 之于 CSS,有点前卫,需要简单的语法:

Does anybody know of a preprocessor for XSLT to make it less verbose? Something like what SASS is to CSS, a little proggy that will take light syntax:

"/":
{
 <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:

and turn this into far-less readable 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 形式的乘法,但我相信你已经明白我的意思了.

I am too lazy to write the XSLT form of multiply but I'm sure you already figured out what I mean.

或者是否有一个 IDE 比其他 IDE 更能识别 XSLT,例如它显示 {author} 但扩展为:

Or is there an IDE that is more XSLT aware than others, e.g it displays {author} but expands to:

<xsl:value-of select="author" />

什么时候点击?

推荐答案

你最好问:如何DSL XSLT?

您可能对一些实际示例感兴趣,例如 YSLTYML 概念.

You might be interested in some practical example such as YSLT based on YML concept.

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

NOTE It should be better not to convert/preprocess the code to get an XSLT transform at all; instead, just emulate the way a processor parses the input document and use your own DSL.

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

You can also write your own DSL. You should use some scripting language which really makes DSL concept easy, as, for example, ruby. In ruby, you can achieve something very nice and clean like:

match "/" do
 value-of :foo
end

这可以作为:

<xsl:template match="/">
 <xsl:value-of select="foo"/>
</xsl:template>

<小时>

另一件值得一提的是模板语言,例如erb.这是我认为一般 XSLT 唯一可行的替代方案(即使这里的替代方案是一个很大的妥协,并且您很快就会迷失在节点的过程操作中).


Another thing that worth to be mentioned is templating languages like erb. This is I think the only feasible alternative to XSLT in general (even if alternative here is a big compromise and you get lost very soon with procedural manipulation of nodes).

注意

我喜欢 XSLT 冗长,并且由于这种冗长,我认为 XSLT 代码比任何其他语言都更具可读性(一旦你了解模板的工作原理).

I love XSLT verbosity and, because of this verbosity, I think XSLT code is much more readable then any other language (once you understand how templates work).

建议搜索一些东西来将一些不同的标记转换为 XSLT 代码.您的最终 XSLT 代码将缺乏可读性,并且它的代码根本不会是最佳的.

I don't recommend to search something for converting some different markup to XSLT code. Your final XSLT code will loose of readbility and its code will not be optimal at all.

这篇关于寻找一个(伪)XSLT 预处理器/模板以使其不那么冗长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆