Created
April 28, 2021 08:44
-
-
Save arbakker/42f73421654b4d5f29211ead2ae0ab25 to your computer and use it in GitHub Desktop.
Commandline tool to convert ISO(19119 and 19139) metadata to DCAT-AP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Commandline tool to convert ISO(19119 and 19139) metadata to DCAT-AP based on https://github.com/SEMICeu/iso-19139-to-dcat-ap. | |
Requires: lxml | |
Usage: `./iso2dcat.py --help` | |
""" | |
from lxml.etree import XML, XSLT, tostring | |
from urllib.request import urlopen | |
import argparse | |
XSL_URL = "https://raw.githubusercontent.com/SEMICeu/iso-19139-to-dcat-ap/master/iso-19139-to-dcat-ap.xsl" | |
def main(xml, xsl): | |
if xml.startswith("http://") or xml.startswith("https://"): | |
with urlopen(xml) as response: | |
xml_string = response.read() | |
else: | |
with open(xml, mode="rb") as xml_file: | |
xml_string = xml_file.read() | |
if xsl.startswith("http://") or xsl.startswith("https://"): | |
with urlopen(xsl) as response: | |
xsl_string = response.read() | |
else: | |
with open(xsl, mode="rb") as xsl_file: | |
xsl_string = xsl_file.read() | |
xml = XML(xml_string) | |
xsl = XML(xsl_string) | |
transform = XSLT(xsl) | |
print(tostring(transform(xml), pretty_print=True).decode("utf-8")) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Convert ISO19139 to DCAT-AP") | |
parser.add_argument("xml", metavar="XML", type=str, help="XML file or URL") | |
parser.add_argument("--xsl", metavar="XSL", type=str, help="XSLT file or URL") | |
args = parser.parse_args() | |
xsl = args.xsl | |
if xsl is None: | |
xsl = XSL_URL | |
main(args.xml, xsl) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment