2015-11-06 35 views
1

我有一個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

回答

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> 

否則 Scala - replace xml element with specific text