1

我有一个 XML 模板,其中预定义了一些字段。我想基于它使用新Value值的模板构建新的 XML RewriteRules

前任。模板:

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吗?

4

1 回答 1

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 - 用特定文本替换 xml 元素

于 2015-11-06T15:14:50.513 回答