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

  • Home
  • SEARCH
  • 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 409955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:50:28+00:00 2026-05-12T17:50:28+00:00

I want to be able to do this: val myXml = <myTag { someAttributes

  • 0

I want to be able to do this:

val myXml = <myTag { someAttributes }> </myTag>

(because I don’t know what the attribute details are at compile time)

and this:

val myXml = <{someTag}></{someTag}>

This isn’t valid Scala syntax. The closest I can come is using the Elem object to construct elements, but it’s being a little troublesome (inserting PCDATA where I don’t want it to). Is there any way of doing it like the above?

  • 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-12T17:50:28+00:00Added an answer on May 12, 2026 at 5:50 pm
    val myXml = <myTag/> % Attribute(None, "name", Text("value"), Null)
    

    See scala.xml.Attribute for different constructors.

    Adding the same attribute to all children:

    scala> val xml = <root><a/><b/><c/></root>
    xml: scala.xml.Elem = <root><a></a><b></b><c></c></root>
    
    scala> xml.child map (_ match {
         | case elem : Elem => elem % Attribute(None, "name", Text("value"), Null)
         | case x => x
         | })
    res3: Sequence[scala.xml.Node] = ArrayBuffer(<a name="value"></a>, <b name="value"></b>, <c name="value"></c>)
    

    You can also use the stuff in scala.xml.transform to do so recursively to all XML:

    val rr = new RewriteRule {
      override def transform(n: Node): Seq[Node] = n match {
        case elem : Elem => elem % Attribute(None, "name", Text("value"), Null) toSeq
        case other => other
      }
    }
    
    val rt = new RuleTransformer(rr)
    
    scala> rt(xml)
    res5: scala.xml.Node = <root name="value"><a name="value"></a><b name="value"></b><c name="value"></c></root>
    

    Or you can add attributes to arbitrary parts of the xml:

    scala> val xml = <root>{<a/> % Attribute(None, "name", Text("value"), Null)}</root>
    xml: scala.xml.Elem = <root><a name="value"></a></root>
    

    EDIT

    Changing the name is easy to do on Scala 2.8, like this:

    val someTag = "tag"
    val myXml = <root>{<a/>.copy(label = someTag)}</root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my DSL I want to be able to do like this: val a
I want to be able to automate this so I can go to one
I want to be able to do this: the TextView to change its size
I want to be able to click the button and have it copy this:
I want to be able to parse file paths like this one: /var/www/index.(htm|html|php|shtml) into
I want to be able to get something similar to this.. I want to
I want my perl script to be able to do this: $ ./code file.txt
I want to be able to capture images from a webcam on Linux. This
I want to be able to replace style=&quot;STUFF&quot; I keep thinking that this is
I have this so far: http://jsfiddle.net/u5vhS/54/ . What I want to be able to

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.