0

I am using and XSL to transform a JSP into an HTML. While transforming I am linking a CSS. When the request has a character & to show in an input type="text" or in a textarea, as shown below, the browser shows a blank page.

How could I show that character on the web page?

The url is defined in the web.xml.

My JSP

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page contentType="text/html; charset=ISO-8859-1"%>
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>

<c:import var="xslt" url="${initParam.URL_XSLT}" charEncoding="ISO-8859-1"/>
<x:transform xslt="${xslt}">

<MyTag>
    <content>
        <div class="">
            <input type="text" name="aName" value="${requestScope.myString}"/>
            <textarea name="aComment">${requestScope.myString2}</textarea>
        </div>
    </content>
</MyTag>

My XSL

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="ISO-8859-1" indent="yes" />
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
    <head>
       <link type="text/css" rel="stylesheet" href="http://general.css"/>
    ...
    </head>
    <xsl:element name="body">
         ...
         <xsl:copy-of select="MyTag/content/div"></xsl:copy-of>
         ...
    </xsl:element>
</html>

I tried changing

<xsl:copy-of-select> 

by

<xsl:for-each select="//MyTag/content/*">
    <xsl:calltemplatename="node"/>
</xsl:for-each>

...


    <xsl:template name="node">
        <xsl:variable name="name" select="name()"/>
        <xsl:element name="{$name}">
            <xsl:copy-of select="@*" />
            <xsl:if test="child::*">
                <xsl:for-each select="current()/*">
                    <xsl:call-template name="node"/>
        </xsl:for-each>
            </xsl:if>
            <!-- first attempt -->
        <xsl:value-of select="translate(text(),'&','&amp;')" />
            <!-- second attempt -->
            <xsl:value-of select="concat('&lt;![CDATA[', text(),']]&gt;')" />
    </xsl:element>
    </xsl:template>

To solving this I can´t modify the JSP. Just can modify the XSL.

Thanx in advance.

1 Answers1

0

xsl:value-of has an attribute disable-output-escaping to disable encoding of the amp.

<xsl:value-of select="..." disable-output-escaping="yes" />
Doug Domeny
  • 4,410
  • 2
  • 33
  • 49
  • I tried but it doesn't work. On the server console I get this message The entity name must immediately follow the '&' in the entity reference. – user2261834 Apr 09 '13 at 14:02
  • Is the XSLT processing the output of the JSP or is the output from the XSLT going in between the textarea tags? I don't know what requestScope.myString and .myString2 are. – Doug Domeny Apr 09 '13 at 14:55
  • The XSLT is used for obtaining the final HTML. During the transformation the CSS and other changes are made. requestScope.myString and requestScope.myString2 are data got from the request using EL. – user2261834 Apr 10 '13 at 06:01
  • Thanks for the additional information. The XSLT is processing the output of the JSP, if I understand your situation. The problem is that the output of the JSP is not valid XML when a requestScope string contains & or other invalid XML characters. I know you said the JSP cannot be changed, but the request strings need to be escaped for XML by some means (e.g., fn:escapeXml). This SO answer may help http://stackoverflow.com/questions/475839/how-can-i-escape-special-html-characters-in-jsp. – Doug Domeny Apr 10 '13 at 12:20
  • The JPS cannot be changed because is published in production enviroment and we have six applications. I must find a global solution. I am investigating if it is possible to modify the objects in the HttpRequest before sending them from the servlet to the JSP. Thank you for your answers. – user2261834 Apr 10 '13 at 13:11
  • 1
    Finally I discarded this possible solution. I will escape the problematic characters in the servlet before sending the data in the request.setAttribute. – user2261834 Apr 10 '13 at 15:14