Solved

How to configure java xml Transformer to return html tags

Posted on 2008-10-23
2
952 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logiā€¦
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now