Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an Empty NodeSeq using Scala XML?

Tags:

xml

scala

I'm building an XML by pieces using different functions such as the following example:

<xml>
  { maybeXml(param) }
</xml>

And trying to return an Empty or Non-empty NodeSeq based on the value of the param such as:

def maybeXml(param: Boolean): NodeSeq = {
  if(param) <someXml></someXml>
  else ??? //Empty or None doesn't work
}

The solution I'm using now is just defining the function type as Option[NodeSeq] and then using it as maybeXml.getOrElse(""), but that doesn't make that much sense to me. My current usage is as follows:

<xml>
  { maybeXml(param).getOrElse("") }
</xml>

def maybeXml(param: Boolean): NodeSeq = {
  if(param) Some(<someXml></someXml>)
  else None
}

Is a better way to express this using an Empty NodeSeq directly?

like image 554
chaotive Avatar asked Apr 27 '16 02:04

chaotive


1 Answers

For empty NodeSeq use NodeSeq.Empty

def maybeXml(param: Boolean): NodeSeq = {
  if(param) <someXml></someXml>
  else NodeSeq.Empty
} 
like image 56
Łukasz Avatar answered Oct 04 '22 16:10

Łukasz