6.25. xml-generator

This is a quick and dirty tag that generates XML tags based upon one of two types of data (delimited and session).

6.25.1. Summary

    [xml-generator type* other_named_attributes][/xml-generator]
    [xml-generator type=value* other_named_attributes][/xml-generator]
    [xml-generator type=value* other_named_attributes][][/xml-generator]


Positional parameters: The first line shows the usage with positional parameters (given in order). The second line shows usage with named parameters.

Parameters Description Default
type Data type. Delimited or session delimited
Attributes Default
attributes none
dbdump none
delimiter \t
separator \n
toplevel_tag 'table' for delimited type and 'session' for other type
record_tag record
field_tag field
key_name none
spacer [\s,]+
no_second none
skip_empty none
Invalidates cache No
Macro No
Has end tag [/xml-generator]

Tag expansion example:

    [xml-generator type=delimited
                   date="[tag time]%d-%b-%Y[/tag]"
    ]code       description     price
        [query list=1
                        sql="select sku, description, price from products"
                        prefix=xml][xml-code]  [xml-param description] [xml-param price]
    <products date="18-May-2001">
            <record key="os28113">
                    <description>The Claw Hand Rake</description>
            <record key="os28006">
                    <description>Painters Brush Set</description>

ASP-like Perl call:

    $Tag->xml_generator( {type => delimited,
                      toplevel_tag => apex,  }, $BODY );

or similarly with positional parameters,

    $Tag->xml_generator( $type, $attribute_hash_reference, $BODY );

6.25.2. Description type


Accepts a delimited and separated (default is TAB delimiter and newline separator) list of records such as that generated by an '[item-list]', '[sql]', or '[loop search=""]' ITL tag.


When the type is not delimited, it can contain any hash reference into the Interchange session. Examples are:

        values       The form values
        scratch      Scratch values
        errors       Error values
        other        Any other Session key, for example "source" for
                     [data session source]

If the value is a hash, then it will be sent as an XML record with the top level equal to "session", and a second_level tag equal to the hash name, and keys as separate XML container tags. If the parameter "that is equal to the type" is given, only those fields will be shown. Otherwise the entire hash will be shown. For example, this tag:

        [xml-generator type="values" values="fname lname"][/xml-generator]

will generate:


if it is a scalar, then only the second level will be done:

        [xml-generator type="cybercash_id"][/xml-generator]

will do the equivalent of:

                <cybercash_id>[data session cybercash_id]</cybercash_id>

So bringing it all together, the following:

        [xml-generator type="values scratch source"
                       values="fname lname"
will generate:

        </session> no_second

Prevents the second-level tags from being generated. Extending the last example in the "session" type above, this

        [xml-generator  type="values scratch source"
                        values="fname lname"
will generate:

        </session> attributes

The attributes (if any) to pass on to the top level tag. For instance,

                date="[tag time]%d-%b-%Y[/tag]"

will generate a toplevel tag pair of:

        <order date="18-Mar-2001">
        </order> dbdump

Will dump all tables in the catalog when this attribute is set true. Used attributes are "toplevel_tag", "record_tag", "field_tag", and "skip_empty" or default values ('table', 'record', 'field' respectively).

Output format:
    <database name="catalogname">
        <toplevel_tag name="tablename1">
            <record_tag key="value of first field-1">
                <field_tag name="fieldname1">fieldvalue1</field_tag>
                <field_tag name="fieldname2">fieldvalue2</field_tag>
            <record_tag key="value of first field-2">
                <field_tag name="fieldname1">fieldvalue1</field_tag>
                <field_tag name="fieldname2">fieldvalue2</field_tag>
        <toplevel_tag name="tablename2">
            <record_tag key="value of first field-1">
                <field_tag name="fieldname1">fieldvalue1</field_tag>
                <field_tag name="fieldname2">fieldvalue2</field_tag>

Important note: All tables are read into memory. So be warned, this could be a real memory hog.

Ton Verhagen's proposal:

  1. Add option to select tables. E.g. dump_tables="products cat area" and/or
  2. Add option to select an output file. E.g. dump_file="tabledump.XML". Send output to file line by line. delimiter

Character used as delimiter of fields in delimited data type. Defaults to a tab character. field_names

Space or comma-delimited list of field names to be used for delimited data type. Should be in the same order as in the data list provided (between the tags).

Another way of providing the field names would be:

    [xml-generator .....]fieldname-1    fieldname-2     fieldname-3
        [field value list
         delimited by option delimiter and
         separated by option separator][/xml-generator]

Note: Field name list must be tab delimited.

Ton Verhagen's humble opinion: This should change in future versions! Use option delimiter instead. separator

Character used as line separator in list between [xml-separator][xml-separator] tags and in output 'session' data type. Defaults to a newline, "\n". toplevel_tag

The toplevel tag name to use. Defaults to "table" for the 'dbdump mode' and delimited type, and "session" for the other. record_tag

Defines the tag name for the record tag. Defaults to 'record'. Used for 'dbdump mode' and delimited type. field_tag

Defines the tag name for the field tag. Defaults to 'field'. Only used in 'dbdump mode'. key_name

Only used in delimited data type. Defines fieldname to determine key value in "record_tag".

    <record_tag key="value of field with name defined by key_name"> .... spacer

Character used as delimiter in type parameter definition and corresponding attributes. Defaults to '[\s,]+' (one or more whitespace or comma).

    [xml-generator type="values|scratch"
    ][/xml-generator] skip_empty

Only used in dbdump mode (dbdump=1). Will skip empty fields if this attribute is set true.