TransformXml

文档中心 › 组件索引 › TransformXml

TransformXml Processor

类型全限定名:cn.org.acal.lynxus.processors.standard.TransformXml

所属 Bundle:cn.org.acal.lynxus:lynxus-standard-nar:2.11.8

描述

将提供的 XSLT 文件应用到 FlowFile XML 负载。 使用转换后的内容创建一个新的 FlowFile,并将其路由到“成功”关系。 如果 XSL 转换失败,原始 FlowFile 将路由到“失败”关系

标签

xmlxslttransform

属性

下表中,加粗的属性为必填项;其余为可选项。表格同时标注默认值、是否支持表达式语言,以及是否为敏感属性(值会被加密)。

名称默认值可选值描述
XSLT Document提供 XSLT 文件的名称(包括完整路径)或要应用于 FlowFile XML内容的实际 XSLT。'XSLT Document' 和 'XSLT Lookup' 属性中必须定义其中一个。
支持表达式语言:是(使用 FlowFile 属性与环境/系统变量求值)
XSLT Lookup控制器服务 API:
StringLookupService
cn.org.acal.lynxus-lynxus-standard-services-api-nar-2.11.8
用于存储 XSLT 定义的控制器查找服务。'XSLT Document' 和 'XSLT Lookup' 属性中必须定义其中一个。警告:请注意,查找控制器服务不应用于存储大型 XSLT 文件。
XSLT 查找键用于从 XSLT 查找控制器检索 XSLT 定义的键。使用 XSLT 控制器属性时必须设置此属性。
支持表达式语言:是(使用 FlowFile 属性与环境/系统变量求值)
Indenttrue
  • true
  • false
是否缩进输出。
Secure Processingtrue
  • true
  • false
是否缓解各种与 XML 相关的攻击,例如 XXE(XML 外部实体)攻击。
Cache Size10要缓存的样式表的最大数量。 零禁用缓存。
Cache Duration60 secs缓存 TTL(生存时间)或上次访问后样式表在缓存中保留的时间。

动态属性

动态属性允许用户自定义属性的名称与值。

名称值描述
XSLT 转换参数名称XSLT 转换参数的值这些 XSLT 参数被传递给转换器。有关更多信息,请参阅附加详细信息。

关系 (Relationships)

名称描述自动终止
failure如果 FlowFile 由于任何原因处理失败(例如,FlowFile 不是有效的 XML),它将被路由到此关系否
success具有转换内容的 FlowFile 将被路由到此关系否

状态管理

该组件不存储状态。

受限 (Restricted)

该组件不受限。

输入要求

该组件要求有传入连接。

详细说明

TransformXml

带参数的 XSLT 转换

XSLT 参数是 XSLT 样式表中的占位符,允许在运行时把外部值传入转换过程。 只要参数通过顶层的 xsl:param 元素声明,就可以在样式表内以普通变量的形式 使用,语法为 $name。如果没有这样的声明,提供的参数值会被静默忽略。

定义了参数的 XSLT

考虑以下 XML

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <item>Some data</item>
</data>

以及 XSLT 样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="customParam" select="'From XSLT'"/>
    <xsl:template match="/">
        <root>
            <message>
                Value Selected: <xsl:value-of select="$customParam"/>
            </message>
        </root>
    </xsl:template>
</xsl:stylesheet>

如果添加参数名为 customParam、参数值为 From LYNXUS 的键值对作为动态属性, 那么输出将是

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <message>
        Value Selected: From LYNXUS</message>
</root>

注意,尽管声明的 xsl:param 元素有默认值 From XSLT, customParam 参数的值还是变成了 From LYNXUS。

未定义参数的 XSLT

如果 XSLT 样式表没有声明顶层的 xsl:param 元素

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <root>
            <message>
                Value Selected:
            </message>
        </root>
    </xsl:template>
</xsl:stylesheet>

即使添加参数名为 customParam、参数值为 From LYNXUS 的键值对作为动态属性, 它们也会被忽略,输出将是

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <message>
  Value Selected:
 </message>
</root>

如果参数没有在 xsl:param 元素中声明,却在 XSLT 样式表中尝试使用,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <root>
            <message>
                Value Selected: <xsl:value-of select="$customParam"/>
            </message>
        </root>
    </xsl:template>
</xsl:stylesheet>

即使添加参数名为 customParam、参数值为 From LYNXUS 的键值对作为动态属性, 转换也会失败,错误消息中包含 Variable $customParam has not been declared 字样。

带必需类型的 XSLT 参数

从 XSLT 2.0 开始,参数新增了 as 属性,可以指定参数的类型。以下是一些较常见的 受支持类型,以及各自的有效取值:

类型名 类型取值
xs:string 任意字符串。
xs:boolean 小写的 true 或 false。
xs:integer 带符号的整数值。
xs:float 32 位 IEEE 单精度浮点数。
xs:double 64 位 IEEE 双精度浮点数。
xs:date ISO 8601 格式 YYYY-MM-DD 的日期。
xs:dateTime ISO 8601 格式的日期时间:YYYY-MM-DDThh:mm:ss,可选用 Z 表示 UTC 或偏移量(如 -05:00)。
xs:time HH:mm:ss 或 HH:mm:ss.SSS 格式的时间,可选用 Z 表示 UTC 或偏移量(如 -05:00)。
xs:anyURI 合法的 URI。

以下 XSLT 样式表演示了在 XSLT 中指定类型,并用动态属性填充其值。

考虑以下 XSLT 样式表

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:param name="stringParam" as="xs:string" select="'From XSLT'"/>
    <xsl:param name="integerParam" as="xs:integer" select="0"/>
    <xsl:param name="floatParam" as="xs:float" select="0.0"/>
    <xsl:param name="doubleParam" as="xs:double" select="0.0"/>
    <xsl:param name="booleanParam" as="xs:boolean" select="false"/>
    <xsl:param name="dateParam" as="xs:date" select="xs:date('1970-01-01')"/>
    <xsl:param name="dateTimeParam" as="xs:dateTime" select="xs:dateTime('1970-01-01T00:00:00')"/>
    <xsl:param name="timeParam" as="xs:time" select="current-time()"/>
    <xsl:param name="utcTimeParam" as="xs:time" select="current-time()"/>
    <xsl:param name="offsetTimeParam" as="xs:time" select="current-time()"/>
    <xsl:param name="uriParam" as="xs:anyURI"/>
    <xsl:template match="/">
        <report>
            <message>Param of type xs:string value is <xsl:value-of select="$stringParam"/></message>
            <message>Param of type xs:integer value is <xsl:value-of select="$integerParam"/></message>
            <message>Param of type xs:float value is <xsl:value-of select="$floatParam"/></message>
            <message>Param of type xs:double value is <xsl:value-of select="$doubleParam"/></message>
            <message>Param of type xs:boolean value is <xsl:value-of select="$booleanParam"/></message>
            <message>Param of type xs:date value is <xsl:value-of select="$dateParam"/></message>
            <message>Param of type xs:dateTime value is <xsl:value-of select="$dateTimeParam"/></message>
            <message>Param of type xs:time value is <xsl:value-of select="$timeParam"/></message>
            <message>Param of type xs:time UTC value is <xsl:value-of select="$utcTimeParam"/></message>
            <message>Param of type xs:time offset value is <xsl:value-of select="$offsetTimeParam"/></message>
            <message>Param of type xs:anyURI <xsl:value-of select="$uriParam"/></message>
        </report>
    </xsl:template>
</xsl:stylesheet>

并使用之前的 XML

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <item>Some data</item>
</data>

如果添加以下参数名与参数值的键值对作为动态属性

参数名 参数值
stringParam From LYNXUS
integerParam 100
floatParam 123.456
doubleParam 12.78e-2
booleanParam true
dateParam 2026-01-01
dateTimeParam 2026-01-01T00:00:00
timeParam 12:34:56.789
utcTimeParam 12:34:56Z
offsetTimeParam 12:34:56-05:00
uriParam http://from-lynxus.com

那么产生的 XML 将是

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <message>Param of type xs:string value is From LYNXUS</message>
    <message>Param of type xs:integer value is 100</message>
    <message>Param of type xs:float value is 123.456</message>
    <message>Param of type xs:double value is 0.1278</message>
    <message>Param of type xs:boolean value is true</message>
    <message>Param of type xs:date value is 2026-01-01</message>
    <message>Param of type xs:dateTime value is 2026-01-01T00:00:00</message>
    <message>Param of type xs:time value is 12:34:56.789</message>
    <message>Param of type xs:time UTC value is 12:34:56Z</message>
    <message>Param of type xs:time offset value is 12:34:56-05:00</message>
    <message>Param of type xs:anyURI http://from-lynxus.com</message>
</report>

请注意,XSLT 3.0 中为 xsl:param 元素添加的 "static" 属性不适用于 LYNXUS 的动态属性。因为顾名思义,static 意味着参数值必须在 XSLT 编译时确定, 而动态属性是在实际转换运行时提供的。