Solved

How to configure java xml Transformer to return html tags

Posted on 2008-10-23
2
958 Views
Last Modified: 2013-12-22
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
Comment
Question by:ddas_auth
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
manuel_m earned 500 total points
ID: 22798947
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
 
LVL 6

Assisted Solution

by:manuel_m
manuel_m earned 500 total points
ID: 22798973
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Download a website to hdd 2 68
configure dependency in POM for new database 3 26
Java string replace 11 48
Fulfillment API php code sample 1 41
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question