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]

*Optional


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
field_names  
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
Other_Characteristics  
Invalidates cache No
Macro No
Has end tag [/xml-generator]

Tag expansion example:

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

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

6.25.2.1. type

delimited

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.

session

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:

        <session>
                <values>
                        <fname>First</fname>
                        <lname>Last</lname>
                </values>
        </session>

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:

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

So bringing it all together, the following:

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

        <session>
                <values>
                        <fname>First</fname>
                        <lname>Last</lname>
                </values>
                <scratch>
                        <downloads>0</downloads>
                </scratch>
                <source>Partner1</source>
        </session>

6.25.2.2. 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"
                        no_second=1
                        values="fname lname"
                        scratch="downloads"][/xml-generator]
will generate:

        <session>
                <fname>First</fname>
                <lname>Last</lname>
                <downloads>0</downloads>
                <source>Partner1</source>
        </session>

6.25.2.3. attributes

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

    [xml-generator
          attributes="date"
                date="[tag time]%d-%b-%Y[/tag]"
        toplevel_tag=order
    ][/xml-generator]

will generate a toplevel tag pair of:

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

6.25.2.4. 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>
            <record_tag key="value of first field-2">
                <field_tag name="fieldname1">fieldvalue1</field_tag>
                <field_tag name="fieldname2">fieldvalue2</field_tag>
                        </record_tag>
                </toplevel_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>
                        </record_tag>
                </toplevel_tag>
        </database>

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.

6.25.2.5. delimiter

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

6.25.2.6. 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.

6.25.2.7. 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".

6.25.2.8. toplevel_tag

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

6.25.2.9. record_tag

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

6.25.2.10. field_tag

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

6.25.2.11. 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"> ....

6.25.2.12. 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"
                 values="value1|value2"
                scratch="scratch1|scratch2"
                 spacer="|"
    ][/xml-generator]

6.25.2.13. skip_empty

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