[xsl] Converting &, >, <, ", and other odd-ball characters...

Subject: [xsl] Converting &, >, <, ", and other odd-ball characters...
From: "Duffey, Kevin" <KDuffey@xxxxxxxxxxxx>
Date: Wed, 14 Feb 2001 17:01:48 -0800
Hi there,

I am about to write a java routine that is called by every single field of
every jsp page just to convert possible ", >, < and & as well as check for
some other characters and strip them (such as an MS Word paste that uses
bullets or the " " characters that use special codes for them).

I am not sure which way to go though. Is there a way to automatically have
XML and/or XSL convert these characters for me? For example, I am using JSP
to return dynamic XML. However, the XML parser chokes when it comes across a
& inside of an attribute. e.g.  <mytag attr="value&value2"/> I thought
anyting inside the " " would be safe, but apparently not. I also see this
problem on our site if someone enters a "> in an input box. It prematurely
closes off the value="  "> of the input box. I know the "> is a bad combo
close togther, so even if the page returns <input type="text" name="x"
value="something" > with a space after it, the " in between a value="  "
breaks the page (at least to some degree).

So the solution our team has come up with is on every field of every page
that can possibly have any of these non-html friendly characters, to call a
method and replace them. Problem is, String comparing/replacing is
intensive, and often creates unnecessary objects that use up more memory.
XSL already uses a bit of memory to keep the DOM representation of the xml
input source, so I don't want to have to use any more memory than I have to.

So I ask, is there a way at the top of an XML page to specify "replace &
with &amp;, " with &quot;" etc? I thought I saw something on [!entity..] or
whatever..but I am not sure if this is used in XML pages, or only XSL, and
if it can properly replace the characters, or if no matter what, I have to
change the & to the &amp; in every occurrence in every String returned on
every field of every page.

Thanks so much for any solutions you may come up with (or already have
figured out).

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread