Class XsltTransformer

java.lang.Object
cloud.opencode.base.xml.transform.XsltTransformer

public final class XsltTransformer extends Object
XSLT Transformer - XSLT stylesheet transformation XSLT 转换器 - XSLT 样式表转换

This class provides XSLT transformation capabilities with a fluent API.

此类提供具有流式 API 的 XSLT 转换功能。

Usage Examples | 使用示例:

// Transform XML using XSLT stylesheet
String result = XsltTransformer.of(xslt)
    .parameter("title", "My Document")
    .transform(xml);

// Transform to file
XsltTransformer.of(xsltPath)
    .transform(xmlPath, outputPath);

// Transform to XmlDocument
XmlDocument doc = XsltTransformer.of(xslt)
    .transformToDocument(xml);

Features | 主要功能:

  • XSLT stylesheet transformation - XSLT 样式表转换
  • Fluent API with parameter support - 带参数支持的流式 API
  • Transform to string, file, or XmlDocument - 转换到字符串、文件或 XmlDocument

Security | 安全性:

  • Thread-safe: No (mutable parameters) - 线程安全: 否(可变参数)
  • Null-safe: No (throws on null XSLT/XML) - 空值安全: 否(null XSLT/XML 抛异常)
Since:
JDK 25, opencode-base-xml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static XsltTransformer of(String xslt)
      Creates a transformer from an XSLT string. 从 XSLT 字符串创建转换器。
      Parameters:
      xslt - the XSLT stylesheet | XSLT 样式表
      Returns:
      a new transformer | 新转换器
    • of

      public static XsltTransformer of(Path path)
      Creates a transformer from an XSLT file. 从 XSLT 文件创建转换器。
      Parameters:
      path - the XSLT file path | XSLT 文件路径
      Returns:
      a new transformer | 新转换器
    • of

      public static XsltTransformer of(InputStream input)
      Creates a transformer from an XSLT input stream. 从 XSLT 输入流创建转换器。
      Parameters:
      input - the XSLT input stream | XSLT 输入流
      Returns:
      a new transformer | 新转换器
    • parameter

      public XsltTransformer parameter(String name, Object value)
      Sets a transformation parameter. 设置转换参数。
      Parameters:
      name - the parameter name | 参数名称
      value - the parameter value | 参数值
      Returns:
      this transformer for chaining | 此转换器以便链式调用
    • parameters

      public XsltTransformer parameters(Map<String,Object> params)
      Sets multiple transformation parameters. 设置多个转换参数。
      Parameters:
      params - the parameters map | 参数映射
      Returns:
      this transformer for chaining | 此转换器以便链式调用
    • clearParameters

      public XsltTransformer clearParameters()
      Clears all parameters. 清除所有参数。
      Returns:
      this transformer for chaining | 此转换器以便链式调用
    • outputProperty

      public XsltTransformer outputProperty(String name, String value)
      Sets an output property. 设置输出属性。
      Parameters:
      name - the property name | 属性名称
      value - the property value | 属性值
      Returns:
      this transformer for chaining | 此转换器以便链式调用
    • method

      public XsltTransformer method(String method)
      Sets the output method (xml, html, text). 设置输出方法(xml、html、text)。
      Parameters:
      method - the output method | 输出方法
      Returns:
      this transformer for chaining | 此转换器以便链式调用
    • encoding

      public XsltTransformer encoding(String encoding)
      Sets the output encoding. 设置输出编码。
      Parameters:
      encoding - the encoding | 编码
      Returns:
      this transformer for chaining | 此转换器以便链式调用
    • indent

      public XsltTransformer indent(boolean indent)
      Sets whether to indent output. 设置是否缩进输出。
      Parameters:
      indent - whether to indent | 是否缩进
      Returns:
      this transformer for chaining | 此转换器以便链式调用
    • omitXmlDeclaration

      public XsltTransformer omitXmlDeclaration(boolean omit)
      Sets whether to omit XML declaration. 设置是否省略 XML 声明。
      Parameters:
      omit - whether to omit | 是否省略
      Returns:
      this transformer for chaining | 此转换器以便链式调用
    • transform

      public String transform(String xml)
      Transforms an XML string. 转换 XML 字符串。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      the transformed result | 转换结果
    • transform

      public String transform(XmlDocument document)
      Transforms an XmlDocument. 转换 XmlDocument。
      Parameters:
      document - the XML document | XML 文档
      Returns:
      the transformed result | 转换结果
    • transform

      public String transform(InputStream input)
      Transforms from input stream to string. 从输入流转换为字符串。
      Parameters:
      input - the input stream | 输入流
      Returns:
      the transformed result | 转换结果
    • transformFile

      public String transformFile(Path path)
      Transforms from file to string. 从文件转换为字符串。
      Parameters:
      path - the input file path | 输入文件路径
      Returns:
      the transformed result | 转换结果
    • transform

      public void transform(Path input, Path output)
      Transforms from file to file. 从文件转换到文件。
      Parameters:
      input - the input file path | 输入文件路径
      output - the output file path | 输出文件路径
    • transform

      public void transform(String xml, OutputStream output)
      Transforms to an output stream. 转换到输出流。
      Parameters:
      xml - the XML string | XML 字符串
      output - the output stream | 输出流
    • transformToDocument

      public XmlDocument transformToDocument(String xml)
      Transforms an XML string to XmlDocument. 将 XML 字符串转换为 XmlDocument。
      Parameters:
      xml - the XML string | XML 字符串
      Returns:
      the transformed document | 转换后的文档
    • transformToDocument

      public XmlDocument transformToDocument(XmlDocument document)
      Transforms an XmlDocument to XmlDocument. 将 XmlDocument 转换为 XmlDocument。
      Parameters:
      document - the XML document | XML 文档
      Returns:
      the transformed document | 转换后的文档
    • getTemplates

      public Templates getTemplates()
      Gets the underlying Templates object. 获取底层 Templates 对象。
      Returns:
      the Templates | Templates 对象