gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:16:04 25 4
gpt4 key购买 nike

请帮助我,我正在进行文件转换项目,即在 Java 中使用 xsl 将 xml 转换为 html,而且我还需要使用 xsl 将 html 转换为 xml。我的第一个过程结束了。但是我在转换的第二部分进行了结构化。是否有可能进行该转换。我会告诉您第一个过程的确切流程...

这是我的示例 xml 文件:tabl.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>

这是我的示例 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>

这是我的示例 java 文件:

主.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

}
}

这是我生成的 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>

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

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

提前致谢

最佳答案

无法进行反向转换

您的转换并非无损。除此之外,您还丢失了 emphasis 信息——它没有反射(reflect)在 HTML 输出中。

关于java - 在java中使用xslt创建基于html的xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9357604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com