Solved

How to configure java xml Transformer to return html tags

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
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 …

948 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

23 Experts available now in Live!

Get 1:1 Help Now