如何在Scala XML元素上更改属性

21

我有一个XML文件,我想用脚本映射其中的一些属性。例如:

<a>
  <b attr1 = "100" attr2 = "50"/>
</a>

可能会有属性按两倍比例缩放:

<a>
  <b attr1 = "200" attr2 = "100"/>
</a>

这个页面提供了一些建议来添加属性,但没有详细介绍如何将当前的属性映射到一个函数(这将使这个过程非常困难):http://www.scalaclass.com/book/export/html/1

我想到的方法是手动创建XML(非Scala)链表......类似于:

// a typical match case for running thru XML elements:
case  Elem(prefix, e, attributes, scope, children @ _*) => {
 var newAttribs = attributes
 for(attr <- newAttribs)  attr.key match {
  case "attr1" => newAttribs = attribs.append(new UnprefixedAttribute("attr1", (attr.value.head.text.toFloat * 2.0f).toString, attr.next))
  case "attr2" => newAttribs = attribs.append(new UnprefixedAttribute("attr2", (attr.value.head.text.toFloat * 2.0f).toString, attr.next))
  case _ =>
 }
 Elem(prefix, e, newAttribs, scope, updateSubNode(children) : _*)  // set new attribs and process the child elements
}

它太丑陋,过于冗长,而且在输出中不必要地重新排列属性,这对我的当前项目很糟糕,因为有一些糟糕的客户端代码。是否有类似Scala的方法可以解决这个问题?


14
我对这个图书馆在这方面的表现感到非常失望。 - Daniel C. Sobral
这里有许多好的答案。还可以查看https://dev59.com/LmnWa4cB1Zd3GeqP2Zwg#23092226 - Philippe
5个回答

15

好的,尽力而为,使用Scala 2.8。我们需要重建属性,这意味着我们必须正确地分解它们。让我们为此创建一个函数:

import scala.xml._

case class GenAttr(pre: Option[String], 
                   key: String, 
                   value: Seq[Node], 
                   next: MetaData) {
  def toMetaData = Attribute(pre, key, value, next)
}

def decomposeMetaData(m: MetaData): Option[GenAttr] = m match {
  case Null => None
  case PrefixedAttribute(pre, key, value, next) => 
    Some(GenAttr(Some(pre), key, value, next))
  case UnprefixedAttribute(key, value, next) => 
    Some(GenAttr(None, key, value, next))
}

接下来,让我们将链式属性分解为序列:

def unchainMetaData(m: MetaData): Iterable[GenAttr] = 
  m flatMap (decomposeMetaData)

此时,我们可以轻松地操作这个列表:

def doubleValues(l: Iterable[GenAttr]) = l map {
  case g @ GenAttr(_, _, Text(v), _) if v matches "\\d+" => 
    g.copy(value = Text(v.toInt * 2 toString))
  case other => other
}

现在,再次将它链接起来:

def chainMetaData(l: Iterable[GenAttr]): MetaData = l match {
  case Nil => Null
  case head :: tail => head.copy(next = chainMetaData(tail)).toMetaData
}

现在,我们只需要创建一个函数来处理这些事情:

def mapMetaData(m: MetaData)(f: GenAttr => GenAttr): MetaData = 
  chainMetaData(unchainMetaData(m).map(f))

因此,我们可以像这样使用它:

import scala.xml.transform._

val attribs = Set("attr1", "attr2")
val rr = new RewriteRule {
  override def transform(n: Node): Seq[Node] = (n match {
    case e: Elem =>
      e.copy(attributes = mapMetaData(e.attributes) {
        case g @ GenAttr(_, key, Text(v), _) if attribs contains key =>
          g.copy(value = Text(v.toInt * 2 toString))
        case other => other
      })
    case other => other
  }).toSeq
}
val rt = new RuleTransformer(rr)

这最终让你实现了你想要的翻译:

rt.transform(<a><b attr1="100" attr2="50"></b></a>)

如果能满足以下条件,所有的这一切都可以简化:

  • 属性确切地定义了前缀、键和值,并且具有可选的前缀
  • 属性是一个序列,而不是链式结构
  • 属性具有映射、mapKeys 和 mapValues
  • 元素具有 mapAttribute

1
这个库的设计似乎做出了一些奇怪的选择。你想出了比我更通用的东西,所以...为此得分。 - Dave
1
我在Scala 2.9.1中尝试过这个。有一些小事情:似乎包装RewriteRule的.toSeq是多余的,因为Node是Seq [Node]。另外,属性最终被颠倒了。 - David Leppik
1
这是我的修改版:def unchainMetaData(m: MetaData): Iterable[GenAttr] = m.flatMap(decomposeMetaData).toList.reverse - David Leppik

12

这是使用Scala 2.10的方法:

import scala.xml._
import scala.xml.transform._

val xml1 = <a><b attr1="100" attr2="50"></b></a>

val rule1 = new RewriteRule {
  override def transform(n: Node) = n match {
    case e @ <b>{_*}</b> => e.asInstanceOf[Elem] % 
      Attribute(null, "attr1", "200", 
      Attribute(null, "attr2", "100", Null))
    case _ => n 
  }
}

val xml2 = new RuleTransformer(rule1).transform(xml1)

10

所以如果我处于你的位置,我想我真正想要写的内容应该是这样的:

case elem: Elem => elem.copy(attributes=
  for (attr <- elem.attributes) yield attr match {
    case attr@Attribute("attr1", _, _) =>
      attr.copy(value=attr.value.text.toInt * 2)
    case attr@Attribute("attr2", _, _) =>
      attr.copy(value=attr.value.text.toInt * -1)
    case other => other
  }
)

这段代码不能直接运行,原因有两个:

  1. Attribute 没有一个有用的 copy 方法。
  2. 对于 MetaData 的映射将产生一个 Iterable[MetaData] 而不是一个 MetaData,所以即使是像 elem.copy(attributes=elem.attributes.map(x => x)) 这样简单的语句也会失败。

为了解决第一个问题,我们将使用隐式转换为 Attribute 添加更好的 copy 方法:

implicit def addGoodCopyToAttribute(attr: Attribute) = new {
  def goodcopy(key: String = attr.key, value: Any = attr.value): Attribute =
    Attribute(attr.pre, key, Text(value.toString), attr.next)
}

由于已经存在一个名为copy的方法,因此它不能被命名为copy,所以我们将其称为goodcopy。(另外,如果您正在创建值,这些值应转换为字符串而不是Seq[Node],则可以稍微注意一下value,但对于我们当前的目的来说,这并不是必要的。)

为了解决第二个问题,我们将使用隐式函数来解释如何从Iterable [MetaData]创建MetaData

implicit def iterableToMetaData(items: Iterable[MetaData]): MetaData = {
  items match {
    case Nil => Null
    case head :: tail => head.copy(next=iterableToMetaData(tail))
  }
}

那么你可以编写类似于我一开始提出的代码:

scala> val elem = <b attr1 = "100" attr2 = "50"/>
elem: scala.xml.Elem = <b attr1="100" attr2="50"></b>

scala> elem.copy(attributes=
     |   for (attr <- elem.attributes) yield attr match {
     |     case attr@Attribute("attr1", _, _) =>
     |       attr.goodcopy(value=attr.value.text.toInt * 2)
     |     case attr@Attribute("attr2", _, _) =>
     |       attr.goodcopy(value=attr.value.text.toInt * -1)
     |     case other => other
     |   }
     | )
res1: scala.xml.Elem = <b attr1="200" attr2="-50"></b>

1

借助Scalate's Scuery及其CSS3选择器和变换,

def modAttr(name: String, fn: Option[String] => Option[String])(node: Node) = node match {
  case e: Elem =>
    fn(e.attribute(name).map(_.toString))
      .map { newVal => e % Attribute(name, Text(newVal), e.attributes.remove(name)) }
      .getOrElse(e)
}

$("#foo > div[bar]")(modAttr("bar", _ => Some("hello")))

— 这将转换例如这个

<div id="foo"><div bar="..."/></div>

转换为

<div id="foo"><div bar="hello"/></div>`

1
Scuery现在在github上存储(http://scalate.github.io/scalate/documentation/scuery.html)。 - millhouse
谢谢你,好消息是它还活着;我已经更新了答案。 - Erik Kaplun
太遗憾了,问题跟踪器仍停留在Assembla上 - 它本可以迁移到Github! - Erik Kaplun

0
我发现创建一个单独的XML片段并合并更容易。这个代码片段还演示了如何删除元素、添加额外的元素以及在XML文字中使用变量:
val alt = orig.copy(
  child = orig.child.flatMap {
    case b: Elem if b.label == "b" =>
      val attr2Value = "100"
      val x = <x attr1="200" attr2={attr2Value}/>  //////////////////// Snippet
      Some(b.copy(attributes = b.attributes.append(x.attributes)))

    // Will remove any <remove-me some-attrib="specific value"/> elems
    case removeMe: Elem if isElem(removeMe, "remove-me", "some-attrib" -> "specific value") => 
      None

    case keep => Some(keep)
  }
    ++
      <added-elem name="..."/>

// Tests whether the given element has the given label
private def isElem(elem: Elem, desiredLabel: String, attribValue: (String, String)): Boolean = {
  elem.label == desiredLabel && elem.attribute(attribValue._1).exists(_.text == attribValue._2)
}

对于其他初学者来说,如果想在 Scala 代码中使用 XML,你还需要添加一个单独的 Scala 模块


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接