Vish
Vish

Reputation: 384

Is there any way to centralize the custom XML namespaces in Mule 4 application?

Application and runtime version: Mule 4 application, with XML Module.

I'm trying to create mule application with many custom xml namespaces. It seems that prefixes are expected to be declared locally, like this,

%dw 2.0
output application/xml
ns orders http://www.acme.com/shemas/Orders
ns stores http://www.acme.com/shemas/Stores
---
root:
    orders#orders: {
        stores#shipNodeId: "SF01",
        stores#shipNodeId @(shipsVia:"LA01"): "NY03"
    }

Is there any way to centralize the custom XML namespaces?


1. Tried something in this : Import xml namespace from custom Dataweave module

I tried creating a custom module and declared namespaces and imported it into my dataweave, but it seems that prefixes are expected to be declared locally

Namespaces.dwl

ns myNs1 http://namespaces/my1
ns myNs2 http://namespaces/my2

Dataweave :

%dw 2.0
import * from modules::Namespaces
output application/java
---
{
    body: {
        myNs1#Response: {
            outcome: 'ACCEPTED'
        }
    } write "application/xml"
}

But this doesn't work, again this requires declaring prefixes locally, something like this. After declaring prefixes locally this works.

%dw 2.0
import * from modules::Namespaces
output application/java

var myNs1Local = myNs1 as Namespace

---
{

    body: {
        myNs1Local#Response: {
            outcome: 'ACCEPTED'
        }
    } write "application/xml"
}

After declaring prefixes locally this works.


2. I tried to understand namespace-directory documented here: https://docs.mulesoft.com/xml-module/latest/xml-xpath

The documentation explains how to use this with Xpaths, but Not sure how to use this while creating XML, couldn't find any documentation.

<xml-module:namespace-directory name="fullNs"> 
    <xml-module:namespaces>
        <xml-module:namespace prefix="soap" uri="http://schemas.xmlsoap.org/soap/envelope/"/>
        <xml-module:namespace prefix="mule" uri="http://simple.component.mule.org/"/>
    </xml-module:namespaces>
</xml-module:namespace-directory>

<flow name="xpathWithFullNs">
    <xml-module:xpath-extract
      xpath="/soap:Envelope/soap:Body/mule:echo/mule:echo"
      namespaceDirectory="fullNs"/> 
</flow>

Upvotes: 1

Views: 125

Answers (1)

Vish
Vish

Reputation: 384

After fixing minor errors, the first approach worked.

Created a custom module and declared namespaces and imported it into my dataweave worked without declaring prefixes again in the local dataweave.

Created below module under the folder src/main/resources/modules

Namespaces.dwl

ns myNs1 http://namespaces/my1
ns myNs2 http://namespaces/my2

Dataweave :

%dw 2.0
import * from modules::Namespaces
output application/java
---
{
    body: {
        myNs1#Response: {
            outcome: 'ACCEPTED'
        }
    } write "application/xml"
}

Upvotes: 2

Related Questions