Quantcast

XML element with optional attribute

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

XML element with optional attribute

Russ P.
Suppose I am printing out an XML element like this:

  def toXML: xml.Elem = {

    <waypt name={name}>
      ...
      </waypt>
    }

The "name" attribute is a String and is optional (some waypoints have names and some don't). I want to show it only if it is nonempty. In other words, I don't want to see name=""

I just want the name attribute completely omitted if the name is an empty String.

How can I do that in the toXML method above? Thanks.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML element with optional attribute

Naftoli Gugenheim
I don't know if there's a better solution, but Elem is a case class so you can add the attribute on afterwards using the copy method.

On Fri, Mar 24, 2017 at 2:01 AM Russ P. <[hidden email]> wrote:
Suppose I am printing out an XML element like this:

  def toXML: xml.Elem = {

    <waypt name={name}>
      ...
      </waypt>
    }

The "name" attribute is a String and is optional (some waypoints have names and some don't). I want to show it only if it is nonempty. In other words, I don't want to see name=""

I just want the name attribute completely omitted if the name is an empty String.

How can I do that in the toXML method above? Thanks.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML element with optional attribute

Daniel Manchester
In reply to this post by Russ P.
Hi,

If you make "name" an Option[scala.xml.Text], I think the XML will be generated as you desire.

Dan


On Friday, March 24, 2017 at 2:01:33 AM UTC-4, Russ P. wrote:
Suppose I am printing out an XML element like this:

  def toXML: xml.Elem = {

    <waypt name={name}>
      ...
      </waypt>
    }

The "name" attribute is a String and is optional (some waypoints have names and some don't). I want to show it only if it is nonempty. In other words, I don't want to see name=""

I just want the name attribute completely omitted if the name is an empty String.

How can I do that in the toXML method above? Thanks.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: XML element with optional attribute

Russ P.
Nice. Thanks!


On Tue, Mar 28, 2017 at 8:49 PM, Daniel Manchester <[hidden email]> wrote:
Hi,

If you make "name" an Option[scala.xml.Text], I think the XML will be generated as you desire.

Dan


On Friday, March 24, 2017 at 2:01:33 AM UTC-4, Russ P. wrote:
Suppose I am printing out an XML element like this:

  def toXML: xml.Elem = {

    <waypt name={name}>
      ...
      </waypt>
    }

The "name" attribute is a String and is optional (some waypoints have names and some don't). I want to show it only if it is nonempty. In other words, I don't want to see name=""

I just want the name attribute completely omitted if the name is an empty String.

How can I do that in the toXML method above? Thanks.

--
You received this message because you are subscribed to a topic in the Google Groups "scala-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scala-user/vqLXM7clpqo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...