Generate a TransformationScheme from VTL Script

In this tutorial, we will generate pysdmx VTL objects from a VTL script using the vtlengine library.

Important

For this tutorial, you also need to install the pysdmx[vtl] extra.

Check the installation guide for more information.

Firstly, we generate a VTL Script as a String, or read it from a file.

from pathlib import Path

# Path to the VTL script file in the same directory as this script
vtl_script = Path(__file__).parent / "vtl_script.vtl"

vtl_script = """
define datapoint ruleset signValidation
        (variable ACCOUNTING_ENTRY as AE, INT_ACC_ITEM as IAI,
            FUNCTIONAL_CAT as FC, INSTR_ASSET as IA, OBS_VALUE as O)
        is
            sign1c: when AE = "C" and IAI = "G" then O > 0 errorcode
            "sign1c" errorlevel 1
        end datapoint ruleset;
define operator filter_ds
        (ds1 dataset, great_cons string default "1",
         less_cons number default 4.0)
        returns dataset
        is ds1[filter Me_1 > great_cons and Me_2 < less_cons]
        end operator;
DS_r <- DS_1 + 1;
"""

Secondly, to generate a Transformation Scheme object, we use the generate_sdmx function from the vtlengine library. We will need to pass the script, agency_id, id and version

from vtlengine import generate_sdmx

# Generate a Transformation Scheme from the VTL script
ts_scheme = generate_sdmx(script=vtl_script, agency_id="MD", id="TS1", version="1.0")

print(repr(ts_scheme))

Finally, the generated TransformationScheme object can now be serialized in a SDMX message or may be used to perform further operations, like running the TransformationScheme.

Check the VTL Toolkit for more information on how to use the generated TransformationScheme.