Oracle SOA Suite Online Training

Interested in learning Oracle SOA Suite 12c?
Learn from the author of this blog!
A complete and comprehensive course on the #1 platform on SOA - Oracle SOA Suite

Click here to find the complete course details
Click here to check the first session on Oracle SOA Suite 12c

================================================================================================

Replace NameSpaces in XSLT

You may come across a situation where you want to replace one set of namespaces to another set. A typical example is that you have a pass-through OSB Service whose proxy is based on your Project's namespaces and the underlying service is based on the out-of-the-box service namespaces.

In such cases, the following XSLT will search-replace the namespaces

For example, if your input xml looks like

<ns1:Response>
   <ns2:task>..</ns2:task>
   <ns2:address>..</ns2:address>
   <ns2:pin>..</ns2:pin>
   <ns3:address>
      <ns4:add1>..</ns4:add1>
      <ns4:add2>..</ns4:add2>
      <ns4:add3>
        <ns5:asdf>..</ns5:asdf>
        <ns5:qwe>..</ns5:qwe>
      </ns4:add3>
      <ns4:add4>..</ns4:add4>
    </ns3:address>
    <ns2:query>..</ns2:query>
</ns1:Response>

and the response has to have a different set of namespaces,

<myns1:Response>
   <myns2:task>..</myns2:task>
   <myns2:address>..</myns2:address>
   <myns2:pin>..</myns2:pin>
   <myns3:address>
      <myns4:add1>..</myns4:add1>
      <myns4:add2>..</myns4:add2>
      <myns4:add3>
        <myns5:asdf>..</myns5:asdf>
        <myns5:qwe>..</myns5:qwe>
      </myns4:add3>
      <myns4:add4>..</myns4:add4>
    </myns3:address>
    <myns2:query>..</myns2:query>
</myns1:Response>

The following XSLT works

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns1="http://www.w3.org/TR/html4/1"
xmlns:myns2="http://www.w3.org/TR/html4/2"
xmlns:myns3="http://www.w3.org/TR/html4/3"
xmlns:myns4="http://www.w3.org/TR/html4/4"
xmlns:myns5="http://www.w3schools.com/furniture"
xmlns:ns1="http://www.w3.org/TR/html4/1"
xmlns:ns2="http://www.w3.org/TR/html4/2"
xmlns:ns3="http://www.w3.org/TR/html4/3"
xmlns:ns4="http://www.w3.org/TR/html4/4"
xmlns:ns5="http://www.w3schools.com/furniture">

<xsl:template match="*">
  <myns1:taskListResponse>
    <xsl:apply-templates/>
  </myns1:taskListResponse>
</xsl:template>

<xsl:template match="ns2:*">
  <xsl:element name="myns2:{local-name()}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="ns3:*">
  <xsl:element name="myns3:{local-name()}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="ns4:*">
  <xsl:element name="myns4:{local-name()}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>
<xsl:template match="ns5:*">
  <xsl:element name="myns5:{local-name()}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:copy/> </xsl:template>
</xsl:stylesheet>

No comments:

Post a comment