Matthew
Matthew

Reputation: 23

Remove XML header declaration and provide XML namespaces without prefix in DataWeave 2.0

I want the XML to come out looking just like the following, no headers:

<LicenseCodeRequest xmlns="http://www.gilmore.ca/services/eVantageBookLicense" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">    
    <ClientID>XXX</ClientID>    
    <PartNumber>1000-VS1</PartNumber>    
    <Qty>2</Qty>    
    <CustomerOrderNumber>56789</CustomerOrderNumber>    
    <BillingReferenceID>1234</BillingReferenceID>    
    <SoldToCode>XX-XXX</SoldToCode>    
    <Students>
      <Student>
         <StudentID>1</StudentID>
         <StudentFirstName>Some</StudentFirstName>
         <StudentLastName>One</StudentLastName>
         <StudentEmail>[email protected]</StudentEmail>
      </Student>
      <Student>
         <StudentID>2</StudentID>
         <StudentFirstName>Another</StudentFirstName>
         <StudentLastName>One</StudentLastName>
         <StudentEmail>[email protected]</StudentEmail>
      </Student>    
   </Students>    
   <SendStudentEmail>false</SendStudentEmail>    
   <ContactFirstName>me</ContactFirstName>    
   <ContactLastName>myself</ContactLastName>    
   <ContactEmail>[email protected]</ContactEmail>    
   <SendContactEmail>true</SendContactEmail>    
   <ShipAddress1>123</ShipAddress1>    
   <ShipAddress2></ShipAddress2>    
   <ShipCity>someplace</ShipCity>    
   <ShipState>NM</ShipState>    
   <ShipCountry>US</ShipCountry>    
   <ShipZipcode>54481</ShipZipcode>    
   <Language>en</Language>    
   <AssignmentApplication>false</AssignmentApplication> 
</LicenseCodeRequest>

Yet, when I preview the output of my Transform message, I see the following:

<?xml version='1.0' encoding='UTF-8'?>

which will cause the submission to the vendor's API to fail.

Question 1: How do I get rid of this header?

Question 2: How do I get the TWO namespaces added to the XML line. I have the following in my dataweave:

%dw 2.0
output application/xml  
ns gilmore http://www.gilmore.ca/services/eVantageBookLicense 
ns w3i http://www.w3.org/2001/XMLSchema-instance
---
LicenseCodeRequest: {   
  ClientID: XXX,   
  PartNumber: "1000-VS1",   
  Qty: 1,   
  CustomerOrderNumber: 56789,   
  BillingReferenceID: 1234,   
  SoldToCode: "XX-XXXX",   
  Students: {       
    Student: {
      StudentID: 1,
      StudentFirstName: "Some",
      StudentLastName: "One",
      StudentEmail:payload.email,       
    }   
  },   
  SendStudentEmail: false,   
  ContactFirstName: "me",   
  ContactLastName: "myself",   
  ContactEmail: "[email protected]",   
  SendContactEmai: true,   
  ShipAddress1: 123,   
  ShipAddress2: "",   
  ShipCity: "someplace",   
  ShipState: "NM",   
  ShipCountry: "US",   
  ShipZipcode: 54481,   
  Language: "en",   
  AssignmentApplication: false 
}

If I add gilmore# in front of the LicenseCodeRequest: it changes the output to:

gilmore:LicenseCodeRequest xmlns="http://www.gilmore.ca/services/eVantageBookLicense"

BUT:

  1. I don't want it to be gilmore:LicenseCodeRequest... I just need it to be LicenseCodeRequest... and
  2. I need to get the 2nd namespace into the same tag.

How do I do that?

Thank you very much in advance.

Upvotes: 0

Views: 2261

Answers (1)

jerney
jerney

Reputation: 2233

You can use writeDeclaration=false in the output header (docs here)

Example:

%dw 2.0
output application/xml writeDeclaration=false
---
[insert transformation code here]

I think you're going to have a hard time w/ namespaces, but you can try this hack and use attributes instead for what you're trying to accomplish. I'd just recommend using plenty of comments because what you're doing is non-standard behavior:

%dw 2.0
output application/xml writeDeclaration=false

var xmlns  = "http://www.gilmore.ca/services/eVantageBookLicense"
var xmlnsi = "http://www.w3.org/2001/XMLSchema-instance"
---
{
  LicenseCodeRequest @("xmlns": xmlns, "xmlns:i": xmlnsi): {
    ClientId: ...
    ...
  }
}

You can find more practical examples of using XML attributes in DataWeave 2.0 here.

Upvotes: 1

Related Questions