Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 964
  • Last Modified:

How to configure java xml Transformer to return html tags

How do I configure Java xml Transformer to return html tags. Please find the xml and xsl used in the transformer in the attached code snippet.
In my result I see that the h3 and p tags are removed from the output.


Code
-----------
Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(xsl));
t.setOutputProperty(OutputKeys.ENCODING, CHARSET);
t.setOutputProperty(OutputKeys.METHOD, "html");
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
   t.transform(new StreamSource(xml), new StreamResult(out));
   System.out.println(out.toString("UTF-8"));
} catch (TransformerException ex) {
  ex.printStackTrace();
}
 
 
xml input data
------------------
<help>
<enterName>
<h3>Entering User Name</h3>
<p>
Please enter your user name in the "User Name" input field. if you notice a hide checkbox next to the user name input field, you can click on it to make your user name invisible to someone standing behind you.
After you enter your user name you may click on the Next button to proceed to the next step to login.
</p>
</enterName>
</help>
 
xsl 
--------------
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/"><xsl:value-of select="//enterName"/></xsl:template></xsl:stylesheet>

Open in new window

0
ddas_auth
Asked:
ddas_auth
  • 2
2 Solutions
 
manuel_mCommented:
If you want to ouput your elements <h3>... to </p>
you have to put the xml content into a CDATA section.

To output your tags as html, you have to declare the output method in your stylesheet.

disable-output-escaping means that the tags inside of the CDATA tags will be printed as <h3> ... etc.
otherwise you will receive an output with converted tags as <h3> for <h3>.

Please visit links for documentation:
http://www.w3.org/TR/xslt#section-HTML-Output-Method
http://www.w3.org/TR/xslt#disable-output-escaping
// xml
<help>
    <enterName>
        <![CDATA[<h3>Entering User Name</h3>
        <p>
Please enter your user name in the "User Name" input field. if you notice a hide checkbox next to the user name input field, you can click on it to make your user name invisible to someone standing behind you.
After you enter your user name you may click on the Next button to proceed to the next step to login.
        </p>]]>
    </enterName>
</help>
 
// xsl
<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:value-of select="//enterName" disable-output-escaping="yes"/>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
manuel_mCommented:
Another way to get your desired output would be to escape your tags.
And use it with the above posted xsl file.

If you have any further question feel free to ask them ;-)
// xml
<help>
    <enterName>
        &lt;h3&gt;Entering User Name&lt;/h3&gt;
        &lt;p&gt;
Please enter your user name in the "User Name" input field. if you notice a hide checkbox next to the user name input field, you can click on it to make your user name invisible to someone standing behind you.
After you enter your user name you may click on the Next button to proceed to the next step to login.
        &lt;/p&gt;
    </enterName>
</help>

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now