Scala XML - passing down values using the .map method in XML -
using scala, have code:
def insertrefintoxml(ref: int, entry: node): node = entry match { case <root>{ mainroot @ _* }</root> => <root>{ mainroot.map(insertrefintoxml ref )}</root> case <here>{ contents }</here> => <here>{ ref }</here> case other @ _ => other }
where want keep on passing "ref" value on down until here element, , swap in.
this doesn't work. will?
check this link originating question
see if works you:
object testxml { def main(args: array[string]) { val xml = <root> <here> <dealid>foo</dealid> </here> </root> println(insertrefintoxml(2, xml)) } def insertrefintoxml(ref: int, entry: node): node = { def doinsertref(n:node):node = { n match { case <root>{ mainroot @ _* }</root> => <root>{ mainroot.map(doinsertref)}</root> case <here><dealid>{ contents }</dealid></here> => <here><dealid>{ ref }</dealid></here> case other @ _ => other } } doinsertref(scala.xml.utility.trim(entry)) } } there couple of issues. first, in order use insertrefintoxml in call map in way wanted to, need have 1 arg , not two. fix that, created local function def , value ref in there via closure. solve problem instead:
def insertrefintoxml(ref: int, entry: node): node = { entry match { case <root>{ mainroot @ _* }</root> => <root>{ mainroot.map(insertrefintoxml(ref, _))}</root> case <here><dealid>{ contents }</dealid></here> => <here><dealid>{ ref }</dealid></here> case other @ _ => other } } and call this:
insertrefintoxml(2, scala.xml.utility.trim(xml)) which brings me second issue. i'm trimming match statements match correctly. when run code, believe gives output desire.
Comments
Post a Comment