Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 703935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:53:09+00:00 2026-05-14T03:53:09+00:00

I have an XML file that I would like to map some attributes of

  • 0

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

<a>
  <b attr1 = "100" attr2 = "50"/>
</a>

might have attributes scaled by a factor of two:

<a>
  <b attr1 = "200" attr2 = "100"/>
</a>

This page has a suggestion for adding attributes but doesn’t detail a way to map a current attribute with a function (this way would make that very hard):
http://www.scalaclass.com/book/export/html/1

What I’ve come up with is to manually create the XML (non-scala) linked-list… something like:

// a typical match case for running thru XML elements:
case  Elem(prefix, e, attributes, scope, children @ _*) => {
 var newAttribs = attributes
 for(attr <- newAttribs)  attr.key match {
  case "attr1" => newAttribs = attribs.append(new UnprefixedAttribute("attr1", (attr.value.head.text.toFloat * 2.0f).toString, attr.next))
  case "attr2" => newAttribs = attribs.append(new UnprefixedAttribute("attr2", (attr.value.head.text.toFloat * 2.0f).toString, attr.next))
  case _ =>
 }
 Elem(prefix, e, newAttribs, scope, updateSubNode(children) : _*)  // set new attribs and process the child elements
}

Its hideous, wordy, and needlessly re-orders the attributes in the output, which is bad for my current project due to some bad client code. Is there a scala-esque way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:53:09+00:00Added an answer on May 14, 2026 at 3:53 am

    Ok, best effort, Scala 2.8. We need to reconstruct attributes, which means we have to decompose them correctly. Let’s create a function for that:

    import scala.xml._
    
    case class GenAttr(pre: Option[String], 
                       key: String, 
                       value: Seq[Node], 
                       next: MetaData) {
      def toMetaData = Attribute(pre, key, value, next)
    }
    
    def decomposeMetaData(m: MetaData): Option[GenAttr] = m match {
      case Null => None
      case PrefixedAttribute(pre, key, value, next) => 
        Some(GenAttr(Some(pre), key, value, next))
      case UnprefixedAttribute(key, value, next) => 
        Some(GenAttr(None, key, value, next))
    }
    

    Next, let’s decompose the chained attributes into a sequence:

    def unchainMetaData(m: MetaData): Iterable[GenAttr] = 
      m flatMap (decomposeMetaData)
    

    At this point, we can easily manipulate this list:

    def doubleValues(l: Iterable[GenAttr]) = l map {
      case g @ GenAttr(_, _, Text(v), _) if v matches "\\d+" => 
        g.copy(value = Text(v.toInt * 2 toString))
      case other => other
    }
    

    Now, chain it back again:

    def chainMetaData(l: Iterable[GenAttr]): MetaData = l match {
      case Nil => Null
      case head :: tail => head.copy(next = chainMetaData(tail)).toMetaData
    }
    

    Now, we only have to create a function to take care of these things:

    def mapMetaData(m: MetaData)(f: GenAttr => GenAttr): MetaData = 
      chainMetaData(unchainMetaData(m).map(f))
    

    So we can use it like this:

    import scala.xml.transform._
    
    val attribs = Set("attr1", "attr2")
    val rr = new RewriteRule {
      override def transform(n: Node): Seq[Node] = (n match {
        case e: Elem =>
          e.copy(attributes = mapMetaData(e.attributes) {
            case g @ GenAttr(_, key, Text(v), _) if attribs contains key =>
              g.copy(value = Text(v.toInt * 2 toString))
            case other => other
          })
        case other => other
      }).toSeq
    }
    val rt = new RuleTransformer(rr)
    

    Which finally let you do the translation you wanted:

    rt.transform(<a><b attr1="100" attr2="50"></b></a>)
    

    All of this could be simplified if:

    • Attribute actually defined prefix, key and value, with an optional prefix
    • Attribute was a sequence, not a chain
    • Attribute had a map, mapKeys, mapValues
    • Elem had a mapAttribute
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file that contains in products. File's structure this: <?xml version=1.0
I have to resolve a problem close to parsing a huge file like, 3
I'm trying to build a map of trails around my town. I'm using an
What I'm looking to do is to sorta a Java List or Map in
What I'm looking to do is to sorta a Java List or Map in
It is tempting, if the only tool you have is a hammer, to treat
i have the following function, which doe a basic job of mapping an lxml
I have a little problem... I have made an Android application which extends the
Currently I am careful to use different IDs for each widget, but if I
I started using NHibernate today, but I cannot figure out how I setup a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.