2

Please note this question is strictly related to returning 0 or 1 elements (Node or Elem). Please don't consider NodeSeq.Empty as a solution, as that already works for functions returning NodeSeq type (0 or more elements), as solved on this question.

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

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

And trying to return an Empty or Non-empty Node (or Elem) based on the value of the param such as:

def maybeXmlNode(param: Boolean): NodeOrElem = {
  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[Elem] and then using it as maybeXml.getOrElse(""), but that doesn't make that much sense to me. My current usage is as follows:

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

def maybeXmlNode(param: Boolean): Option[Elem] = {
  if(param) Some(<someXml></someXml>)
  else None
}

Does a better way to express this exists, probably by using an Empty Node or Elem directly?

Community
  • 1
  • 1
chaotive
  • 182
  • 14
  • Possible duplicate of [How to return Empty Node or NodeSeq using Scala XML?](http://stackoverflow.com/questions/36879390/how-to-return-empty-node-or-nodeseq-using-scala-xml) – Dima Apr 27 '16 at 17:29
  • Haven't you already asked the same question yesterday? http://stackoverflow.com/questions/36879390/how-to-return-empty-node-or-nodeseq-using-scala-xml?lq=1 – Dima Apr 27 '16 at 17:30
  • No, that one was about zero to many elements, this is to strictly one or zero – chaotive Apr 27 '16 at 17:37
  • What about a simple null ? – gekomad Jun 08 '21 at 12:05

2 Answers2

1

I've been looking for the same thing (years later). It doesn't seem like a real solution exists.
However I've hacked around it with xml.Text("") (Unparsed("") is similar, but pretty prints funny sometimes).

example:

val pretty = new PrettyPrinter(100, 2)
println(pretty.format(
  <elem attrib={Text("")}>
    {Text("")}
    <withtag thing="thang">
      boop
    </withtag>
    {"something"}
  </elem>
))

outputs

<elem attrib="">
  <withtag thing="thang"> boop </withtag>
  something
</elem>

This may however lead to non-obvious behavior with things like

def wrap(node: Node) = <wrapped>{node}</wrapped>

(hard to tell if one should be expecting another "empty" node back, or a wrapped element with no children). It also doesn't work with xml.Utility.trim.

kag0
  • 5,624
  • 7
  • 34
  • 67
0

Try

<xml>{maybeXmlNode(param).getOrElse(new NodeBuffer())}</xml>
zero323
  • 322,348
  • 103
  • 959
  • 935
user1763729
  • 167
  • 1
  • 11
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/14387336) – Wildcat Nov 24 '16 at 09:33
  • That's a good point. I don't think there is something like an empty elem or node since node is abstract and an element needs to have a label. So an option represents the zero or one concept without including more than one. – user1763729 Dec 12 '16 at 22:56