Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change attribute on Scala XML Element
    primarykey
    data
    text
    <p>I have an XML file that I would like to map some attributes of in with a script. For example:</p> <pre><code>&lt;a&gt; &lt;b attr1 = "100" attr2 = "50"/&gt; &lt;/a&gt; </code></pre> <p>might have attributes scaled by a factor of two:</p> <pre><code>&lt;a&gt; &lt;b attr1 = "200" attr2 = "100"/&gt; &lt;/a&gt; </code></pre> <p>This page has a suggestion for <em>adding</em> attributes but doesn't detail a way to map a current attribute with a function (this way would make that very hard): <a href="http://www.scalaclass.com/book/export/html/1" rel="noreferrer">http://www.scalaclass.com/book/export/html/1</a></p> <p>What I've come up with is to manually create the XML (non-scala) linked-list... something like:</p> <pre><code>// a typical match case for running thru XML elements: case Elem(prefix, e, attributes, scope, children @ _*) =&gt; { var newAttribs = attributes for(attr &lt;- newAttribs) attr.key match { case "attr1" =&gt; newAttribs = attribs.append(new UnprefixedAttribute("attr1", (attr.value.head.text.toFloat * 2.0f).toString, attr.next)) case "attr2" =&gt; newAttribs = attribs.append(new UnprefixedAttribute("attr2", (attr.value.head.text.toFloat * 2.0f).toString, attr.next)) case _ =&gt; } Elem(prefix, e, newAttribs, scope, updateSubNode(children) : _*) // set new attribs and process the child elements } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload