gpt4 book ai didi

xml - 如何更改 Scala XML 元素的属性

转载 作者:数据小太阳 更新时间:2023-10-29 01:38:45 25 4
gpt4 key购买 nike

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

<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 的方法来执行此操作?

最佳答案

好的,尽力而为,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
  • Elem 有一个 mapAttribute

关于xml - 如何更改 Scala XML 元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2569580/

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