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?

4
  • Possible duplicate of How to return Empty Node or NodeSeq using Scala XML? Commented Apr 27, 2016 at 17:29
  • Haven't you already asked the same question yesterday? stackoverflow.com/questions/36879390/… Commented Apr 27, 2016 at 17:30
  • No, that one was about zero to many elements, this is to strictly one or zero Commented Apr 27, 2016 at 17:37
  • What about a simple null ? Commented Jun 8, 2021 at 12:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try

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

2 Comments

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.