How to change attribute on Scala XML Element

后端 未结 5 795
渐次进展
渐次进展 2020-12-13 04:41

I have an XML file that I would like to map some attributes of in with a script. For example:


  
         


        
5条回答
  •  心在旅途
    2020-12-13 05:23

    With the help of Scalate's Scuery and its CSS3 selectors and transforms:

    def modAttr(name: String, fn: Option[String] => Option[String])(node: Node) = node match {
      case e: Elem =>
        fn(e.attribute(name).map(_.toString))
          .map { newVal => e % Attribute(name, Text(newVal), e.attributes.remove(name)) }
          .getOrElse(e)
    }
    
    $("#foo > div[bar]")(modAttr("bar", _ => Some("hello")))
    

    — this transforms e.g. this

    into

    `

提交回复
热议问题