<ph type="x-smartling-placeholder"></ph>
您正在查看 Apigee Edge 文档。
转到
Apigee X 文档。 信息

内容
XSLTransform 政策将自定义可扩展样式表语言转换 (XSLT) 应用于 XML 消息,让您可以将其从 XML 格式转换为其他格式(如 XML、HTML 或纯文本格式)。该政策通常用于集成支持 XML 的应用,但要求对相同的数据使用不同的 XML 格式。
示例
以下示例展示了一个 XSL 转换流中的所有资源。
XSL 政策 ->
<XSL name="TransformXML"> <ResourceURL>xsl://my_transform.xsl</ResourceURL> <Source>request</Source> </XSL>
简单的 XSL 政策。转到下一个示例,查看政策中引用的 XSLT 样式表 (my_transform.xsl
)。<Source>
元素很重要。例如,如果要转换的 XML 位于响应中,除非您将来源设置为 response
(且政策附加到响应流),否则不会发生转换。但在这种情况下,要转换的 XML 位于请求中。
XSLT 样式表 ->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="/"> <xsl:text><Life></xsl:text> <xsl:value-of select="$newline"/> <xsl:text>Here are the odd-numbered items from the list:</xsl:text> <xsl:value-of select="$newline"/> <xsl:for-each select="list/listitem"> <xsl:if test="(position() mod 2) = 1"> <xsl:number format="1. "/> <xsl:value-of select="."/> <xsl:value-of select="$newline"/> </xsl:if> </xsl:for-each> <xsl:text></Life></xsl:text> </xsl:template> </xsl:stylesheet>
政策中引用的 my_transform.xsl
样式表。转到下一个示例以查看传入 XML 消息的示例。
消息 ->
<?xml version="1.0"?> <list> <title>A few of my favorite albums</title> <listitem>A Love Supreme</listitem> <listitem>Beat Crazy</listitem> <listitem>Here Come the Warm Jets</listitem> <listitem>Kind of Blue</listitem> <listitem>London Calling</listitem> <listitem>Remain in Light</listitem> <listitem>The Joshua Tree</listitem> <listitem>The Indestructible Beat of Soweto</listitem> </list>
请求中的示例消息(在第一个示例中,在政策的 <Source>request</Source>
元素中指示)。
转换后的消息
<Life> Here are the odd-numbered items from the list: 1. A Love Supreme 3. Here Come the Warm Jets 5. London Calling 7. The Joshua Tree </Life>
来自这些示例的 XSLT 样式表之后的转换消息应用于 XML 消息。
元素参考
使用以下元素配置 XSLTransformation 政策。
字段名称 | 说明 | |||
---|---|---|---|---|
名称(必填) |
政策的名称。您可以在名称中使用的字符仅限于 A-Z0-9._\-$ % 。不过,管理界面会强制实施一些额外限制
例如,自动移除非字母数字字符。
|
|||
来源(可选) |
包含需要从中提取信息的消息。通常,此值会设置为 request 或 response ,具体取决于要转换的消息是入站还是出站消息。
|
|||
OutputVariable(可选) |
存储转换输出的变量。OutputVariable 类型不能为消息,也就是说,不能为“message”、“request”或“response”。您应将此元素设为自定义变量,然后再使用该变量。 如需将消息内容替换为转换输出,请删除此元素。例如,如果您要将消息转换为 HTML,请勿添加此元素。 |
|||
ResourceURL(必需) | 用于转换消息的 XSLT 文件。 | |||
参数(可选) | ignoreUnresolvedVariables(可选) |
忽略 XSLT 脚本说明中任何未解决的变量错误。
有效值:true/false
默认值:false
|
||
参数(可选) | 名称(必填) |
参数支持在样式表中使用 XSLT 参数,其中,您在政策中添加的名称是 XSLT 参数的名称。例如,如果您输入名称“uid”,则您的 XSL 可能如下所示:( 该参数会通过对上下文变量的引用(由 如需查看示例和更多信息,请参阅 http://community.apigee.com/questions/1860/how-should-the-the-optional-parameters-on-the-xsl.html#answer-1864。 |
||
ref(可选) |
指定从变量中计算值的引用。例如,如果“uid”参数需要从名为“authn.uid”的变量中获取其值,则参数元素将如下所示: 如果您使用此属性,请勿使用值属性。 |
|||
值(可选) |
您可以使用此属性对参数值进行硬编码。 如果您使用此属性,请勿使用 ref 属性。 |
使用说明
XSLT 是在独立的 .xsl
文件中实现的,该文件存储在 API 代理中的 /resources/xsl
下。XSL 政策仅引用 XSL 文件。如需了解详情,请参阅资源文件。
这项 XSL 政策需要两个输入:
- XSLT 样式表的名称(包含一组转换规则)存储在 API 代理的
/resources/xsl
下 - 要转换的 XML 的来源(通常是请求或响应消息)
不支持 <xsl:include>
和 <xsl:import>
。
Apigee Edge 依赖于 Saxon XSLT 处理器,并且支持 XSLT 1.0 和 2.0。
错误参考信息
Runtime errors
These errors can occur when the policy executes.
Fault code | HTTP status | Cause | Fix |
---|---|---|---|
steps.xsl.XSLSourceMessageNotAvailable |
500 |
This error occurs if the message or string variable specified in the <Source> element of the
XSL Transform policy is either out of scope (not available in the specific flow where the
policy is being executed) or can't be resolved (is not defined).
|
build |
steps.xsl.XSLEvaluationFailed |
500 | This error occurs if the input XML payload is unavailable/malformed or the XSLTransform policy fails/is unable to transform the input XML file based on the transformation rules provided in the XSL file. There could be many different causes for the XSLTransform policy to fail. The reason for failure in the error message will provide more information on the cause. | build |
Deployment errors
These errors can occur when you deploy a proxy containing this policy.
Error name | Cause | Fix |
---|---|---|
XSLEmptyResourceUrl |
If the <ResourceURL> element in the XSL Transform policy is empty, then the
deployment of the API proxy fails. |
build |
XSLInvalidResourceType |
If the resource type specified in the <ResourceURL> element of the XSL Transform
policy is not of type xsl , then the deployment of the API proxy fails. |
build |