3

Hi Can anyone help me in html to xml conversion using xslt in java.I converted xml to html using xslt in java.This is the code i used for that converstion:

import javax.xml.transform.*;

import java.net.*;
import java.io.*;

public class HowToXSLT {

public static void main(String[] args) {

  try {

    TransformerFactory tFactory = TransformerFactory.newInstance();

    Transformer transformer =
      tFactory.newTransformer
         (new javax.xml.transform.stream.StreamSource
            ("howto.xsl"));

    transformer.transform
      (new javax.xml.transform.stream.StreamSource
            ("howto.xml"),
       new javax.xml.transform.stream.StreamResult
            ( new FileOutputStream("howto.html")));
    }
  catch (Exception e) {
    e.printStackTrace( );
    }
  }
}

But i dont know the reverse process of this program that is to convert html to xml? Is there is any jar files available to do that? please help me...

5 Answers 5

3

Generally, it isn't possible to "reverse" a transformation, because a transformation in the general case isn't a 1:1 mapping.

For example, if the transformation does this:

<xsl:value-of select= "/x * /x"/>

and we get as result: 16

(and we know that the source XML document had only one element),

it isn't possible to determine from the value 16 whether the source XML document was:

<x>4</x>

or whether it was:

<x>-4</x>

And the above was only a simple example! :)

Sign up to request clarification or add additional context in comments.

Comments

1

This will depend on what you wish to do exactly. Apparently, howto.xsl contains the rules to be applied on the xml to get the html.
You will have to write another xsl file to do the reverse.

1 Comment

True, but I meant the reverse direction :-)
0

I believe it is not possible. XLST input must be XML conforming and HTML is not conforming to XML (unless you talk about XHTML).

4 Comments

The XHTML part is not the problem; you can tell XSLT to output well-formed XHTML. The real problem is trying to figure out which of the HTML elements was rendered by which bit of XSLT. I mean, if the XSLT has translated every piece of XML data into a div, you won't be able to work out from the resulting HTML alone which div was the result of which transformation.
A reverse transformation doesn't exist if the original transformation wasn't an 1:1 mapping -- which it very rarely is. This is why, in general the problem has no solution.
Thanks for your Reply...... Actually my flow is converting xml to html using xsl in java is completed but after applying some styles to html file i need to convert this html to xml using the xsl ? Is it possible to create the new xsl based on the html file that i have edited before? please help me.
I believe it is impossible, unless you took special care creating HTML.
0

May be you need to first make your html xhtml complaint, then use a xsl (reverse of the original xsl)which has instruction to convert the xhtml file to xml.

Comments

-1

Its not possible, you can use Microsoft.XMLDOM for converting from HTML to XML.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.