带重写规则的Scala XML转换
问题描述:
我有一个XML模板,其中预定义了一些字段。我想使用RewriteRules
基于它创建新的XML,并使用新的Value
值。带重写规则的Scala XML转换
ex。 模板:
val template = <xml>
<Persons>
<Name>Persons</Name>
<Person>
<FullName>
<Name>Name of the field</Name>
<Value></Value>
</FullName>
<LastName>
<Name>Name of the field</Name>
<Value></Value>
</LastName>
</Person>
</Persons>
</xml>
case class Person(fullName: String, lastName: String)
val persons = Seq(Person("John Smith", "Smith"), Person("Bob Saver", "Saver"))
输出应该是:
<xml>
<Persons>
<Name>Persons</Name>
<Person>
<FullName>
<Name>Name of the field</Name>
<Value>John Smith</Value>
</FullName>
<LastName>
<Name>Name of the field</Name>
<Value>Smith</Value>
</LastName>
</Person>
<Person>
<FullName>
<Name>Name of the field</Name>
<Value>Bob Saver</Value>
</FullName>
<LastName>
<Name>Name of the field</Name>
<Value>Saver</Value>
</LastName>
</Person>
</Persons>
</xml>
是否有可能做RewriteRules
?
答
为此您不需要RewriteRules
。你可以在你的xml模板中定义变量。
scala> def template(id: String = "defaultValue can be here") = <someXml>{id}</someXml>
template: (id: String)scala.xml.Elem
scala> template("person")
res4: scala.xml.Elem = <someXml>person</someXml>
scala> template("person2")
res5: scala.xml.Elem = <someXml>person2</someXml>