Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Scala XML literals sensitive to whitespace between the tags?

Tags:

xml

scala

I've discovered that Scala XML literals are sensitive to whitespace, which is kinda strange, isn't it? since XML parsers don't normally give a damn about spaces between the tags.

This is a bummer because I'd like to set out my XML neatly in my code:

<sample>
  <hello />
</sample>

but Scala considers this to be a different value to

<sample><hello /></sample>

Proof is in the pudding:

scala> val xml1 = <sample><hello /></sample>
xml1: scala.xml.Elem = <sample><hello></hello></sample>

scala> val xml2 = <sample>
     | <hello />
     | </sample>
xml2: scala.xml.Elem = 
<sample>
<hello></hello>
</sample>

scala> xml1 == <sample><hello /></sample>
res0: Boolean = true

scala> xml1 == xml2
res1: Boolean = false

... What gives?

like image 667
David Avatar asked Jan 08 '11 20:01

David


People also ask

Can you have spaces in XML tags?

XML Naming Rules Element names cannot start with the letters xml (or XML, or Xml, etc) Element names can contain letters, digits, hyphens, underscores, and periods. Element names cannot contain spaces.

Are spaces allowed in HTML tags?

You can add space in HTML to any lines of text. You can use the &nbsp; HTML entity to create blank spaces in both paragraph text and text in tables, for example. Since there is no blank space keyboard character in HTML, you must type the entity &nbsp; for each space to add.


1 Answers

If you liked it you should have put a trim on it:

scala> val xml1 = <sample><hello /></sample>
xml1: scala.xml.Elem = <sample><hello></hello></sample>

scala> val xml2 = <sample>
     | <hello />
     | </sample>
xml2: scala.xml.Elem = 
<sample>
<hello></hello>
</sample>

scala> xml1 == xml2
res14: Boolean = false

scala> xml.Utility.trim(xml1) == xml.Utility.trim(xml2)
res15: Boolean = true
like image 90
Daniel C. Sobral Avatar answered Sep 21 '22 15:09

Daniel C. Sobral