From 610cf108fd2331d694f4bb4b294aea99a0c42b69 Mon Sep 17 00:00:00 2001 From: Mazztok45 Date: Mon, 9 Dec 2024 14:12:28 +0100 Subject: [PATCH] advancing work on processing codemeta from OAI-PMH to be deposited --- .../convertSoftware_from_json_toXml.py | 2 +- src/swmath2swh/staging_deposit_v2.py | 18 +++++++++++------- xslt/software/xslt_SWH_deposit.xslt | 0 3 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 xslt/software/xslt_SWH_deposit.xslt diff --git a/src/swmath2swh/convertSoftware_from_json_toXml.py b/src/swmath2swh/convertSoftware_from_json_toXml.py index 3ff7381d..8cab6bb8 100644 --- a/src/swmath2swh/convertSoftware_from_json_toXml.py +++ b/src/swmath2swh/convertSoftware_from_json_toXml.py @@ -122,7 +122,7 @@ def json_to_xml(json_data): # Add references references_elem = ET.SubElement(root, 'references') for ref in result.get('references', []): - reference_elem = ET.SubElement(references_elem, 'reference') + reference_elem = ET.SubElement(references_elem, 'references') reference_elem.text = str(ref) return root diff --git a/src/swmath2swh/staging_deposit_v2.py b/src/swmath2swh/staging_deposit_v2.py index 2a7915d6..de5fa513 100644 --- a/src/swmath2swh/staging_deposit_v2.py +++ b/src/swmath2swh/staging_deposit_v2.py @@ -6,22 +6,26 @@ import time import tempfile import os +import requests env = os.environ.copy() env['SWMATH_USER_DEPOSIT'] = os.getenv('SWMATH_USER_DEPOSIT') env['SWMATH_PWD_DEPOSIT'] = os.getenv('SWMATH_PWD_DEPOSIT') env['SWMATH_PWD_DEPOSIT'] = os.getenv('SWMATH_PWD_DEPOSIT') - -import requests +xsl_filename = '../xslt/software/xslt_SWH_deposit.xslt' r = requests.get("https://oai.staging.mardi4nfdi.org/oai/OAIHandler?verb=GetRecord&metadataPrefix=codemeta&identifier=oai:swmath.org:4532") - -xml = r.text - - +xml_str = r.content +dom = ET.fromstring(xml_str) +xslt = ET.parse(xsl_filename) +transform = ET.XSLT(xslt) +newdom = transform(dom) +formatted_newdom = ET.tostring(newdom, pretty_print=True, encoding='unicode') + + # Add this before the write statement # Write transformed XML to a temporary file with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.xml') as temp_file: - temp_file.write(xml) + temp_file.write(formatted_newdom) temp_filename = temp_file.name print(f"Temporary file created: {temp_filename}") # Format current time for deposit status diff --git a/xslt/software/xslt_SWH_deposit.xslt b/xslt/software/xslt_SWH_deposit.xslt new file mode 100644 index 00000000..e69de29b