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书并没有具体说明这一点!谢谢你!
4 个解决方案
#1
3
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代码。当您运行时,执行此代码。将创建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);
}
}
#2
2
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
样式表保存为demo。xslt位于与XML文件相同的目录中
<?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中是适用的。
#3
2
Dimitre's answer is what you need. But here you have an example:
迪米特里的答案是你所需要的。这里有个例子
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文档。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.
在我看来,当涉及到浏览器的转换、糟糕的支持以及仅仅是XSLT 1.0时,情况就相当糟糕了。MIME类型的文本/xsl甚至“不正确”,但它是浏览器最普遍支持的。正确的MIME类型应该是application/xslt+xml,但据我所知,任何(?)浏览器都不支持这种类型。请参阅下面的alejandro评论。
#4
1
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处理器的浏览器,并且不支持类型=“text/xsl”伪属性的xml样式表PI。
For more information read the W3C spec on "Associating Style Sheets with XML Documents"
有关更多信息,请参阅“将样式表与XML文档关联”的W3C规范。
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文件(做足功课,学习如何从浏览器打开本地文件),并使用“查看源”或类似的命令检查结果。
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、oXygen、Visual Studio……等。
#1
3
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代码。当您运行时,执行此代码。将创建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);
}
}
#2
2
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
样式表保存为demo。xslt位于与XML文件相同的目录中
<?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中是适用的。
#3
2
Dimitre's answer is what you need. But here you have an example:
迪米特里的答案是你所需要的。这里有个例子
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文档。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.
在我看来,当涉及到浏览器的转换、糟糕的支持以及仅仅是XSLT 1.0时,情况就相当糟糕了。MIME类型的文本/xsl甚至“不正确”,但它是浏览器最普遍支持的。正确的MIME类型应该是application/xslt+xml,但据我所知,任何(?)浏览器都不支持这种类型。请参阅下面的alejandro评论。
#4
1
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处理器的浏览器,并且不支持类型=“text/xsl”伪属性的xml样式表PI。
For more information read the W3C spec on "Associating Style Sheets with XML Documents"
有关更多信息,请参阅“将样式表与XML文档关联”的W3C规范。
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文件(做足功课,学习如何从浏览器打开本地文件),并使用“查看源”或类似的命令检查结果。
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、oXygen、Visual Studio……等。