Merging data

It is sometimes necessary to merge two data sources. For this purpose #current-input, which refers to a single source, is not sufficient. You can process multiple active sources at a time by passing multiple string source arguments to a function.

The following function, for example, merges a list of employee names with their employee numbers.

  define string source function 
     join-employee-data value string source employee-names
                    and value string source employee-numbers
  as
     output "<employees>"
     repeat scan employee-names
     match any ++ => employee-name
              ("%n" | value-end)
        local string employee-number
        set employee-number
        to (employee-numbers take any ++ ("%n" | value-end)) take [\"%n"]*
        output "<employee>"
               || "<name>" 
               || employee-name
               || "</name>"
               || "<number>" 
               || employee-number 
               || "</number>"
               || "</employee>"
     again
     output "</employees>%n"

This string source function join-employee-data combines the two string sources employee-names and employee-numbers and produces a single string of XML data. The join-employee-data function can then be used as a source for an XML parse:

  process
     local string employee-names   initial { "Tom%nDick%nHarry" }
     local string employee-numbers initial { "0001%n0002%n0003%n" }
     do xml-parse scan join-employee-data employee-names and employee-numbers
        output "%c"
     done
  
  element "employees"
     output "EMPLOYEES%n"
         || "=========%n"
         || "%n"
         || "%c"
  
  element "employee"
     output "%c%n"
  
  element "name"
     output "   NAME: %c%n"
  
  element "number"
     output " NUMBER: %c%n"