scala:将属性(奇数行和偶数行)添加到xml表 [英] scala: Adding attributes (odd and even rows) to xml table

查看:101
本文介绍了scala:将属性(奇数行和偶数行)添加到xml表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Lift应用程序中,我想添加一个特殊的标签,该标签将下一张表的<tbody>部分并为每个<tr>标签添加oddeven类(例如).当然是交替的.虽然我找到了一种向所有<tr>标签添加另一个属性的方法,但仍然存在一些问题(请参见下面的代码).

In a Lift application, I’d like to add a special tag which takes the <tbody> part of the next table and adds odd and even classes (for example) to each <tr> tag. Alternating, of course. While I have found a way to add another attribute to all <tr> tags, there are still a few problems left (see code below).

首先,它不起作用. cycle.next经常被调用,所以最后,所有内容都是odd行.其他问题是代码没有排除内部表(因此嵌套的<tr>也将被转换),并且还会影响表的<thead>部分.

First, it doesn’t work. cycle.next is called too often, so in the end, everything is an odd row. Other problems are that the code doesn’t exclude inner tables (so a nested <tr> would be transformed as well) and that it also affects the <thead> part of the table.

使此代码有效的想法? (当然,如果为此已经有了基于升降机的解决方案,而没有jQuery,我将不胜感激.)

Ideas to make this code work? (Of course, if there already is a lift-based solution – without jQuery – for this, I’ll gratefully take it.)

// Helper class for looping
class Loop(val strs: String*) {
    val stream_iter = Stream.continually(strs.toStream).flatten.iterator
    def next = stream_iter.next
}

val cycle = new Loop("even", "odd")

val rr = new RewriteRule {
  override def transform(n: Node): Seq[Node] = n match {
    // match any Elem
    case elem : Elem => elem match {
        // If it’s a <tr> do some further processing
        case Elem(_, "tr", att @ _, _, _*) => 
            elem % Attribute(None, "class", Text(
                // add the attribute and concatenate with others
                List(att.get("class").getOrElse("").toString, cycle.next).reduceLeft(_+" "+_).trim
                ), Null) toSeq
        case other => other
    }
    case other => other
  }
}

val rt = new RuleTransformer(rr)

val code = <table>
  <thead><tr><td>Don’t</td><td>transform this</td></tr></thead>
  <tbody>
    <tr class="otherclass">
      <td>r1c1</td><td>r1c2</td>
    </tr>
    <tr>
      <td>r2c1</td><td>r2c2</td>
    </tr>
    <tr>
      <td>r3c1</td><td>r3c2</td>
    </tr>
  </tbody>
</table>

println(rt(code))

推荐答案

RewriteRule的问题似乎在于它们嵌套得太深了.即,一旦启动了向<tr>添加属性的规则,就无法停止它. (至少,它对我不起作用.)但是,我找到了对我有效的递归解决方案.另外,只要内部有 ,递归就会提前停止.如果没有,我们可能会遇到问题...

The problem with RewriteRules seems to be that they nest too deeply. That is, once a rule for adding attributes to <tr> is started, it is not possible to stop it. (At least, it did not work for me.) However, I have found a recursive solution which works for me. Also, as long as there is a <tbody> inside, the recursion will stop early. If there isn’t, we might have a problem…

abstract class Loop {
    val stream_iter = Stream.continually(elems.toStream).flatten.iterator
    def next = stream_iter.next
    def elems: Seq[String]
}
class Cycle extends Loop { override def elems = List("odd", "even") }

// Call this when in <tbody>
def transformChildren(sn: Seq[Node]): Seq[Node] = {
    // Start a new cycle
    val cycle = new Cycle
    sn.map{ node => node match {
        case Elem(prefix, "tr", att, scope, ch @ _*) => 
            Elem(prefix, "tr", att, scope, ch:_*) % Attribute(None, "class", Text(
                List(att.get("class").getOrElse("").toString, cycle.next).reduceLeft(_+" "+_).trim
                ), Null)
        case other => other
        }
    }
}

// Look for first <tbody>, transform child tr elements and stop recursion
// If no <tbody> found, recurse
def recurse(sn: NodeSeq): NodeSeq = sn.map{ node =>
    node match {
        case Elem(prefix, "tbody", att, scope, ch @ _*)
            => Elem(prefix, "tbody", att, scope, transformChildren(ch):_*)
        case Elem(prefix, label, att, scope, ch @ _*)
            => Elem(prefix, label, att, scope, recurse(ch):_*)
        case other => other
    }
}

val code = <table>
  <thead><tr><td>Don’t</td><td>transform this</td></tr></thead>
  <tbody>
    <tr class="otherclass">
      <td>r1c1</td><td>r1c2</td>
    </tr>
    <tr>
      <td><table><tbody><tr><td>Neither this.</td></tr></tbody></table></td><td>r2c2</td>
    </tr>
    <tr>
      <td>r3c1</td><td>r3c2</td>
    </tr>
  </tbody>
</table>

println(recurse(code))

赠予:

<table>
  <thead><tr><td>Don’t</td><td>transform this</td></tr></thead>
  <tbody>
    <tr class="otherclass odd">
      <td>r1c1</td><td>r1c2</td>
    </tr>
    <tr class="even">
      <td><table><tbody><tr><td>Neither this</td></tr></tbody></table></td><td>r2c2</td>
    </tr>
    <tr class="odd">
      <td>r3c1</td><td>r3c2</td>
    </tr>
  </tbody>
</table>

这篇关于scala:将属性(奇数行和偶数行)添加到xml表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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