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

问题描述 投票:0回答:1

我希望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>[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>

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

<?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: "[email protected]",   
  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. 我需要将第二个名称空间放入同一个标签中。

我怎么做?

非常感谢你提前。

xml namespaces dataweave mulesoft
1个回答
0
投票

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

例:

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

我认为你将很难使用命名空间,但是你可以尝试这个hack并使用属性代替你想要完成的任务。我只是建议使用大量的评论,因为你所做的是非标准的行为:

%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属性的实际示例。

© www.soinside.com 2019 - 2024. All rights reserved.