XSLT policy in APigee

Hi All,

I am working on a project that requires to modify some url's in the wsdl.

for ex: <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:abc="http://www.weather.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="WebServiceProvider" targetNamespace="https://xyz.com">

I need below output:

xmlns:abc="http://www.test.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="WebServiceProvider" <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" ">

In short I want to chnage the url for xmlns:abc and targetNameSpace.

Is it possible using XSLT .

Dino-at-Google

Thanks

Sonal

0 8 471
8 REPLIES 8

I don't know, maybe you could paste your code in with thge CODE button.

I can't see the XML formatting.

Thanks Dino-at-Google for your quick response.

This is the response from backend and the url's for parameters 'xmlns:tns' and 'targetNamespace' needs to be changed before sending the response to client.

Input:

<wsdl:definitions

xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

xmlns:tns="http://www.weather.com"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

name="WebServiceProvider"

targetNamespace="www.abc.com">

</wsdl:definitions>

Is it possible to achieve this in apigee via xslt?

Could you please help?

Dino-at-Google

Thanks

Sonal

There are many examples on how to change an attribute value using XSLT. See for instance: https://stackoverflow.com/questions/615875/xslt-how-to-change-an-attribute-value-during-xslcopy

Bear in mind that in your 2nd example you're also changing the namespace alias (from abc to tns). If this is really your intention you may also need to change element namespace prefixes (eg: from <abc:myElement> to <tns:myElement>. In the end an alias it's just an alias, I'd recommend you don't change it.

Maybe this?

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                >


    <xsl:output omit-xml-declaration="yes" indent="yes"/>


    <xsl:param name="newUri" select="'https://newuri.org'"/>
    <xsl:param name="oldUri" select="'http://www.abc.com'"/>


    <xsl:template match="/wsdl:definitions/@targetNamespace">
        <xsl:attribute name="targetNamespace">
            <xsl:value-of select="$newUri"/>
        </xsl:attribute>
    </xsl:template>


    <xsl:template match="/wsdl:definitions">
      <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                        xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                        xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        xmlns:tns="https://newuri.org"
                        name="WebServiceProvider">
        <xsl:apply-templates select="@*|node()"/>
      </wsdl:definitions>
    </xsl:template>


    <xsl:template match="*[namespace-uri()=$oldUri]" priority="1">
    <xsl:element name="tns:{local-name()}" namespace="{$newUri}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>


  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>


input:

<wsdl:definitions
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    name="WebServiceProvider"
    xmlns:tns="http://www.abc.com"
    targetNamespace="http://www.abc.com">
  <tns:foo/>
  <bar xmlns='http://www.abc.com'/>
</wsdl:definitions>


output:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  xmlns:tns="https://newuri.org"
                  name="WebServiceProvider"
                  targetNamespace="https://newuri.org">
  <tns:foo/>
  <tns:bar/>
</wsdl:definitions>


HI Dino,

Thanks for your help and I have one more query on this.

How do I pass these values dynamically?

For example:xmlns:tns="https://newuri.org"

how can I do that in below definition. I have defined a variable 'tnsUrl' but not sure how do I refer it in 'xmlns:tns' in wsdl definition.

Please help Dino-at-Google

Thanks

Sonal

<wsdl:definitionsxmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="{$tnsUrl}"
name="WebServiceProvider">

Hi Sonal

I think you can use the xsl:namespace element in your XSL 2.0 stylesheet to declare a namespace.

hint: https://stackoverflow.com/a/39182125/48082

The Apigee XSL policy supports XSL 1.0 or 2.0.


Combine the xsl:namespace element with an xsl:param, and you can pass context variables into the XSL.

policy defn:

<XSL name='XSL-1'>
  <Source>sourceXmlMessage</Source>
  <OutputVariable>transformedContent</OutputVariable>
  <ResourceURL>xsl://mysheet.xsl</ResourceURL>
  <Parameters ignoreUnresolvedVariables='true'>
    <Parameter name='tnsuri' ref='context_variable_containing_uri'/>

    <!--
      reference this param at the top of the XSL as:
      <xsl:param name="tnsuri" select="''"/>
    -->

  </Parameters>
</XSL>

You should be able to test your XSL outside of Apigee to tweak its behavior to your desires.

Hi Dino-at-Google,

Thankyou so much for your help and you saved alot of time and effort for me 🙂

I could do it using namespace and referring it like mentioned below using xslt 2.0:

<xsl:param name="tnsuri" select="''"/>

<wsdl:definitionsxmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
name="WebServiceProvider">

<xsl:namespace name="tns"
        select="$tnsuri"/>

oh, good! I'm glad you got it sorted. Nice work.