我有一个XML文件,我想用脚本映射in的一些属性.例如:
<a>
<b attr1 = "100" attr2 = "50"/>
</a>
Run Code Online (Sandbox Code Playgroud)
可能有两个缩放的属性:
<a>
<b attr1 = "200" attr2 = "100"/>
</a>
Run Code Online (Sandbox Code Playgroud)
此页面提供了添加属性的建议,但没有详细说明使用函数映射当前属性的方法(这种方式会非常困难):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
}
Run Code Online (Sandbox Code Playgroud)
它的丑陋,冗长,不必要地重新排序输出中的属性,由于一些糟糕的客户端代码,这对我当前的项目是不利的.有一种scala式的方式吗?
Dan*_*ral 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))
}
Run Code Online (Sandbox Code Playgroud)
接下来,让我们将链接的属性分解为一个序列:
def unchainMetaData(m: MetaData): Iterable[GenAttr] =
m flatMap (decomposeMetaData)
Run Code Online (Sandbox Code Playgroud)
此时,我们可以轻松操作此列表:
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
}
Run Code Online (Sandbox Code Playgroud)
现在,再次链接它:
def chainMetaData(l: Iterable[GenAttr]): MetaData = l match {
case Nil => Null
case head :: tail => head.copy(next = chainMetaData(tail)).toMetaData
}
Run Code Online (Sandbox Code Playgroud)
现在,我们只需要创建一个函数来处理这些事情:
def mapMetaData(m: MetaData)(f: GenAttr => GenAttr): MetaData =
chainMetaData(unchainMetaData(m).map(f))
Run Code Online (Sandbox Code Playgroud)
所以我们可以像这样使用它:
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)
Run Code Online (Sandbox Code Playgroud)
最后让你做你想要的翻译:
rt.transform(<a><b attr1="100" attr2="50"></b></a>)
Run Code Online (Sandbox Code Playgroud)
如果符合以下条件,所有这些都可以
Mag*_*nov 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)
Run Code Online (Sandbox Code Playgroud)
Ste*_*eve 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
}
)
Run Code Online (Sandbox Code Playgroud)
这有两个原因:开箱即用:
Attribute没有一个有用的copy方法,和MetaData产生一个Iterable[MetaData]而不是一个MetaData简单的事情elem.copy(attributes=elem.attributes.map(x => x))就会失败.要解决第一个问题,我们将使用隐式方法将更好的复制方法添加到Attribute:
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)
}
Run Code Online (Sandbox Code Playgroud)
copy由于具有该名称的方法已经存在,因此无法命名,因此我们只需调用它即可goodcopy.(另外,如果您创建的值Seq[Node]不是应该转换为字符串的值,那么您可能会更加小心value,但对于我们当前的目的,它不是必需的.)
为了解决第二个问题,我们将使用一个隐式来解释如何创建一个MetaData来自Iterable[MetaData]:
implicit def iterableToMetaData(items: Iterable[MetaData]): MetaData = {
items match {
case Nil => Null
case head :: tail => head.copy(next=iterableToMetaData(tail))
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以编写代码,就像我在开头提出的那样:
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9617 次 |
| 最近记录: |