基本的な XSLT 操作の実行
XFactory.prepareXSLT メソッドを使用して作成された XSLTExecutable インスタンスを使用して、 XSLT 変換を実行できます。
このタスクについて
XSLT スタイルシートは、JAXP ソース・オブジェクトを使用して XFactory.prepareXSLT メソッドに渡すことができます。 結果として得られる XSLTExecutable インスタンスはスレッド・セーフであり、 複数の入力文書を変換するために再利用できます。
手順
例
以下に、インタープリットされた変換の作成と実行を行う基本的な例を示します。
// Create the factory
XFactory factory = XFactory.newInstance();
// Create a StreamSource for the stylesheet
StreamSource stylesheet = new StreamSource("simple.xsl");
// Create an XSLT executable for the stylesheet
XSLTExecutable executable = factory.prepareXSLT(stylesheet);
// Create the input source
Source input = new StreamSource("simple.xml");
// Create the result
Result result = new StreamResult(System.out);
// Execute the transformation
executable.execute(input, result);
以下に、コンパイルされた変換の作成と実行を行う基本的な例を示します。
// Create the factory
XFactory factory = XFactory.newInstance();
// Create a StreamSource for the stylesheet
StreamSource stylesheet = new StreamSource("simple.xsl");
// Create a new static context
XStaticContext staticContext = factory.newStaticContext();
// Enable the compiler
staticContext.setUseCompiler(true);
// Create an XSLT executable for the stylesheet
XSLTExecutable executable = factory.prepareXSLT(stylesheet, staticContext);
// Create the input source
Source input = new StreamSource("simple.xml");
// Create the result
Result result = new StreamResult(System.out);
// Execute the transformation
executable.execute(input, result);
以下に、ID 変換を作成する基本的な例を示します。
// Create the factory
XFactory factory = XFactory.newInstance();
// Create the item factory
XItemFactory itemFactory = factory.getItemFactory();
// Create the input source
Source input = new StreamSource("simple.xml");
// Create the XItemView object from the input source
XItemView item = itemFactory.item(input);
// Create an XOutputParameters object
XOutputParameters params = factory.newOutputParameters();
// Set parameters
params.setMethod("xml");
params.setEncoding("UTF-8");
params.setIndent(true);
// Create the result
Result result = new StreamResult(System.out);
// Serialize to the result
item.exportItem(result, params);
以下に、スキーマ認識変換を作成する基本的な例を示します。
// Create the factory
XFactory factory = XFactory.newInstance();
// Enable validation
factory.setValidating(true);
// Create the schema source
StreamSource schema = new StreamSource("schema.xsd");
// Register the schema
factory.registerSchema(schema);
// Create the stylesheet source
StreamSource stylesheet = new StreamSource("schema.xsl");
// Create an XSLT executable for the stylesheet
XSLTExecutable executable = factory.prepareXSLT(stylesheet);
// Create the input source
StreamSource input = new StreamSource("schema.xml");
// Create the result
StreamResult result = new StreamResult(System.out);
// Execute the transformation
executable.execute(input, result);