?
Solved

How to configure java xml Transformer to return html tags

Posted on 2008-10-23
2
Medium Priority
?
962 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
manuel_m earned 2000 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

764 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