在java中使用xslt创建基于html的xml。

时间:2021-01-02 21:45:32

Please help me i am doing project of file conversion that is converting xml to html using xsl in java and also i need to convert html to xml using the xsl. my first process is got over.but i structed in second part of conversion.. Is there is any possibility to do that conversion.I will tell you the exact flow of the first process...

请帮助我正在做文件转换项目,使用java中的xsl将xml转换为html,还需要使用xsl将html转换为xml。我的第一个过程结束了。但我是在第二部分进行转换的。有没有可能进行这种转换?我会告诉你第一个过程的确切流程……

This is my sample xml file: tabl.xml:

这是我的示例xml文件:table .xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xml" href="testxsl.xsl"?>
<mainpara>
<epigraph>
<para>Though successful research demands a deep
<emphasis role="italic">trained</emphasis> 
<emphasis role="italic">taught</emphasis> to regard.
</para>
<para>Kuhn (1976, p. 66)</para>
</epigraph>
<blockquote role="extract">
<para>Though successful research demands a deep commitment to the status quo.
<emphasis role="italic">trained</emphasis>
<emphasis role="italic">taught</emphasis> 
</para>
</blockquote>
</mainpara>

This is my sample xsl file:

这是我的xsl文件示例:

testme.xsl

testme.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="mainpara">
    <html>
    <body>
      <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>     

  <xsl:template match="epigraph">
    <div>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="para">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="blockquote">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>  
</xsl:stylesheet>

This is my sample java file:

这是我的java文件示例:

Main.java

Main.java

import java.io.File;
import java.io.InputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {  


    public static void simpleTransform(String sourcePath, String xsltPath,  
                                       String resultDir) {

        TransformerFactory tFactory = TransformerFactory.newInstance();  
        try {  
            Transformer transformer =  
                tFactory.newTransformer(new StreamSource(new File(xsltPath)));  

            transformer.transform(new StreamSource(new File(sourcePath)),  
                                  new StreamResult(new File(resultDir)));  

        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  

    public static void main(String[] args) {  


        System.setProperty("javax.xml.transform.TransformerFactory",
                "net.sf.saxon.TransformerFactoryImpl");

      simpleTransform("E:/bh/tabl.xml","E:/bh/testme.xsl", "E:/bh/me.html");
      //simpleTransform("E:/bh/me.html","E:/bh/11111.xsl","E:/bh/tab.xml" ); //This is i need

    }  
}  

This is my generated html file:

这是我生成的html文件:

<html>
   <body>
      <div>

         <p>Though successful research demands a deep commitment to the status quo, innovation
         </p>

         <p>Kuhn (1976, p. 66)</p>

      </div>
      <b>

         <p>Though successful research demands a deep commitment to the status quo
         </p>
         </b>

   </body>
</html>

Now i will edit this html file after edited this html file i need to convert this again to the xml file or xhtml file that is i need the reverse process. I think it is possible only by modifying the xsl. Is it possible to create the xsl based on that html.If i created that xsl file then i pass that xsl file in the above java program( i commented that line in the main java program).So i have to create that xsl file.

现在,在编辑了这个html文件之后,我将编辑这个html文件,我需要将它再次转换为xml文件或xhtml文件,我需要反向处理。我认为只有修改xsl才有可能。是否可以基于该html创建xsl。如果我创建了xsl文件,那么我将在上面的java程序中传递xsl文件(我在主java程序中注释了这一行)。所以我必须创建那个xsl文件。

Please help me.. I need your help..

请帮我. .我需要你的帮助。

Thanks in advance

谢谢提前

2 个解决方案

#1


0  

The reverse transformation can't be done.

逆变换是不可能的。

Your transformation isn't lossless. Among other things, you have lost the emphasis information -- it isn't reflected in the HTML output.

您的转换不是无损。除此之外,您还失去了重点信息——它没有反映在HTML输出中。

#2


0  

Dimitre is, as always, correct but let me explain a bit more.

迪米特里和往常一样是对的,让我再解释一下。

There's at least two issues here.

这里至少有两个问题。

Firstly, you have text such as <emphasis role="italic">trained</emphasis> in your input XML that doesn't get output to your HTML. So there is absolutely no way that any tool or indeed person could reverse the transformation and guess what XML was used to generate the HTML. That's what Dimitre means by "Your transformation isn't lossless"

首先,您的输入XML中有诸如< focus role="italic">训练的 ,它不会将输出输出到HTML。因此,任何工具或人员都不可能逆转转换,猜出用于生成HTML的XML是什么。这就是Dimitre所说的"变换不是无损的"

But let's assume that you changed your XSLT that turned the XML into HTML such that it was humanly possible to work out what the original XML was by looking at the HTML...

但是让我们假设您更改了将XML转换为HTML的XSLT,以便通过查看HTML可以了解原始XML是什么……

Secondly, the XLS to turn the HTML back into XML is going to be completely different to the XSL that turned the XML into HTML.

其次,将HTML转换回XML的XLS将与将XML转换成HTML的XSL完全不同。

Did you code the XSLT shown in your question yourself? If not then the first thing that you need to do is try and understand that code. You'll never figure out a solution to your problem if you can't even understand this much.

您自己编写了问题中显示的XSLT吗?如果不是,那么您需要做的第一件事就是尝试理解该代码。如果你连这一点都理解不了,你就永远无法找到解决问题的办法。

#1


0  

The reverse transformation can't be done.

逆变换是不可能的。

Your transformation isn't lossless. Among other things, you have lost the emphasis information -- it isn't reflected in the HTML output.

您的转换不是无损。除此之外,您还失去了重点信息——它没有反映在HTML输出中。

#2


0  

Dimitre is, as always, correct but let me explain a bit more.

迪米特里和往常一样是对的,让我再解释一下。

There's at least two issues here.

这里至少有两个问题。

Firstly, you have text such as <emphasis role="italic">trained</emphasis> in your input XML that doesn't get output to your HTML. So there is absolutely no way that any tool or indeed person could reverse the transformation and guess what XML was used to generate the HTML. That's what Dimitre means by "Your transformation isn't lossless"

首先,您的输入XML中有诸如< focus role="italic">训练的 ,它不会将输出输出到HTML。因此,任何工具或人员都不可能逆转转换,猜出用于生成HTML的XML是什么。这就是Dimitre所说的"变换不是无损的"

But let's assume that you changed your XSLT that turned the XML into HTML such that it was humanly possible to work out what the original XML was by looking at the HTML...

但是让我们假设您更改了将XML转换为HTML的XSLT,以便通过查看HTML可以了解原始XML是什么……

Secondly, the XLS to turn the HTML back into XML is going to be completely different to the XSL that turned the XML into HTML.

其次,将HTML转换回XML的XLS将与将XML转换成HTML的XSL完全不同。

Did you code the XSLT shown in your question yourself? If not then the first thing that you need to do is try and understand that code. You'll never figure out a solution to your problem if you can't even understand this much.

您自己编写了问题中显示的XSLT吗?如果不是,那么您需要做的第一件事就是尝试理解该代码。如果你连这一点都理解不了,你就永远无法找到解决问题的办法。