Ampersand in string literal of XML node.

119 views
Skip to first unread message

Tim Stokes

unread,
Jun 23, 2011, 4:23:15 PM6/23/11
to scala-language
Why can't I have this?

val link = <a href="search?term=scala&count=20"></a>

The compiler does not like the ampersand in the string.

Tim Stokes

unread,
Jun 23, 2011, 4:26:15 PM6/23/11
to scala-language
If I turn it into a special character then it compiles.

val link = <a href="search?term=scala&nbsp;count=20"></a>

But that doesn't help me.

Is this a bug?

Tim Stokes

unread,
Jun 23, 2011, 4:30:08 PM6/23/11
to scala-language
So if I use:

val link = <a href="search?term=scala&amp;count=20"></a>

Then I actually get the right thing but it doesn't scan well from a
templating point of view.

Any chance this could be fixed or is it invalid XML?

coach3pete

unread,
Jun 23, 2011, 4:46:14 PM6/23/11
to scala-l...@googlegroups.com
Invalid:
<a href="search?term=scala&count=20"></a>

Valid:


<a href="search?term=scala&amp;count=20"></a>

See http://www.w3schools.com/XML/xml_validator.asp

What do you mean by "it doesn't scan well"?

Erik Peterson
m: (281) 804-9023
e...@ardec.com

Tim Stokes

unread,
Jun 23, 2011, 5:09:09 PM6/23/11
to scala-l...@googlegroups.com
I mean it doesn't read easily for people who are used to seeing just '&'

Martin S. Weber

unread,
Jun 23, 2011, 5:22:52 PM6/23/11
to scala-l...@googlegroups.com


It's not valid XML.

Compare the following quote from section 2.4 from the xml spec:

"The ampersand character (&) and the left angle bracket (<) MUST NOT appear in
their literal form, except when used as markup delimiters, or within a
comment, a processing instruction, or a CDATA section. If they are needed
elsewhere, they MUST be escaped using either numeric character references or
the strings " &amp; " and " &lt; " respectively. The right angle bracket (>)
may be represented using the string " &gt; ", and MUST, for compatibility, be
escaped using either " &gt; " or a character reference when it appears in the
string " ]]> " in content, when that string is not marking the end of a CDATA
section."

from: http://www.w3.org/TR/2008/REC-xml-20081126/#syntax

I know it sucks, but so does XML.

Have a great existence,

-Martin

coach3pete

unread,
Jun 23, 2011, 5:36:41 PM6/23/11
to scala-l...@googlegroups.com
I guessing a bit without knowing more context...but have you seen scalate for templating?
http://scalate.fusesource.org/documentation/jade.html

If you want something directly in a scala api consider creating a "searchLinkBuilder" using a case class with named params...or a fluent api: http://stackoverflow.com/questions/6347083/type-safe-builder-library-for-scala-and-java

Lars Hupel

unread,
Jun 23, 2011, 5:18:11 PM6/23/11
to scala-l...@googlegroups.com
That's why it's generally a good idea to avoid & for separation of
parameters. If you control the application, better choose ; or something
else (which is recommended by the W3C IIRC).

Reply all
Reply to author
Forward
0 new messages