Skip to main content

SSB Altinn Python

PyPI Status Python Version License

Documentation Tests Coverage Quality Gate Status

pre-commit Black Ruff Poetry

Features

This is work-in-progress Python-package for dealing with xml-data from Altinn3. Here are some examples of how it can be used:

Transform to ISEE-Dynarev format

If you want to transform an Altinn3 xml-file to a Pandas Dataframe, in the same form as the ISEE Dynarev database in our on-prem environment, you can use the isee_transform-function.

from altinn import isee_transform

file = "gs://ssb-prod-dapla-felles-data-delt/altinn3/RA-0595/2023/2/6/810409282_460784f978a2_ebc7af7e-4ebe-4883-b844-66ee6292a93a/form_460784f978a2.xml"

isee_transform(file)

Mapping the FELTNAVN-column

If you want to recode/map names in the FELTNAVN-column, you can use a dictionary with the original names from the xml as keys, and the new names as values. And then pass the dictionary as an argument when running the function isee_transform(file, mapping).

from altinn import isee_transform

file = "gs://ssb-prod-dapla-felles-data-delt/altinn3/RA-0595/2023/2/6/810409282_460784f978a2_ebc7af7e-4ebe-4883-b844-66ee6292a93a/form_460784f978a2.xml"

mapping = {'kontAmbulForeDispJaNei':'ISEE_VAR1',
           'kontAmbulForeDispAnt':'ISEE_VAR2',
           'kontAmbulForeDriftAnt':'ISEE_VAR3',}

isee_transform(file, mapping)

Flatten more tags than SkjemaData

If you need to flatten more than 'SkjemaData' from the XML, you can make a list of the tags in the XML you need to flatten, and add this list as an argument (tag_list) when running the function isee_transform(file, taglist=taglist). The default value is 'SkjemaData', so if you only need to flatten this, its not needed to pass the argument tag_list.

from altinn import isee_transform

file = "gs://ssb-prod-dapla-felles-data-delt/altinn3/RA-0595/2023/2/6/810409282_460784f978a2_ebc7af7e-4ebe-4883-b844-66ee6292a93a/form_460784f978a2.xml"

mapping = {'kontAmbulForeDispJaNei':'ISEE_VAR1',
           'kontAmbulForeDispAnt':'ISEE_VAR2',
           'kontAmbulForeDriftAnt':'ISEE_VAR3',
           'kontaktPersonNavn'; 'ISEE_KONTAKTPERSON',}

tags = ['SkjemaData', 'Kontakt']

isee_transform(file, mapping, tag_list=tags)

Flatten checkboxes

Altinn 3 checkboxes comes as a string value seperated by ",". To get this into oracle, the value must be extracted and pivoted to unique rows. Listing variables from altinn 3 xml file that are checkboxes in checkbox_vars will make each value to a unique row. Checkboxes can use unique codes from KLASS or be 1:N. If your checkbox codes are unique, you should also set unique_code to True. When we then transform the xml file to isee format, we will end up with more variables than we begun with. This extra potential variables must then be included in the mapping so they have the right name when loaded into oracle.

from altinn import isee_transform

file = 'gs://ra0187-01-altinn-data-staging-c629-ssb-altinn/2024/4/11/7d5b52259b89_de4a24aa-4948-48d8-b2e4-a0f2160a0bd0/form_7d5b52259b89.xml'

mapping = {
    "storOkningOmsAarsak31":"ISEE_storOkningOmsAarsak31",
    "storOkningOmsAarsak33":"ISEE_storOkningOmsAarsak33",
    "omsForrigePerPrefill":"ISEE_omsForrigePerPrefill",
}

checkboxList = ["storOkningOmsAarsak"]

isee_transform(file=file,mapping=mapping,checkbox_vars=checkboxList,unique_code=False)

Include LEVEL-column

Some alinn3 forms contain complex structures, known as table-in-table, or table-in-table-in-table, etc. In these cases, further adaptation of the result from 'isee_transform()'' is necessary in order to prepare data for loading into ISEE. To facilitate this adaptation, it is possible to extract an additional column 'LEVEL' by using 'isee_transform()''. The 'LEVEL' column displays the different levels in the structure of the relevant RA form as a list, and can be used to customize sequence numbers as needed. 'include_level' is set to False by default, so those who are already using isee_transform() in Kildomaten or other solutions do not need to take any action.

Note! — The 'LEVEL' column must be removed before the load file for ISEE is created.

from altinn import isee_transform

file = 'gs://ssb-suv-altinn-ra0485-01-test/2025/11/11/ad7dc245dd22_41d54855-0ae9-48c5-abd2-c62a5ab071b6/form_ad7dc245dd22.xml'

tags = ['SkjemaData', 'Kontakt']

df = isee_transform(file, tag_list = tags, include_level=True)

The function handles flat structures and 'tables' in the XML. If the XML contains repeating values, it puts a suffix containig a number at the end of the FELTNAVN-column. If the XML-contains more complex structures as 'table in table' if will give a warning with a list of which values in FELTNAVN that needs to be further processed before it can be used in ISEE. These warnings will not be visible in 'Kildomaten' unless you check the logs, so it's recommended to test outside of 'Kildomaten' before ypu put it in production.

The XML needs to contain certain fields in the 'InternInfo'-block, The required fields for RA-forms are:

  • 'enhetsIdent'
  • 'enhetsType'
  • 'delregNr'

The required fields for RS-forms are:

  • 'enhetsOrgNr'
  • 'enhetsType'
  • 'delregNr'

The field 'raNummer' under 'InternInfo' in the XML determines whether a form is flattened as RA or RS. If one or more of these fields are missing in the XML, the processing will stop, giving a message with witch fields that are missing.

The resulting object is a Pandas Dataframe with the following columns:

  • SKJEMA_ID
  • DELREG_NR
  • IDENT_NR
  • ENHETS_TYPE
  • FELTNAVN
  • FELTVERDI
  • VERSION_NR

This dataframe can be written to csv and uploaded to the ISEE Dynarev database.

Transform all XML-data to a pd.DataFrame

If you want to transform an Altinn3 xml-file to a Pandas Dataframe, without the extra ISEE-information, and keep all information (not just ‘SkjemaData), you can use the xml_transform-function.

from altinn import xml_transform

file = "gs://ssb-prod-dapla-felles-data-delt/altinn3/RA-0595/2023/2/6/810409282_460784f978a2_ebc7af7e-4ebe-4883-b844-66ee6292a93a/form_460784f978a2.xml"

xml_transform(file)

The resulting object is a Pandas Dataframe with the following columns:

  • FELTNAVN
  • FELTVERDI
  • LEVEL

FELTNAVN: the name of the xml-tags concatenated together for each level in the XML. FELTVERDI: the value of the xml-tag. LEVEL: A list with information about the concatenation level. If one or more of the values is greater than 1, it means there are repeating values in the tag.

Create filename for use in ISEE

If you need to transfer ISEE-data to the On-Prem-platform, the .csv-filename need a spesific format. The function create_isee_filename can create this filename from the filepath and contents of a XML-file.

from altinn import create_isee_filename

file = "gs://ssb-prod-dapla-felles-data-delt/altinn3/RA-0595/2023/2/6/810409282_460784f978a2_ebc7af7e-4ebe-4883-b844-66ee6292a93a/form_460784f978a2.xml"

create_isee_filename(file)

In the example above the output will be RA-0595A3_460784f978a2.csv. This can be used to build a new filepath to where you need to store the result after the XML is transformed to ISEE-format.

Get information about a file

from altinn import FileInfo

file = "gs://ssb-prod-dapla-felles-data-delt/altinn3/RA-0595/2023/2/6/810409282_460784f978a2_ebc7af7e-4ebe-4883-b844-66ee6292a93a/form_460784f978a2.xml"

# Create an instance of FileInfo
form = FileInfo(file)

# Get file filename without '.xml'-postfix
form.filename()
# Returns: 'form_dc551844cd74'

# Print an unformatted version of the file. Does not require the file to be parseable by an xml-library. Useful for inspecting unvalid xml-files.
form.print()

# Print a nicely formatted version of the file
form.pretty_print()

# Check if xml-file is valid. Useful to inspect xml-files with formal errors in the xml-schema.
form.validate()
# Returns True og False

Parse xml-file

If you want to transform an Altinn3 xml-file to a Pandas Dataframe, you can use the ParseSingleXml-class. This will not handle complex structures of the XML.

from altinn import ParseSingleXml

file = "gs://ssb-prod-dapla-felles-data-delt/altinn3/RA-0595/2023/2/6/810409282_460784f978a2_ebc7af7e-4ebe-4883-b844-66ee6292a93a/form_460784f978a2.xml"

form_content=ParseSingleXml(file)

# Get a Pandas Dataframe representation of the contents of the file
df=form_content.to_dataframe()

df.head()

Requirements

  • dapla-toolbelt >=1.6.2
  • defusedxml >=0.7.1
  • xmltodict >=0.13.0
  • pandas >= 2.2.0

Installation

You can install SSB Altinn Python via poetry from PyPI:

poetry add ssb-altinn-python

To install this in the Jupyter-environment on Dapla, where it is ment to be used, it is required to install it in an virtual environment. It is recommended to do this in an ssb-project where the preferred tool is poetry.

Usage

Please see the Reference Guide for details.

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the MIT license, SSB Altinn Python is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from Statistics Norway's SSB PyPI Template.

Metadata

Release files for ssb-altinn-python 0.4.21

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ssb-altinn-python 0.4.21
File Size Uploaded
ssb_altinn_python-0.4.21.tar.gz 20.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ssb-altinn-python 0.4.21
File Interpreter ABI Platform
ssb_altinn_python-0.4.21-py3-none-any.whl Python 3 none any Details

Total release size: 37.7 kB

Release files / ssb_altinn_python-0.4.21.tar.gz

Download URL ssb_altinn_python-0.4.21.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fc645257c1e6f345e5901045d7ba88d3987ee2d37ed1090d89f47a8aee227073
BLAKE2b-256 checksum
How to use checksums
58cee6fd9270189d7d43a2d325e594a0edaf0fc2e77f734e6d9844ce64e2967b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.

Transparency log

Release files / ssb_altinn_python-0.4.21-py3-none-any.whl

Download URL ssb_altinn_python-0.4.21-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aed03a5de27c1a2ddd1a4fa20d41ceb8ad213e9699b7d7dc6c095e9dc400a9b0
BLAKE2b-256 checksum
How to use checksums
f3633263f97317e2b020a623ab20012827dadd6bff05d03d4a015596374ba023
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.4.21 This release

2 release files

0.4.19

2 release files

0.4.18

2 release files

0.4.16

2 release files

0.4.15

2 release files

0.4.14

2 release files

0.4.12

2 release files

0.4.11

2 release files

0.4.9

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page