This page is no longer maintained — Please continue to the home page at www.scala-lang.org

[SCALA.XML] How can I use scala.xml.Unparsed efficiently?

6 replies
Arlindo Lima
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.
Hi. I'm making a small academic program that transforms XML into HTML. The HTML is represented as a scala.xml.Node.

I have a small function that should transform the XML tag <EXAMPLE>just text</EXAMPLE> into <BR /><I>just text</I><BR /> (this function is called a lot of times).

Since <BR /> is translated by scala.XML.Node into <BR></BR>, browsers interpret this as two line-breaks.

Let's assume I must use the function scala.xml.Unparsed to guarantee the <BR /> isn't transformed into <BR></BR>.




A good, efficient, solution would be something like this:

val unparsedBR = Unparsed( "<BR />" )

def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => unparsedBR<I>{ text }</I>unparsedBR
   }
}

This, however generates a compilation error (not surprisingly).




The solution I've found (highly inefficient) is:

def exampleItem2( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => Unparsed( "<BR />" + "<I>" + text + "</I>" + "<BR />" )
   }
}




Is there a better way to do this?


David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: [SCALA.XML] How can I use scala.xml.Unparsed efficiently?
You could try setting the content type to XHTML rather than HTML.  That'll work for most cases.

You could also do:
def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => <xml:group>{unparsedBR}<I>{ text }</I>{unparsedBR></xml:group>
   }
}


On Sun, Jan 4, 2009 at 6:08 AM, Arlindo Lima <arlindolima [at] gmail [dot] com> wrote:
Hi. I'm making a small academic program that transforms XML into HTML. The HTML is represented as a scala.xml.Node.

I have a small function that should transform the XML tag <EXAMPLE>just text</EXAMPLE> into <BR /><I>just text</I><BR /> (this function is called a lot of times).

Since <BR /> is translated by scala.XML.Node into <BR></BR>, browsers interpret this as two line-breaks.

Let's assume I must use the function scala.xml.Unparsed to guarantee the <BR /> isn't transformed into <BR></BR>.




A good, efficient, solution would be something like this:

val unparsedBR = Unparsed( "<BR />" )

def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => unparsedBR<I>{ text }</I>unparsedBR
   }
}

This, however generates a compilation error (not surprisingly).




The solution I've found (highly inefficient) is:

def exampleItem2( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => Unparsed( "<BR />" + "<I>" + text + "</I>" + "<BR />" )
   }
}




Is there a better way to do this?





--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
Arlindo Lima
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.
Re: [SCALA.XML] How can I use scala.xml.Unparsed efficiently?
<xml:group> works very well (XHTML would too).

Only now I've seen there's a small reference to it in scalaxbook.docbk.html (although it's too hidden).

Thanks David Pollack.

2009/1/4 David Pollak <feeder [dot] of [dot] the [dot] bears [at] gmail [dot] com>
You could try setting the content type to XHTML rather than HTML.  That'll work for most cases.

You could also do:
def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => <xml:group>{unparsedBR}<I>{ text }</I>{unparsedBR></xml:group>
   }
}


On Sun, Jan 4, 2009 at 6:08 AM, Arlindo Lima <arlindolima [at] gmail [dot] com> wrote:
Hi. I'm making a small academic program that transforms XML into HTML. The HTML is represented as a scala.xml.Node.

I have a small function that should transform the XML tag <EXAMPLE>just text</EXAMPLE> into <BR /><I>just text</I><BR /> (this function is called a lot of times).

Since <BR /> is translated by scala.XML.Node into <BR></BR>, browsers interpret this as two line-breaks.

Let's assume I must use the function scala.xml.Unparsed to guarantee the <BR /> isn't transformed into <BR></BR>.




A good, efficient, solution would be something like this:

val unparsedBR = Unparsed( "<BR />" )

def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => unparsedBR<I>{ text }</I>unparsedBR
   }
}

This, however generates a compilation error (not surprisingly).




The solution I've found (highly inefficient) is:

def exampleItem2( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => Unparsed( "<BR />" + "<I>" + text + "</I>" + "<BR />" )
   }
}




Is there a better way to do this?





--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: [SCALA.XML] How can I use scala.xml.Unparsed efficiently?
I think you should switch to a better solution: Use CSS for styling your text.

<EXAMPLE>just text</EXAMPLE> = <p class="example">just text</p>

On Sun, Jan 4, 2009 at 3:08 PM, Arlindo Lima <arlindolima [at] gmail [dot] com> wrote:
Hi. I'm making a small academic program that transforms XML into HTML. The HTML is represented as a scala.xml.Node.

I have a small function that should transform the XML tag <EXAMPLE>just text</EXAMPLE> into <BR /><I>just text</I><BR /> (this function is called a lot of times).

Since <BR /> is translated by scala.XML.Node into <BR></BR>, browsers interpret this as two line-breaks.

Let's assume I must use the function scala.xml.Unparsed to guarantee the <BR /> isn't transformed into <BR></BR>.




A good, efficient, solution would be something like this:

val unparsedBR = Unparsed( "<BR />" )

def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => unparsedBR<I>{ text }</I>unparsedBR
   }
}

This, however generates a compilation error (not surprisingly).




The solution I've found (highly inefficient) is:

def exampleItem2( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => Unparsed( "<BR />" + "<I>" + text + "</I>" + "<BR />" )
   }
}




Is there a better way to do this?





--
Viktor Klang
Senior Systems Analyst
Arlindo Lima
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.
Re: [SCALA.XML] How can I use scala.xml.Unparsed efficiently?
This is just a simple, academic, example. It won't be used in a "real life" situation. :)

But CSS is indeed very powerful.

2009/1/4 Viktor Klang <viktor [dot] klang [at] gmail [dot] com>
I think you should switch to a better solution: Use CSS for styling your text.

<EXAMPLE>just text</EXAMPLE> = <p class="example">just text</p>

On Sun, Jan 4, 2009 at 3:08 PM, Arlindo Lima <arlindolima [at] gmail [dot] com> wrote:
Hi. I'm making a small academic program that transforms XML into HTML. The HTML is represented as a scala.xml.Node.

I have a small function that should transform the XML tag <EXAMPLE>just text</EXAMPLE> into <BR /><I>just text</I><BR /> (this function is called a lot of times).

Since <BR /> is translated by scala.XML.Node into <BR></BR>, browsers interpret this as two line-breaks.

Let's assume I must use the function scala.xml.Unparsed to guarantee the <BR /> isn't transformed into <BR></BR>.




A good, efficient, solution would be something like this:

val unparsedBR = Unparsed( "<BR />" )

def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => unparsedBR<I>{ text }</I>unparsedBR
   }
}

This, however generates a compilation error (not surprisingly).




The solution I've found (highly inefficient) is:

def exampleItem2( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => Unparsed( "<BR />" + "<I>" + text + "</I>" + "<BR />" )
   }
}




Is there a better way to do this?





--
Viktor Klang
Senior Systems Analyst

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: [SCALA.XML] How can I use scala.xml.Unparsed efficiently?
My reply was a friendly reminder that sometimes people are trying to solve symptoms, and not problems :)

On Sun, Jan 4, 2009 at 10:47 PM, Arlindo Lima <arlindolima [at] gmail [dot] com> wrote:
This is just a simple, academic, example. It won't be used in a "real life" situation. :)

But CSS is indeed very powerful.

2009/1/4 Viktor Klang <viktor [dot] klang [at] gmail [dot] com>
I think you should switch to a better solution: Use CSS for styling your text.

<EXAMPLE>just text</EXAMPLE> = <p class="example">just text</p>

On Sun, Jan 4, 2009 at 3:08 PM, Arlindo Lima <arlindolima [at] gmail [dot] com> wrote:
Hi. I'm making a small academic program that transforms XML into HTML. The HTML is represented as a scala.xml.Node.

I have a small function that should transform the XML tag <EXAMPLE>just text</EXAMPLE> into <BR /><I>just text</I><BR /> (this function is called a lot of times).

Since <BR /> is translated by scala.XML.Node into <BR></BR>, browsers interpret this as two line-breaks.

Let's assume I must use the function scala.xml.Unparsed to guarantee the <BR /> isn't transformed into <BR></BR>.




A good, efficient, solution would be something like this:

val unparsedBR = Unparsed( "<BR />" )

def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => unparsedBR<I>{ text }</I>unparsedBR
   }
}

This, however generates a compilation error (not surprisingly).




The solution I've found (highly inefficient) is:

def exampleItem2( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => Unparsed( "<BR />" + "<I>" + text + "</I>" + "<BR />" )
   }
}




Is there a better way to do this?





--
Viktor Klang
Senior Systems Analyst




--
Viktor Klang
Senior Systems Analyst
hsyl20
Joined: 2008-08-19,
User offline. Last seen 4 years 11 weeks ago.
Re : [SCALA.XML] How can I use scala.xml.Unparsed efficiently?
Off-topic: if you want to have xhtml compliant html code, you have to make <br> and <i> lowercase.

"XHTML documents must use lower case for all HTML element and attribute names. This difference is necessary because XML is case-sensitive e.g. <li> and <LI> are different tags."
(http://www.w3.org/TR/xhtml1/#h-4.2)

Cheers,
Sylvain

De : Arlindo Lima <arlindolima [at] gmail [dot] com>
À : scala-user [at] listes [dot] epfl [dot] ch
Envoyé le : Dimanche, 4 Janvier 2009, 15h08mn 23s
Objet : [scala-user] [SCALA.XML] How can I use scala.xml.Unparsed efficiently?

Hi. I'm making a small academic program that transforms XML into HTML. The HTML is represented as a scala.xml.Node.

I have a small function that should transform the XML tag <EXAMPLE>just text</EXAMPLE> into <BR /><I>just text</I><BR /> (this function is called a lot of times).

Since <BR /> is translated by scala.XML.Node into <BR></BR>, browsers interpret this as two line-breaks.

Let's assume I must use the function scala.xml.Unparsed to guarantee the <BR /> isn't transformed into <BR></BR>.




A good, efficient, solution would be something like this:

val unparsedBR = Unparsed( "<BR />" )

def exampleItem1( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => unparsedBR<I>{ text }</I>unparsedBR
   }
}

This, however generates a compilation error (not surprisingly).




The solution I've found (highly inefficient) is:

def exampleItem2( item: Node) = {
   item match {
      case <EXAMPLE>{ text }</EXAMPLE> => Unparsed( "<BR />" + "<I>" + text + "</I>" + "<BR />" )
   }
}




Is there a better way to do this?



Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland