我有一个XML模板,其中预定义了一些字段。我想使用Value
以新的RewriteRules
值为基础构建基于其模板的新XML。
离。 模板:
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
?
答案 0 :(得分:2)
出于此目的,您不需要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>