Hello,
Is there a way to pass a XSLT resource as a parameter on XSLT Policy Transformation?
My need is to apply a dynamic template to remove some undesired elements from an response.
Public Webservice got as an example: http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl
Follow my assets for someone that may want to help me, the goal is to take control of which element is an array of elements.
By default applying only a transformation XML → JSON policy i got:
"GetWeatherInformationResponse": {
"GetWeatherInformationResult": {
"WeatherDescription": [
{
"WeatherID": 1,
"Description": "Thunder Storms",
"PictureURL": "http://wsf.cdyne.com/WeatherWS/Images/thunderstorms.gif"
}, ...
But i expected this:
"WeatherDescription": [
{
"WeatherID": 1,
"Description": "Thunder Storms",
"PictureURL": "http://wsf.cdyne.com/WeatherWS/Images/thunderstorms.gif"
}, ...
This is a simple scenario, but i have other more complex scenarios where we can have 3 or more lists of elements.
PS: We already made this with an API Gateway that makes use of Saxon XSLT processor, so i know that this is possible, but i’m in doubt on how to pass a reference to an XSLT Resource as a parameter.
XSLT policy:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XSL async="false" continueOnError="false" enabled="true" name="SoapResponseRewrite">
<DisplayName>SoapResponseRewrite</DisplayName>
<Properties/>
<Source>response</Source>
<ResourceURL>xsl://SoapResponseRewrite.xsl</ResourceURL>
<Parameters ignoreUnresolvedVariables="false">
<Parameter name="soap.response.template" value="apiproxy/resources/xsl/ListWeathersResponse.xsl"/>
</Parameters>
</XSL
Main XSLT SoapResponseRewrite.xsl (Rewrite the entire SOAP Response according to an template passed as parameter)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:param name="soap.response.template"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:param name="source"/>
<xsl:variable name="current-lookup-elem" select="current()"/>
<xsl:for-each select="$source/*[local-name() = local-name($current-lookup-elem)]">
<xsl:choose>
<xsl:when test="$current-lookup-elem[processing-instruction('xml-multiple')]">
<xsl:if test="./*">
<xsl:processing-instruction name="xml-multiple">
<xsl:value-of select="local-name(./*)"/>
</xsl:processing-instruction>
</xsl:if>
<xsl:apply-templates select="$current-lookup-elem/*">
<xsl:with-param name="source" select="current()"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{local-name($current-lookup-elem)}">
<xsl:apply-templates select="$current-lookup-elem/*">
<xsl:with-param name="source" select="current()"/>
</xsl:apply-templates>
<xsl:copy-of select="text()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="/*">
<xsl:apply-templates select="document($soap.response.template)/*/soap:Envelope/soap:Body/*/node()">
<xsl:with-param name="source" select="/*/soap:Body/*"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="/">
<xsl:element name="response">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
XSLT Template ListWeathersResponse.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetWeatherInformationResponse xmlns="http://ws.cdyne.com/WeatherWS/">
<GetWeatherInformationResult>
<?xml-multiple?>
<WeatherDescription>
<WeatherID/>
<Description/>
<PictureURL/>
</WeatherDescription>
</GetWeatherInformationResult>
</GetWeatherInformationResponse>
</soap:Body>
</soap:Envelope>
</xsl:stylesheet>