如何从 XML 和 XSLT 样式表输出 HTML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3959219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to output HTML file from XML and XSLT stylesheet
提问by Hyman
I've created a XML data document and an XSLT stylesheet, and I want to output an HTML document based on the two. My stylesheet has the tag, and my XML document has the processor instuction (along with various "xsl:value-of" references). My question is, what are the actual "mechanics" of getting the XSLT processor (which, I understand, is built in to all web browsers) to read the XML and XSLT stylesheet files and output the HTML document, which will then display on the browser? The XML book that I've been reading doesn't specify this! Thank you
我已经创建了一个 XML 数据文档和一个 XSLT 样式表,我想基于这两者输出一个 HTML 文档。我的样式表有标签,我的 XML 文档有处理器指令(以及各种“xsl:value-of”引用)。我的问题是,让 XSLT 处理器(据我了解,内置于所有 Web 浏览器中)读取 XML 和 XSLT 样式表文件并输出 HTML 文档的实际“机制”是什么,然后将显示在浏览器?我一直在阅读的 XML 书籍没有指定这一点!谢谢
回答by yuvraj kadam
Following is a java code which is used to create the HTML file.When you will run execute this code the out.html file will be created.
以下是用于创建 HTML 文件的 Java 代码。当您运行此代码时,将创建 out.html 文件。
package xslt;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.*;
class XSLT {
public static void main ( String argv[] ) throws Exception {
File stylesheet = new File("xslt-example.xsl");
File xmlfile = new File("SigmodRecord.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(xmlfile);
StreamSource stylesource = new StreamSource(stylesheet);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer(stylesource);
DOMSource source = new DOMSource(document);
//The Html output is in out.html
StreamResult result = new StreamResult("out.html");
transformer.transform(source,result);
}
}
回答by Dimitre Novatchev
My question is, what are the actual "mechanics" of getting the XSLT processor (which, I understand, is built in to all web browsers) to read the XML and XSLT stylesheet files and output the HTML document, which will then display on the browser?
我的问题是,让 XSLT 处理器(据我了解,内置于所有 Web 浏览器中)读取 XML 和 XSLT 样式表文件并输出 HTML 文档的实际“机制”是什么,然后将显示在浏览器?
It is the task of the specific HTML browser being used to invoke its XSLT processor. Then the browser interpretes the results of the XSLT transformation as the HTML that should be displayed. Do note that in general browsesers are not required to support XSLT pre-processing, so there may exist browsers that do not have an associated XSLT processor and do not honor the xml-stylesheet
PI for the type="text/xsl"
pseudo-attribute.
这是用于调用其 XSLT 处理器的特定 HTML 浏览器的任务。然后浏览器将 XSLT 转换的结果解释为应该显示的 HTML。请注意,一般浏览器不需要支持 XSLT 预处理,因此可能存在没有关联 XSLT 处理器并且不xml-stylesheet
支持type="text/xsl"
伪属性的PI 的浏览器。
For more information read the W3C spec on "Associating Style Sheets with XML Documents"
有关更多信息,请阅读 W3C 规范“将样式表与 XML 文档相关联”
To test the XSLT transformation in this, somewhat primitive way, you can open the XML file with your browser (do your homework and learn how to open a local file from the browser) and examine the results with a "View Source"
or similar command.
要以这种有点原始的方式测试 XSLT 转换,您可以使用浏览器打开 XML 文件(做功课并学习如何从浏览器打开本地文件)并使用一个"View Source"
或类似的命令检查结果。
I certainly do not recommend this primitive technique. It is much better to use one of the many existing XSLT IDEs, such as the XSelerator, oXygen, Visual Studio, ..., etc.
我当然不推荐这种原始技术。这是更好的使用许多现有的XSLT的IDE之一,如XSelerator,氧气,Visual Studio中,...等等。
回答by Robin
You can either run XSL transforms in the "normal way" using Javascript API, or use an xml-stylesheet processing instruction, like this:
您可以使用 Javascript API 以“正常方式”运行 XSL 转换,也可以使用 xml 样式表处理指令,如下所示:
Load this in to your browser...
将此加载到您的浏览器中...
<?xml version="1.0"?>
<?xml-stylesheet href="demo.xslt" type="text/xsl"?>
<data>
<first>first</first>
<second>second</second>
</data>
and the stylesheet, save this as demo.xslt in the same dir as the XML file
和样式表,将其保存为与 XML 文件相同的目录中的 demo.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head><title>Xslt browser demo</title></head>
<body>
Here's my data:
<xsl:for-each select="/data/*"><b><xsl:value-of select="."/></b></xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
This works for me in Firefox on Linux.
这在 Linux 上的 Firefox 中对我有用。
回答by Per T
Dimitre's answer is what you need. But here you have an example:
Dimitre 的回答正是您所需要的。但这里有一个例子:
XML:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
<document>
...
</document>
Opening the preceding XML document in any (not really but you get it...) browser and it will transform the XML document with stylesheet.xsl
and display the result.
在任何浏览器中打开前面的 XML 文档(不是真的,但你会得到它......),它将转换 XML 文档stylesheet.xsl
并显示结果。
It's actully quite a mess when it comes to transformations in browsers imo, bad support and only XSLT 1.0. The MIME type See Alejandros comment below.text/xsl
is not even "correct" but it's the one most commonly supported by browsers. The correct MIME type should be application/xslt+xml
but that's not supported in any (?) browser to my knowledge.
当涉及到浏览器中的转换 imo、糟糕的支持和只有 XSLT 1.0 时,这实际上是一团糟。MIME 类型请参阅下面的亚历杭德罗斯评论。text/xsl
甚至不是“正确的”,但它是浏览器最常支持的类型。正确的 MIME 类型应该是,application/xslt+xml
但据我所知,在任何 (?) 浏览器中都不支持。