0

I'm trying to compare the values of two strings in a jsp file. Both of these strings contain an apostrophe '. These strings some from different sources.

When I directly compare the two strings like this:

<c:if test="${ fn:toLowerCase(colorInfo.value.name) eq fn:toLowerCase(colorName) }">
    hello!
</c:if>

the comparison returns a false. When I print out the two values, however, I see

Mike's Green

Mike's Green

in a previous step, these strings are trimmed to this is not a whitespace issue. For whatever reason, when stored as variables, one of these two is stored with an xml escaped apostrophe

&#39;

To prove this, I modified the comparison to the following:

<c:set var="apostrophe" value="'" />

<%-- I'm multilining this purely for readability on SO, it's 1 line in the code --%>
<c:if test="${ 
    fn:replace(fn:toLowerCase(colorInfo.value.name), '&#39;', apostrophe) 
    eq 
    fn:replace(fn:toLowerCase(colorName), '&#39;', apostrophe) 
}">
    hello!
</c:if>

This comparison returns true. Therefore, one of the two strings has a literal xml escape'd version of apostrophe while being stored.

Is there a more efficient way to escape any xml strings within a stored jsp variable? This is a very ugly way of doing the comparison, and it's not easily scalable as if future strings have other characters that are escaping improperly, then I will need to add more code to this logic.

Community
  • 1
  • 1
jros
  • 714
  • 1
  • 10
  • 33
  • You'd better fix the problem at the source where this data is coming from. – BalusC May 17 '16 at 19:53
  • I am not in control of that, and I feel like it's good practice to have any issues handled at both ends. I will be working with another team on resolving the issue on their end, but if in the future their stuff stops working I want my code to be able to handle it. – jros May 17 '16 at 19:56
  • You can unescape using commons lang. – BalusC May 17 '16 at 19:57

0 Answers0