XSLT 策略示例
XSLT 策略的 OpenAPI 定义的示例。
注:
- 此处所述的与上下文 API Connect 交互的机制旨在适用于 v5-compatible 网关,同时也为针对 API Gateway 创建的 API 提供 v5 兼容性。 v5-compatible 网关在“Enterprise API Connect as a Service”中不可用,因此您必须使用 《在 GatewayScript 和XSLT策略中使用上下文变量 》一文中所述的机制,并访问 DataPower API Gateway。 对于 API Gateway API,此处列出的 v5-compatibility 函数仅用于 v5 的迁移。
- XML 规范 https://www.w3.org/TR/xml/ 并未规定 XML 命名空间 (XMLNS) 属性的推荐顺序。 最佳实践是,在编写定制解析代码时,不依赖于 XMLNS 属性的顺序。
不含上下文当前有效内容的简单示例
在以下示例中,XSLT 输入文档不使用上下文当前有效内容(无输入):
- xslt:
title: example xslt
source: |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Hello>World!</Hello>
</xsl:template>
</xsl:stylesheet>并置与变换
以下示例显示了更复杂的 XSLT 变换源,其中样式表将两个输入字符串并置在一起,并将第三个输入字符串变换为客户机的 IP 地址:
- xslt:
title: xslt
input: true
source: |
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xslt"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs4xs="http://www.w3.org/2001/XMLSchema"
xmlns:io="http://xformMessage"
xmlns:map="http://xformMessage/xform"
xmlns:msl="http://www.ibm.com/xmlmap"
exclude-result-prefixes="fn dp dp map xalan msl"
version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<!-- root wrapper template -->
<xsl:template match="/">
<msl:datamap>
<xsl:choose>
<xsl:when test="not(msl:datamap/dataObject[1]/@xsi:nil)">
<xsl:element name="dataObject">
<xsl:attribute name="xsi:type">
<xsl:value-of select="'io:data'"/>
</xsl:attribute>
<xsl:call-template name="map:xform">
<xsl:with-param name="data" select="msl:datamap/dataObject[1]"/>
</xsl:call-template>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="dataObject">
<xsl:attribute name="xsi:type">
<xsl:value-of select="'io:data'"/>
</xsl:attribute>
<xsl:attribute name="xsi:nil">
<xsl:text>true</xsl:text>
</xsl:attribute>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</msl:datamap>
</xsl:template>
<!-- This rule represents a type mapping: "data" to "io:data". -->
<xsl:template name="map:xform">
<xsl:param name="data"/>
<!-- a simple data mapping: "$data/StringOne"(string) to "StringOne"(string) -->
<xsl:if test="$data/StringOne">
<StringOne>
<xsl:value-of select="concat($data/StringOne, $data/StringTwo)"/>
</StringOne>
</xsl:if>
<!-- a simple mapping with no associated source: to "StringTwo"(string) -->
<StringTwo>
<xsl:value-of select="dp:client-ip-addr()"/>
</StringTwo>
<!-- a simple data mapping: "$data/NumberOne"(int) to "NumberOne"(int) -->
<xsl:if test="$data/NumberOne">
<NumberOne>
<xsl:value-of select="$data/NumberOne"/>
</NumberOne>
</xsl:if>
<!-- a simple data mapping: "$data/NumberTwo"(int) to "NumberTwo"(int) -->
<xsl:if test="$data/NumberTwo">
<NumberTwo>
<xsl:value-of select="$data/NumberTwo"/>
</NumberTwo>
</xsl:if>
<!-- a simple data mapping: "$data/NumberThree"(int) to "NumberThree"(int) -->
<xsl:if test="$data/NumberThree">
<NumberThree>
<xsl:value-of select="$data/NumberThree"/>
</NumberThree>
</xsl:if>
</xsl:template>
<!-- ***************** Utility Templates ****************** -->
<!-- copy the namespace declarations from the source to the target -->
<xsl:template name="copyNamespaceDeclarations">
<xsl:param name="root"/>
<xsl:for-each select="$root/namespace::*[not(name() = '')]">
<xsl:copy/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
获取查询参数值并引用上下文变量
以下示例显示完整的 OpenAPI 源文件。 该 API 包含在 XSLT 中获取查询参数值的 XSLT 策略,并且还使用
getvariable 方法来检索上下文变量 request.headers.user-agent的值。swagger: '2.0'
info:
x-ibm-name: xslt
title: xslt
version: 1.0.0
schemes:
- https
host: $(catalog.host)
basePath: /xslt
consumes:
- application/json
produces:
- application/json
securityDefinitions:
clientIdHeader:
type: apiKey
in: header
name: X-IBM-Client-Id
security:
- clientIdHeader: []
x-ibm-configuration:
testable: true
enforced: true
cors:
enabled: true
assembly:
execute:
- operation-switch:
title: operation-switch
case:
- operations:
- verb: get
path: /hello
execute:
- xslt:
title: SayHello
input: false
source: |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:element name="APIc">
<xsl:text>Hello World!</xsl:text>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
- operations:
- verb: get
path: /getContextQueryVar
execute:
- xslt:
title: GetContextQueryVar
input: false
source: |
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:apim="http://www.ibm.com/apimanagement">
<xsl:import href="local:/isp/policy/apim.custom.xsl"/>
<xsl:template match="/">
<xsl:call-template name="apim:output">
<xsl:with-param name="mediaType" select="'application/xml'"/>
</xsl:call-template>
<APIC>
<xsl:element name="apim.getVariable">
<xsl:element name="useragent">
<xsl:value-of select="apim:getVariable('request.headers.user-agent')"/>
</xsl:element>
<xsl:element name="query">
<xsl:value-of select="apim:getVariable('request.querystring')"/>
</xsl:element>
</xsl:element>
</APIC>
</xsl:template>
</xsl:stylesheet>
- operations:
- verb: get
path: /getQuery
execute: []
otherwise:
- throw: null
title: handling unknown operation
name: Unsupported
catch:
- errors:
- Unsupported
execute:
- set-variable:
actions:
- set: message.body
value: '<error>Not Supported</error>'
phase: realized
paths:
/hello:
get:
responses:
'200':
description: 200 OK
/getContextQueryVar:
get:
responses:
'200':
description: 200 OK
definitions: {}
tags: []返回与客户端 TLS 配置文件关联的 DataPower 对象名称
以下示例使用该
apim:getTLSProfileObjName 函数返回与指定的 TLS 客户配置文件关联的 DataPower 对象名称。- xslt:
title: xslt
input: false
version: 2.0.0
source: >-
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:apim="http://www.ibm.com/apimanagement"
exclude-result-prefixes="dp apim"
extension-element-prefixes="dp"
version="1.0">
<!-- For apigw ... store:///dp/apim.custom.xsl -->
<!-- For v5/v5c ... local:///isp/policy/apim.custom.xsl -->
<!-- <xsl:import href="local:///isp/policy/apim.custom.xsl"/> -->
<xsl:import href="store:///dp/apim.custom.xsl"/>
<xsl:template match="/">
<xsl:variable name="httpHeaders">
<header name="Accept">application/xml</header>
</xsl:variable>
<!--
For v5, the name returned is client:<orgname>-my-test
For v5c, the name returned is client:<orgname>-my-testV1.0.0
For API Gateway, the name returned is client:<orgname>_<catalogname>_tlsp-my-testV1.0.0
-->
<xsl:variable name="tlsClientProfile" select="apim:getTLSProfileObjName('my-test')" />
<xsl:variable name="targetUrl" select="'https://127.0.0.1:6201/PingRequest'"/>
<dp:url-open target="{$targetUrl}" response="responsecode" timeout="20" http-headers="$httpHeaders" ssl-proxy="{$tlsClientProfile}"/>
</xsl:template>
</xsl:stylesheet>请注意,关于“ v5c ”( v5-compatible )网关的说明不适用于 API Connect Enterprise as a Service。
有关如何使用 XSLT 访问和修改属性及上下文的更多示例,请参阅“实现代码示例”;如果您正在使用 GatewayScript ,请参阅“在 中使用 DataPower® API Gateway上下文变量”以及“在 DataPower 中的 XSLT 策略” API Gateway。
Need more info here on what the API does, from an end-user perspective.