Skip to content

About

Parser for Swiss federal law (OR, ZGB, BV) - extracts structured articles from official AkomaNtoso XML for LLM applications

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

fedlex-parser

Extract article text, headings and source references from three German-language Swiss laws. Each JSONL record identifies the exact Fedlex XML snapshot it came from.

Supported snapshots

Key Law SR Applicable from Pinned XML Body records
OR Obligationenrecht (Code of Obligations) 220 2024-01-01 German XML, file 9 1,600
ZGB Schweizerisches Zivilgesetzbuch (Swiss Civil Code) 210 2024-01-01 German XML, file 24 1,274
BV Bundesverfassung (Federal Constitution) 101 2024-01-01 German XML, file 14 232

Counts refer to XML article elements in the main act body, including grouped article labels and entries with repeal notices. The ZGB count includes 1,098 main-text records, 102 final-title records and 74 records in the section reproducing earlier provisions. The files are January 2024 snapshots. The project uses these fixed versions.

Install and verify

From the checkout root, run the following with Python 3.11 or newer. Installation fetches the declared Python dependencies. The tests and example run offline after installation.

python3 -m venv .venv
source .venv/bin/activate
python -m pip install .
python -m unittest discover -s test -p "test*.py" -v

GitHub Actions runs the same suite on Python 3.11 and 3.14. The suite uses real Fedlex excerpts for text and identity checks and synthetic inputs for failure cases. The optional live check downloads all three pinned files into a temporary cache and checks their identity, extraction and counts.

python -m unittest discover -s test -p "live_snapshots.py" -v

Offline example

Run python -m example from the checkout root. It parses the bundled OR Article 1 XML, writes UTF-8 articles.jsonl, reads it back and looks up art_1. Pass an output path as the first argument to use a different file.

OR (SR 220) · Art. 1 [art_1]
Snapshot 2024-01-01 (de)
Erste Abteilung: Allgemeine Bestimmungen / Erster Titel: Die Entstehung der Obligationen / Erster Abschnitt: Die Entstehung durch Vertrag / A. Abschluss des Vertrages / I. Übereinstimmende Willensäusserung / 1. Im Allgemeinen
1 Zum Abschlusse eines Vertrages ist die übereinstimmende gegenseitige Willensäusserung der Parteien erforderlich.
2 Sie kann eine ausdrückliche oder stillschweigende sein.
https://www.fedlex.admin.ch/filestore/fedlex.data.admin.ch/eli/cc/27/317_321_377/20240101/de/xml/fedlex-data-admin-ch-eli-cc-27-317_321_377-20240101-de-xml-9.xml

To use a full snapshot, pass the XML bytes from LawRepo.get_xml("ZGB") to parse_articles, then pass the returned articles and a UTF-8 text stream to write_jsonl. See the downloader, the parser and the example. For local XML, pass source=LawRepo.laws["ZGB"] to parse_articles when you expect a specific law. With no explicit source, the parser matches the XML work URI against the same three source configurations.

Record fields

Field Meaning
source Law abbreviation (law), Fedlex ELI path (law_id), sr_number, language, applicability_date and exact XML url. Articles in memory share one source object.
article_label and article_number Source label and normalized number, including suffixes, ranges or paired numbers.
e_id, article_id and section Original XML eId, unique lookup key within the snapshot and article section. Historical ZGB records use a lvl_u7/ prefix in their lookup keys because Fedlex reuses their source IDs. The ZGB final title uses the source's tit_fin/ IDs.
context and headings Ancestor headings from outermost to innermost, starting inside the body, and the article's own headings.
paragraphs and paragraph_labels Substantive text in source paragraph order, with labels at the corresponding indexes. An unnumbered paragraph has a null label.
notes Article-level source notes in encounter order, including explicit repeal notices. A repeal entry may have an empty paragraphs array.

Design and boundaries

Fedlex's Akoma Ntoso XML supplies structured article boundaries, heading hierarchy and version metadata directly. The parser validates the ELI work and expression URIs, law abbreviation, SR number, language and the explicitly named applicability date. Extraction follows the XML's order. The downloader uses connect and read timeouts, validates both downloaded and cached bytes and atomically replaces the cache file after successful validation. The default cache is raw/. If a cached file is invalid, the error gives the path to remove before retrying.

The extraction contract covers the three pinned German act bodies. It preserves paragraph boundaries, multiple text blocks, list labels and nesting, introductions, conclusions and explicit line breaks. Table rows appear in order with tab-separated cells. Text output normalizes layout whitespace and flattens inline formatting and link labels to plain text. It omits link targets, table styling, note anchors, notes attached to ancestor headings, preambles and standalone components such as the OR's appended transitional material. Unexpected article structures, invalid article identifiers, duplicate identities and empty bodies raise errors. The parser preserves source notices without assigning legal status.

The source fixture inventory identifies the excerpts used in the offline tests. The code is MIT-licensed. The XML excerpts come from Fedlex. Fedlex reuse information

About

Parser for Swiss federal law (OR, ZGB, BV) - extracts structured articles from official AkomaNtoso XML for LLM applications

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages