fn:escapeXml() JSTL function is used for HTML/XML character escaping which means it treats html/xml tags as a string rather than markup tags. It is similar to the escapeXml attribute of <c:out> tag. Let’s understand this with the help of an example-
Syntax
String escapeXml(String input_string)
Converts the input_string into a output string after escaping html/xml markup tags.
- function return type is String
- Argument: input String
fn:escapeXml() Example
Here we have two Strings which have html tags bold(<b>) and italic (<i>). We are processing them using fn:escapeXml() function.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <html> <head> <title>JSTL fn:escapeXml() example</title> </head> <body> Message1: <b>Hi This is just a message</b> <br>Message2: <i>This is an example</i> <br>Message1 and fn:escapeXml(): ${fn:escapeXml("<b>Hi This is just a message</b>")} <br>Message2 and fn:escapeXml(): ${fn:escapeXml("<i>This is an example</i>")} </body> </html>
Output:
As you can see when we used the function on input strings, the html tags didn’t work and get printed as it is, just like another normal string.
Prateek Ashtikar says
Hello,
Many Thanks for example. But I would like to escape all html characters and prints only values in it
For Example:
If passing TestUser —> I should get value only TestUser
If passing TestUATUser –> I should get value TestUATUser
Could any one please suggest what function or code I need to used? Your input is much appreciated.
regards,