Skip to main content

marc-bibframe

Test PyPI

Convert MARC records to BIBFRAME RDF in Python, using the Library of Congress marc2bibframe2 XSLT.

The stylesheet is vendored in this package, so there is nothing to install alongside it and no Java or Saxon involved, just lxml, pymarc and rdflib.

Install

pip install marc-bibframe

Command line

You can use it from the command line:

$ marc-bibframe record.mrc
$ marc-bibframe record.xml --format json-ld --baseuri https://example.edu/catalog/
$ yaz-marcdump -o marcxml big.mrc | marc-bibframe -f nt -o big.nt

Binary MARC21 and MARCXML are both accepted and told apart by their content, so there is no flag for it. With no filename it reads standard input.

--format takes turtle (the default), json-ld, ntriples, xml, or rdfxml. The last is the stylesheet's own output passed through without a parse into rdflib, which is faster and keeps the transform's exact shape.

marc-bibframe --help lists the stylesheet parameters, which are also available as flags.

Use as a library

Or you can use it as a function in your own Python programs:

from marc_bibframe import marc_to_graph

with open("record.mrc", "rb") as fh:
    graph = marc_to_graph(fh, baseuri="https://example.edu/catalog/")

print(graph.serialize(format="turtle"))

MARCXML is accepted directly, and RDF/XML is available if you would rather not pay for a parse into an rdflib.Graph:

from marc_bibframe import marc_to_marcxml, marcxml_to_graph, marcxml_to_rdfxml

marcxml = marc_to_marcxml(open("record.mrc", "rb").read())
graph = marcxml_to_graph(marcxml)
rdfxml = marcxml_to_rdfxml(marcxml)

All four functions accept the stylesheet's parameters as keyword arguments — baseuri, idfield, idsource, localfields, bcp47_inference and generation_datestamp. See the docstring on marcxml_to_rdfxml for what each one does.

A note on the URIs it mints

MARC does not identify most of what it describes, so the transform invents URIs for them, built from baseuri and the record's own id:

https://example.edu/catalog/99123456#Work
https://example.edu/catalog/99123456#Instance
https://example.edu/catalog/99123456#Agent100-3
https://example.edu/catalog/99123456#Topic650-5

These are not authority URIs. They are scoped to the record they came from, so two records describing the same person produce two different agent URIs. If you need them reconciled against an authority (LC, Wikidata, your own store) that happens after this library hands you the graph.

For the same reason the default baseuri is http://example.org/, matching the stylesheet's own. Pick something under your control, and prefer a namespace that cannot be mistaken for an authority's: minting http://id.loc.gov/resources/99123456#Agent100-3 produces URIs that look like LC's but are not.

Keeping up with upstream

src/marc_bibframe/xsl/ is a copy of the LC stylesheets — see UPSTREAM for the tag and commit. Local changes are not edited in place; they live as patches in patches/ and are reapplied on every re-vendor:

./scripts/vendor.py v3.2.0

If a patch stops applying the script says so and stops, which is usually the signal that it was fixed upstream and can be deleted. The aim is to keep patches/ empty; anything in there should also be reported to LC.

The test suite checks the vendored tag against LC's latest release and fails when there is a newer one, with the command to run. It skips rather than fails if the GitHub API is unreachable.

The stylesheets are CC0. This wrapper is MIT.

Reproducible output

The transform stamps the current time into the work's admin metadata. generation_datestamp (--datestamp) overrides it, which makes the RDF/XML byte-for-byte reproducible. The other serializations still vary between runs: rdflib mints fresh blank node labels every time it serializes.

Development

uv sync
uv run pytest

Metadata

Release files for marc-bibframe 0.2.2

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

Source distribution (sdist)

Source distribution for marc-bibframe 0.2.2
File Size Uploaded
marc_bibframe-0.2.2.tar.gz 161.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for marc-bibframe 0.2.2
File Interpreter ABI Platform
marc_bibframe-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 321.1 kB

Release files / marc_bibframe-0.2.2.tar.gz

Download URL marc_bibframe-0.2.2.tar.gz
Size 161.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0bd24643c6c3d4728684d59c46594907f80e6d9af0965d92929fa88ed768ef1a
BLAKE2b-256 checksum
How to use checksums
784a3706feb2f5546120bd33114e6c09478e41b75507689927a1975b8d4e6e5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.12 {"installer":{"name":"uv","version":"0.12.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / marc_bibframe-0.2.2-py3-none-any.whl

Download URL marc_bibframe-0.2.2-py3-none-any.whl
Size 159.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
630d03871d5d4ebe73730c5449b5bb9cbd12e6976cb8d82a2303740c7cb3637e
BLAKE2b-256 checksum
How to use checksums
ad28b998f69824db297c9df62140bcb92c68f6c38bfdf2bf475078cae352559f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.12 {"installer":{"name":"uv","version":"0.12.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.3.0

2 release files

This release

0.2.2 This release

2 release files

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