Discussion:
[FreeMarker-user] html markup escape and CDATA issue
FOX427
2015-03-03 20:31:22 UTC
Permalink
Hi All! I am fetching data from xml as usual, I have a node
${document.details.location} and it might have html markup in it, so for
example in one xml the node is empty and in another it has a lot of markup
like:

<location>
<p> Some Hotel Name <www.google.com> &#160;<br /> Some Street<br /> Miami,
FL 33131<br /> 305 503 5555 </p>.
</location>

The problem is, if I use <#escape x as x?html> and put
${document.details.location} in it the it will give me:

<p> Some Hotel Name <www.google.com> &#160;<br /> Some Street<br /> Miami,
FL 33131<br /> 305 503 5555 </p>.(including <br> <p> and etc. in the browser
)


If I generate xml and wrap that node in CDATA like here:
<location>

</location>

then I have:

Some Hotel Name
Some Street
Miami, FL 33131
305 503 5555
]]>

So the issue here I am getting "*]]>* " out of nowhere....Can someone
explain what is best practice in handling random html markup nodes so i can
be safe. Thanks in advance.



--
View this message in context: http://freemarker.624813.n4.nabble.com/html-markup-escape-and-CDATA-issue-tp4655387.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2015-03-03 21:21:07 UTC
Permalink
I don't understand what you trying to say. If you don't want to escape
the output, then why are you using #escalpe (or why don't you use
#noescape)? Where do you put that CDATA section and how?
Post by FOX427
Hi All! I am fetching data from xml as usual, I have a node
${document.details.location} and it might have html markup in it, so for
example in one xml the node is empty and in another it has a lot of markup
<location>
<p> Some Hotel Name <www.google.com> &#160;<br /> Some Street<br /> Miami,
FL 33131<br /> 305 503 5555 </p>.
</location>
The problem is, if I use <#escape x as x?html> and put
<p> Some Hotel Name <www.google.com> &#160;<br /> Some Street<br /> Miami,
FL 33131<br /> 305 503 5555 </p>.(including <br> <p> and etc. in the browser
)
<location>
</location>
Some Hotel Name
Some Street
Miami, FL 33131
305 503 5555
]]>>
Post by FOX427
So the issue here I am getting "*]]>* " out of nowhere....Can someone
explain what is best practice in handling random html markup nodes so i can
be safe. Thanks in advance.
--
http://freemarker.624813.n4.nabble.com/html-markup-escape-and-CDATA-issue-tp4655387.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
Thanks,
Daniel Dekany
FOX427
2015-03-03 21:51:08 UTC
Permalink
Ok, when you have xml with this node:

<location>
<p> Some Hotel Name &#160;<br /> Some Street<br /> Miami,
FL 33131<br /> 305 503 5555 </p>.
</location>

in order to get value from it I have to use:

<#list document.details.location as loc>
<#if loc.p??&&loc.p?has_content>
<#list loc.p as p>
<#if p.a??&&p.a?has_content>
${p.a} <${***@href}>
</#if>
<#list p?children as nodes>
${nodes}
</#list>
</#list>
</#if>
</#list>

and all these lines will be for nothing if next xml has two "<p></p>" or no
"<p></p>" at all or completely different things in node, i dont know why but
freemarker treats <p> as child node, as a result, if node has <p> and you
dont use ${p.xxxx} it complains saying it has child node, and vice versa, if
you add ${p.xxxx} and there is no <p> it prints nothing. So my main goal is
to be able to use freemarker that handles html markups in prompt way so I
don't have to worry about having different html markups in different xml
with the same node name(<location>).

So i thought maybe I need to use escape or CDATA to get rid of this
issue...I am just starting to learn freemarker, maybe i don't understand a
lot of stuff that might be very basic for you so i apologize in advance :(
Thank you. :)



--
View this message in context: http://freemarker.624813.n4.nabble.com/html-markup-escape-and-CDATA-issue-tp4655387p4655389.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2015-03-03 23:09:36 UTC
Permalink
Post by FOX427
<location>
<p> Some Hotel Name &#160;<br /> Some Street<br /> Miami,
FL 33131<br /> 305 503 5555 </p>.
</location>
<#list document.details.location as loc>
<#if loc.p??&&loc.p?has_content>
(`loc.p??` is always true - see XML Processing Guide)
Post by FOX427
<#list loc.p as p>
<#if p.a??&&p.a?has_content>
(`loc.a??` is always true too)
Are you sure there are never multiple `p.a`-s?
Post by FOX427
</#if>
<#list p?children as nodes>
${nodes}
</#list>
</#list>
</#if>
</#list>
(There was no #escape here BTW.)
Post by FOX427
and all these lines will be for nothing if next xml has two "<p></p>" or no
"<p></p>" at all or completely different things in node, i dont know why but
freemarker treats <p> as child node, as a result, if node has <p> and you
dont use ${p.xxxx} it complains saying it has child node, and vice versa, if
you add ${p.xxxx} and there is no <p> it prints nothing. So my main goal is
to be able to use freemarker that handles html markups in prompt way so I
don't have to worry about having different html markups in different xml
with the same node name(<location>).
Sorry, I don't really understand what you are saying, but if you just
want to print everything inside <location>...</location>, then use
${location.@@nested_markup}. (Inside #noescape if you are inside
#escape.)

Or, the contents of <location> should be CDATA, inside the XML. Now of
course it's up to the designers of the XML schema, and I don't know
what their design goals were.
Post by FOX427
So i thought maybe I need to use escape or CDATA to get rid of this
issue...I am just starting to learn freemarker, maybe i don't understand a
lot of stuff that might be very basic for you so i apologize in advance :(
Read the XML Processing Guide of you haven't. You can spare a lot of
time of yours with that.
Post by FOX427
Thank you. :)
--
Thanks,
Daniel Dekany
FOX427
2015-03-04 19:11:38 UTC
Permalink
Thanks Daniel,

@@nested_markup - that's what i need! But I used @@markup before it worked
perfectly fine if node has a lot of markups but failed to show result if
node had a string(<location>Mars</location>) so even node has string value
it treats it as empty node?

Also, I would like to know what do you think about including html inside
XML? Do you think it is a good idea? If its bad then what would you suggest?
Thanks again




--
View this message in context: http://freemarker.624813.n4.nabble.com/html-markup-escape-and-CDATA-issue-tp4655387p4655391.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2015-03-05 00:14:11 UTC
Permalink
Post by FOX427
Thanks Daniel,
@@nested_markup - that's what i need! But I used @@markup before it worked
perfectly fine if node has a lot of markups but failed to show result if
node had a string(<location>Mars</location>) so even node has string value
it treats it as empty node?
You have overlooked something there... If you go to
http://freemarker-online.kenshoo.com/ and copy-paste this into the
template field:

Nested markup: ${doc.location.@@nested_markup}
Markup: ${doc.location.@@markup}

and this to the data-model field:

doc=<location>Marsh</location>

then output will be:

Nested markup: Marsh
Markup: <location>Marsh</location>
Post by FOX427
Also, I would like to know what do you think about including html inside
XML? Do you think it is a good idea? If its bad then what would you suggest?
If you won't parse that HTML content then store it in the XML as
non-XML character data. Like inside a CDATA section, or just escaped.
Especially as HTML is not always well-formed XML. Then you don't need
@@markup and such either. It's much faster to load and output the
content then.
Post by FOX427
Thanks again
--
http://freemarker.624813.n4.nabble.com/html-markup-escape-and-CDATA-issue-tp4655387p4655391.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
Thanks,
Daniel Dekany
Loading...