ph_flattenxml PowerHome formula function
Description
This function takes a string in XML format and flattens it to a carriage return linefeed separated list of name/value pairs 
Syntax
ph_flattenxml ( as_xml, as_sep, ai_flags, ai_type )
Argument Description
as_xml String. The XML data to be flattened
as_sep String. The separator character(s) to be used to separate the name/value pairs in the output
ai_flags Integer. Flags controlling how the flattened data is returned. See Usage below
ai_type Integer. The type of flattening to be performed. Valid values are 0 and 1
Return value
String. The flattened XML data is returned as a carriage return linefeed separated list of name/value pairs if successful. If an error occurs, the string "*ERROR*" followed by the error details will be returned. 
Usage
Use this function to "flatten" XML data into name/value pairs to make it easier for other PowerHome functions to extract specific data.  

The ai_type parameter controls the specific method used to flatten the XML data with each the two methods returning potentially different results. Both methods (ai_type = 0 and ai_type = 1) use C# XDocuments to parse the XML.

Both type 0 and type 1 allow different ai_flag parameters to be specified to control the operation. Not all flags are supported in both types. The supported flag values and which types they apply to are listed below. The desired flag values are added together to achieve the final result to be passed in the ai_flags parameter:

  • 1 - Encloses the flattened XML NAME in double quotes. Supported by both types 0 and 1.
  • 2 - Encloses the flattened XML VALUE in double quotes. Supported by both types 0 and 1.
  • 4 - Preceed XML array numbers with a period character instead of an open bracket. Supported only by type 1.
  • 8 - Append XML array numbers with a period character instead of a close bracket. Supported only by type 1.
  • Examples
    The following examples demonstrate typical syntax/usage for this function.
    Assume that the file named c:\powerhome\xmldata.txt contains the following XML data: <XMLROOT><people><name>John</name><age>31</age><city>New York</city><phones>4075551212</phones><phones>3215551212</phones><phones>8885551212</phones></people></XMLROOT>

    • ph_flattenxml(ph_readfile("c:\powerhome\xmldata.txt"),0,0) will return:
    XMLROOT.people.name:John
    XMLROOT.people.age:31
    XMLROOT.people.city:New York
    XMLROOT.people.phones:4075551212
    XMLROOT.people.phones:3215551212
    XMLROOT.people.phones:8885551212

    • ph_flattenxml(ph_readfile("c:\powerhome\xmldata.txt"),0,1) will return:
    XMLROOT.people.name:John
    XMLROOT.people.age:31
    XMLROOT.people.city:New York
    XMLROOT.people.phones[0]:4075551212
    XMLROOT.people.phones[1]:3215551212
    XMLROOT.people.phones[2]:8885551212