删除XML标头声明,并在DataWeave 2.0中提供不带前缀的XML名称空间

时间:2019-03-15 14:43:18

标签: xml namespaces dataweave mulesoft

我希望XML如下所示,没有标题:

<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>someone@exampe.com</StudentEmail>
      </Student>
      <Student>
         <StudentID>2</StudentID>
         <StudentFirstName>Another</StudentFirstName>
         <StudentLastName>One</StudentLastName>
         <StudentEmail>anotherone@exampe.com</StudentEmail>
      </Student>    
   </Students>    
   <SendStudentEmail>false</SendStudentEmail>    
   <ContactFirstName>me</ContactFirstName>    
   <ContactLastName>myself</ContactLastName>    
   <ContactEmail>me.myself@example.com</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>

但是,当我预览转换消息的输出时,看到以下内容:

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

,这将导致提交到供应商的API失败。

问题1:如何摆脱此标头?

问题2:如何将两个名称空间添加到XML行。我的数据编织中有以下内容:

%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: "me.myself@example.com",   
  SendContactEmai: true,   
  ShipAddress1: 123,   
  ShipAddress2: "",   
  ShipCity: "someplace",   
  ShipState: "NM",   
  ShipCountry: "US",   
  ShipZipcode: 54481,   
  Language: "en",   
  AssignmentApplication: false 
}

如果我在gilmore#前面添加LicenseCodeRequest:,则会将输出更改为:

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

但是:

  1. 我不希望它是gilmore:LicenseCodeRequest...,我只希望它是LicenseCodeRequest...,并且
  2. 我需要将第二个名称空间放入同一标签。

我该怎么做?

非常感谢您。

1 个答案:

答案 0 :(得分:0)

您可以在输出标题(文档here)中使用writeDeclaration=false

示例:

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

我认为您将很难使用名称空间,但是您可以尝试这种方法,并使用属性代替您要完成的工作。我只建议使用大量注释,因为您正在执行的是非标准行为:

%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: ...
    ...
  }
}

您可以在DataWeave 2.0 here中找到使用XML属性的更多实际示例。