'Progress OpenEdge XML optional child node

I need a suggestion on how to write the following XML using write-xml? is this possible? thank you

Notice the last material node has a different elements. How can I made elements optional and "show/notShow" when writing the output?

def temp-table ttMaterials no-undo       serialize-name "Materials" 
  field MatsID                  as int   serialize-hidden.

def temp-table ttMaterial  no-undo       serialize-name "Material" 
  field MatsID                  as int   serialize-hidden
  field MaterialNumber          as char
  field PalletNumber            as char
  field Quantity                as char
  field UnitOfMeasure           as char
  field StorageLocation         as char
  field OperationCode           as char
  field ReasonCode              as char
  field CancellationFlag        as char
  field ItemNo                  as char.
  
def dataset dsMaterial xml-node-name "ns1:Message" for 
  ttMaterials, ttMaterial.
  data-relation Rel1 for ttMaterials, ttMaterial nested relation-fields(MatsID, MatsID).


<?xml version="1.0"?>
<ns1:Message xmlns:ns1="http://google.envelope">
  <Materials>
    <Material>
      <MaterialNumber>00111</MaterialNumber>
      <PalletNumber>1888845001</PalletNumber>
      <Quantity>91.0000000000</Quantity>
      <UnitOfMeasure>PAC</UnitOfMeasure>
      <StorageLocation>700</StorageLocation>
      <OperationCode>101</OperationCode>
      <ReasonCode/>
      <CancellationFlag>false</CancellationFlag>
      <ItemNo>10</ItemNo>
    </Material>
    <Material>
      <MaterialNumber>00112</MaterialNumber>
      <PalletNumber>45828760</PalletNumber>
      <Quantity>58.3100000000</Quantity>
      <UnitOfMeasure>PAC</UnitOfMeasure>
      <StorageLocation>700</StorageLocation>
      <OperationCode>543</OperationCode>
      <ReasonCode/>
      <CancellationFlag>false</CancellationFlag>
      <ItemNo>10</ItemNo>
    </Material>
    <Material>
       <FromMaterialNumber>000000000000063299</FromMaterialNumber>
       <ToMaterialNumber>000000000000063299</ToMaterialNumber>
       <FromPalletNumber>81592826</FromPalletNumber>
       <ToPalletNumber>81592826</ToPalletNumber>
       <Quantity>8</Quantity>
       <UnitOfMeasure>PAC</UnitOfMeasure>
       <FromStorageLocation>300</FromStorageLocation>
       <ToStorageLocation>300</ToStorageLocation>
       <OperationCode>551</OperationCode>
       <ReasonCode>12</ReasonCode>
       <CancellationFlag>false</CancellationFlag>
    </Material>
  </Materials>
</ns1:Message>


Solution 1:[1]

You can make use of parameter 9 (omit-initial-values) of the write-xml method and ensure that this is true for the data you need to hide.

dataset dsMaterial:write-xml(
  'longchar',
  lcxml,
  true, // formatted
  ?,    // encoding
  ?,    // schema-location
  ?,    // write-xml-schema
  ?,    // min-xml-schema
  ?,    // write-before-image
  true  // omit-initial-values
).
message string( lcxml ).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefan Drissen