Understanding XSLT Processing in Java – 了解 Java 中的 XSLT 处理

最后修改: 2023年 9月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

XSLT, which stands for Extensible Stylesheet Language Transformations, is an XML-based language that we can use to transform one XML document into another text document. In this article, we’ll discuss everything that goes into XSLT processing, from setup to advanced techniques.

XSLT 是可扩展样式表语言转换的缩写,是一种基于 XML 的语言,我们可以使用它将一个 XML 文档转换为另一个文本文档。在本文中,我们将讨论 XSLT 处理中从设置到高级技术的所有内容

2. XSLT Fundamentals

2. XSLT 基础

XSLT operates based on the input XML document and an XSLT stylesheet. The XML document contains the data that needs to be transformed, while the stylesheet defines the rules for carrying out the transformation. The transformation process involves applying predefined templates from the stylesheet to parts of the input XML document.

XSLT 基于输入的 XML 文档和 XSLT 样式表运行。XML 文档包含需要转换的数据,而样式表则定义了执行转换的规则。转换过程包括将样式表中的预定义模板应用到输入 XML 文档的各个部分。

An XSLT stylesheet is an XML document that lays down the guidelines and instructions for executing transformations. It consists of elements, including templates, match patterns, and transformation instructions. Templates define how different nodes in the XML document should be transformed, while match patterns determine which nodes should be matched by these templates.

XSLT 样式表是一种 XML 文档,规定了执行转换的准则和指令。它由模板、匹配模式和转换指令等元素组成。模板定义了应如何转换 XML 文档中的不同节点,而匹配模式则决定了这些模板应匹配哪些节点

XSLT stylesheets offer a range of functionalities, such, as logic, loops, variable assignments, and sorting capabilities. These features empower developers to convert XML data into output formats with flexibility.

XSLT 样式表提供一系列功能,如逻辑、循环、变量赋值和排序功能。这些功能使开发人员能够灵活地将 XML 数据转换为输出格式。

3. Loading XML Input and XSLT Stylesheet

3.加载 XML 输入和 XSLT 样式表

To perform an XSLT transformation in Java, let’s load the input XML document and the XSLT stylesheet:

要在 Java 中执行 XSLT 转换,让我们加载输入的 XML 文档和 XSLT 样式表:

Source xmlSource = new StreamSource("input.xml");
Source xsltSource = new StreamSource("stylesheet.xslt");

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);

StreamResult result = new StreamResult("output.html");
transformer.transform(xmlSource, result);
System.out.println("XSLT transformation completed successfully.");

Let’s break down the code in the above snippet. After loading our XML and XSLT documents, we create a TransformerFactory using the TransformerFactory.newInstance() method. This factory method’s purpose is to generate instances of Transformers, which are responsible for carrying out the transformation process.

让我们分解一下上述代码段中的代码。加载 XML 和 XSLT 文档后,我们使用 TransformerFactory.newInstance() 方法创建了一个 TransformerFactory 。该工厂方法的目的是生成负责执行转换过程的 Transformer 实例。

Next, we load the XSLT stylesheet by using a StreamSource to represent the file containing the stylesheet. The TransformerFactory.newTransformer() method compiles this stylesheet into a Transformer instance, which enables us to perform the desired transformation.

接下来,我们使用 StreamSource 表示包含样式表的文件,从而加载 XSLT 样式表。TransformerFactory.newTransformer()方法将该样式表编译为一个Transformer实例,使我们能够执行所需的转换

Next, we utilize another StreamSource that represents the XML file to be transformed as we specify the input XML document. We create a StreamResult object to store the output of our XSLT transformation. In this case, we save the transformed result as a file named output.html.

接下来,我们利用另一个 StreamSource 来表示要转换的 XML 文件,因为我们指定了输入 XML 文档。我们创建一个 StreamResult 对象来存储 XSLT 转换的输出结果。在本例中,我们将转换结果保存为名为 output.html 的文件。

Finally, we initiate the transformation process by calling our Transformer object’s transform() method with both input xmlSource and output result as parameters. The transform() method processes our input XML document and applies the rules defined in our XSLT stylesheet to generate an output, which the method stores in our StreamResult object.

最后,我们以输入 xmlSource 和输出 result 作为参数,调用 Transformer 对象的 transform() 方法,启动转换过程。transform()方法处理输入的 XML 文档,并应用 XSLT 样式表中定义的规则生成输出,该方法将输出存储在 StreamResult 对象中

3.1. The Role of Templates in XSLT Transformation

3.1.模板在 XSLT 转换中的作用

In addition to the aforementioned approach, we can enhance our XSLT transformations by utilizing templates. Precompiled representations of XSLT stylesheets, which offer performance advantages over repeatedly creating new Transformer instances for the same stylesheet, are provided by templates.

除上述方法外,我们还可以利用模板来增强 XSLT 转换。模板提供了 XSLT 样式表的预编译表示,与重复为同一样式表创建新的 Transformer 实例相比,它具有性能优势。

Here’s how we can employ Templates in our XSLT processing:

以下是我们如何在 XSLT 处理中使用 Templates 的方法:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource("stylesheet.xslt");
Templates templates = transformerFactory.newTemplates(xsltSource);        
Transformer transformer = templates.newTransformer();

Source xmlSource = new StreamSource("input.xml");
Result result = new StreamResult("output.html");

transformer.transform(xmlSource, result);

In this updated example, we continue to create a TransformerFactory instance using newInstance(). However, instead of directly creating a Transformer instance, we now use TransformerFactory.newTemplates(xsltSource) to compile advanced XSLT methods into Templates. These precompiled templates can then be used to create multiple Transformer instances.

在这个更新的示例中,我们继续使用 newInstance() 创建 TransformerFactory 实例。不过,我们现在不再直接创建 Transformer 实例,而是使用 TransformerFactory.newTemplates(xsltSource) 将高级 XSLT 方法编译到 Templates 中。然后,这些预编译模板可用于创建多个 Transformer 实例。

This approach offers an advantage when we need to perform multiple transformations using the same stylesheet, as it avoids the overhead of recompiling the stylesheet. The call to templates.newTransformer() creates a new Transformer instance from the precompiled Templates, allowing us to execute the transformation as usual.

当我们需要使用同一样式表执行多个转换时,这种方法具有优势,因为它避免了重新编译样式表的开销。调用 templates.newTransformer() 将从预编译的模板中创建一个新的 Transformer 实例,使我们可以像往常一样执行转换。

4. Configuring Transformation Parameters and Options

4.配置转换参数和选项

When performing XSLT transformations, there may be cases where we need to pass values to the stylesheet or modify how the transformation is carried out. We can achieve this in Java by using the javax.xml.transform.Transformer class.

在执行 XSLT 转换时,我们可能需要向样式表传递值或修改转换的执行方式。我们可以在 Java 中使用 javax.xml.transform.Transformer 类来实现这一目的。

We can use the Transformer.setParameter(String name, Object value) method to assign values to the Transformer‘s parameters. Here, the name refers to the parameter name specified in the XSLT stylesheet, and the value represents the desired value for that parameter.

我们可以使用 Transformer.setParameter(String name, Object value) 方法为 Transformer 的参数赋值。在这里,name 指的是 XSLT 样式表中指定的参数名称,而 value 表示该参数的期望值

Let’s see a quick example that demonstrates how to assign a parameter:

下面我们来看一个快速示例,演示如何分配参数:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
transformer.setParameter("companyName", "Baeldung Corporation");

In this example, we’ve set up the companyName parameter with a value of “Baeldung Corporation” using the setParameter() method. This enables us to provide data to the XSLT stylesheet during the transformation process.

在本示例中,我们使用 setParameter() 方法将 companyName 参数设置为 “Baeldung Corporation” 值。这样,我们就能在转换过程中为 XSLT 样式表提供数据。

To modify the settings for transforming options, such as activating indentation for output or controlling output escaping, we can then use methods like Transformer.setOutputProperty(String name, String value).

要修改转换选项的设置,例如激活输出缩进或控制输出转义,我们可以使用 Transformer.setOutputProperty(String name, String value) 等方法。

Let’s see another example that shows how to enable output indentation:

让我们再看一个例子,看看如何启用输出缩进:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

In this example, we configure the output to be indented using the setOutputProperty() method with the OutputKeys.INDENT property set to “yes”.

在此示例中,我们使用 setOutputProperty() 方法配置输出缩进,并将 OutputKeys.INDENT 属性设置为 “yes”

As we’ve seen throughout this subsection, we can customize the transformation process and control the output format according to our specific requirements by configuring parameters and options.

正如我们在本小节中所看到的,我们可以通过配置参数和选项来定制转换过程,并根据具体要求控制输出格式。

5. Conclusion

5.结论

In this article, we delved into the realm of XSLT processing using Java and explored its capabilities in converting XML documents to different formats. By understanding the basics of XSLT and becoming proficient in XPath expressions by exploring the wide range of Java APIs available, we unlock the potential to perform efficient and impactful transformations.

在本文中,我们深入探讨了使用 Java 进行 XSLT 处理的领域,并探索了其将 XML 文档转换为不同格式的功能。通过了解 XSLT 的基础知识,并通过探索广泛的 Java API 来熟练掌握 XPath 表达式,我们将释放执行高效且有影响力的转换的潜力。

As always, the code is available over on GitHub.

与往常一样,代码可在 GitHub 上获取。