Java中的 XSL 处理器
在Java中,XSLProcessor 是一个扩展Java.lang.Object类的类。它提供了创建 XSLStylesheet 对象和转换输入 XML 文档的方法。 XSL 代表可扩展样式表语言,它用于创建样式表,就像 CSS 一样,它描述了如何显示给定类型的 XML 文档。
--> java.lang Package
--> Object Class
--> XSL Processor Class extends Object Class
XSLProcessor 类的方法
Methods | Description |
---|---|
getParam(java.lang.String name) | Uses getParam(String, String); |
getParam(java.lang.String uri, java.lang.String name) | Gets the value of a top-level stylesheet parameter. |
XSLStylesheet newXSLStylesheet(java.io.InputStream xsl) | Constructs an XSLStylesheet using the given Inputstream XSL function document(”) is not supported as there is no way to re-access the input Stylesheet as XMLDocument. |
XSLStylesheet newXSLStylesheet(java.io.Reader xsl) | Constructs an XSLStylesheet using the given Reader XSL function document(”) is not supported as there is no way to re-access the input Stylesheet as XMLDocument. |
newXSLStylesheet(java.net.URL xsl) | Constructs an XSLStylesheet using the given URL |
newXSLStylesheet(XMLDocument xsl) | Constructs an XSLStylesheet using the given XMLDocument |
processXSL(XSLStylesheet xsl, java.io.InputStream xml, java.net.URL ref) | Transforms the input XML document using given InputStream and stylesheet. |
processXSL(XSLStylesheet xsl, java.io.Reader xml, java.net.URL ref) | Transforms the input XML document using given Reader and stylesheet. |
processXSL(XSLStylesheet xsl, java.net.URL xml, java.net.URL ref) | Transforms the input XML document using given URL and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocument xml) | Transforms the input XML document using given XMLDocument and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocument xml, ContentHandler handler) | Transforms the input XML document using given XMLDocument and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocumentFragment inp) | Transforms the input XML document using given XMLDocument and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocumentFragment xml, java.io.OutputStream os) | Transforms the input XML using given XMLDocumentFragment and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocumentFragment xml, java.io.PrintWriter pw) | Transforms the input XML using given XMLDocumentFragment and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocumentFragment inp, XMLDocumentHandler handler) | Transforms the input XML document using given XMLDocument and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocument xml, java.io.OutputStream os) | Transforms the input XML document using given XMLDocument and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocument xml, java.io.PrintWriter pw) | Transforms the input XML document using given XMLDocument and stylesheet. |
processXSL(XSLStylesheet xsl, XMLDocument xml, XMLDocumentHandler handler) | Transforms the input XML document using given XMLDocument and stylesheet. |
processXSL(XSLStylesheet xsl, XMLElement inp) | Transforms the input XML document using given XMLDocument and stylesheet. |
processXSL(XSLStylesheet xsl, XMLElement inp, ContentHandler handler) | Transforms the input XML document using given XMLElement and stylesheet. |
processXSL(XSLStylesheet xsl, XMLElement xml, java.io.OutputStream os) | Transforms the input XML using given XMLElement and stylesheet. |
processXSL(XSLStylesheet xsl, XMLElement xml, java.io.PrintWriter pw) | Transforms the input XML using given XMLElement and stylesheet. |
processXSL(XSLStylesheet xsl, XMLElement xml, XMLDocumentHandler handler) | Transforms the input XML document using given XMLElement and stylesheet. |
removeParam(java.lang.String uri, java.lang.String name) | Removes the value of a top-level stylesheet parameter. |
resetParams() | Resets all the params set. |
setBaseURL(java.net.URL url) | Sets the base url to resolve include/import hrefs EntityResolver if set is used before using the base url. |
setEntityResolver(EntityResolver eResolver) | Sets the entity resolver to resolve include/import hrefs if not set, base url (if set) is used. |
setErrorStream(java.io.OutputStream out) | Generate an output stream for the output of warnings. |
setLocale(java.util.Locale locale) | This method applications can use this to set the locale for error reporting. |
setOutputEncoding(java.lang.String externalenc) | In this method if output encoding is not specified in XSL document, output charset as “UTF-8” in META element by default. |
setParam(java.lang.String uri, java.lang.String name, java.lang.Object value) | Sets the value of a top-level stylesheet parameter. |
setXSLTVersion(XSLProcessor.XSLTVersion version) | Sets the specification version to be used for transformation. |
showWarnings(boolean flag) | Used when switching to determine whether to output warnings. |
例子
Java
// Java Program to Illustrate XSLSample Class
// Class
public class XSLSample {
// Main driver method
public static void main(String args[]) throws Exception
{
if (args.length < 2) {
System.err.println(
"Usage: java XSLSample xslFile xmlFile.");
System.exit(1);
}
// Creating a new XSLProcessor
XSLProcessor processor = new XSLProcessor();
// Register a base URL to resolve relative
// references processor.setBaseURL(baseURL);
// Or register an org.xml.sax.EntityResolver to
// resolve relative references
// processor.setEntityResolver(myEntityResolver);
// Register an error log
// processor.setErrorStream(new
// FileOutputStream("error.log"));
// Set any global parameters to the processor
// processor.setParam(namespace, param1, value1);
// processor.setParam(namespace, param2, value2);
// resetParam is for multiple XML documents with
// different parameters
String xslFile = args[0];
String xmlFile = args[1];
// Create a XSLStylesheet
// The stylesheet can be created using one of
// following inputs:
//
// XMLDocument xslInput = /* using DOMParser; see
// later in this code */ URL xslInput = new
// URL(xslFile); Reader xslInput = new
// FileReader(xslFile);
InputStream xslInput = new FileInputStream(xslFile);
XSLStylesheet stylesheet
= processor.newXSLStylesheet(xslInput);
// Prepare the XML instance document
// The XML instance can be given to the processor
// in one of
// following ways:
//
// URL xmlInput = new URL(xmlFile);
// Reader xmlInput = new FileReader(xmlFile);
// InputStream xmlInput = new
// FileInputStream(xmlFile); Or using DOMParser
DOMParser parser = new DOMParser();
parser.retainCDATASection(false);
parser.setPreserveWhitespace(true);
parser.parse(xmlFile);
XMLDocument xmlInput = parser.getDocument();
// Transform the XML instance
// The result of the transformation can be one of
// the following:
//
// 1. Return a XMLDocumentFragment
// 2. Print the results to a OutputStream
// 3. Report SAX Events to a ContentHandler
// 1. Return a XMLDocumentFragment
XMLDocumentFragment res;
res = processor.processXSL(stylesheet, xmlInput);
// Print the result to System.out
res.print(System.out);
// 2. Print the results to a OutputStream
// processor.processXSL(stylesheet, xmlInput,
// System.out);
// 3. Report SAX Events to a ContentHandler
// ContentHandler cntHandler = new
// MyContentHandler();
// processor.processXSL(stylesheet, xmlInput,
// cntHandler);
// Display message for successful execution
System.out.println("Executed Successfully");
}
}
输出:
Executed Successfully
XSL 还添加:
- 一种考虑 XML 文档的转换语言:XLST 代表可扩展样式表语言转换。最初,它旨在执行复杂的样式操作,例如创建目录和索引,现在它被用作通用 XML 处理语言。因此,除了 XSL 之外,XSLT 被广泛使用,例如从 XML 数据创建 HTML 网页。
- 最新的样式功能,由定义一组称为格式化对象的元素和属性的 XML 文档类型来说明。
用于Java的 XSLT 处理器:
Oracle 为Java、C、C++ 和 PL/SQL 提供 XSLT 处理。本文描述了用于Java的 XSLT 处理器。 XSLT 有两个版本,即 1.0 版本,以及目前正在开发的 2.0 版本,这是 W3C 互联网标准。 XSLT 也使用导航语言 XPath,并有相应的 1.0 和 2.0 版本。在Java中,XSLT 和 XPath 都由 XSLT 处理器实现。