1

I have a XML template with some fields predefined in it. I want to build new XML based on it template with new Value values using RewriteRules.

ex. template:

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"))

output should be:

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

Is it possible to do with RewriteRules?

1 Answers1

2

You do not need RewriteRules for this purposes. You can define variables in your xml template.

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>

otherwise Scala - replace xml element with specific text

Community
  • 1
  • 1
vvg
  • 6,325
  • 19
  • 36