diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py new file mode 100644 index 0000000..ca60571 --- /dev/null +++ b/Backend-libXML/Parser.py @@ -0,0 +1,56 @@ +from lxml import etree + + +def xpath(source: str, xpath: str) -> str: + """ + Method used to get nodes from XML string using XPath + :param source: XML string used for selection + :param xpath: XPath query used for selection + :return: Nodes selected using XPath + """ + root = etree.XML(source) + nsmap = root.nsmap + + # LXML doesn't accept namespace with None key, + # so it need to be deleted if exists + try: + nsmap.pop(None) + except KeyError: + print(end="") + + result = root.xpath(xpath, namespaces=nsmap) + result_string = "" + for e in result: + result_string += etree.tostring(e, pretty_print=True).decode() + "\n" + return result_string + + + +def xsd(source: str, xsd: str) -> bool: + """ + Method used to validate XML string against XSD schema + :param source: XML string used for validation + :param xsd: XSD schema to validate XML against + :return: If the validation was successful or not + """ + xml_schema = etree.XMLSchema(etree.XML(xsd)) + + xml = etree.XML(source) + + return xml_schema.validate(xml) + + +def xslt(source: str, xslt: str) -> str: + """ + Method used to transformate XML string using XSLT + :param source: Transformed XML string + :param xslt: XSLT string used to transformate XML + :return: Result of transformation + """ + xslt_transform = etree.XSLT(etree.XML(xslt)) + + xml = etree.XML(source) + + transformated = xslt_transform(xml) + print(transformated) + return str(transformated) \ No newline at end of file diff --git a/Backend-libXML/__pycache__/Parser.cpython-310.pyc b/Backend-libXML/__pycache__/Parser.cpython-310.pyc new file mode 100644 index 0000000..a6014e1 Binary files /dev/null and b/Backend-libXML/__pycache__/Parser.cpython-310.pyc differ diff --git a/Backend-libXML/__pycache__/server.cpython-310.pyc b/Backend-libXML/__pycache__/server.cpython-310.pyc new file mode 100644 index 0000000..6fdb3a0 Binary files /dev/null and b/Backend-libXML/__pycache__/server.cpython-310.pyc differ diff --git a/Backend-libXML/main.py b/Backend-libXML/main.py new file mode 100644 index 0000000..9dbe0d8 --- /dev/null +++ b/Backend-libXML/main.py @@ -0,0 +1,17 @@ +import Parser + + +if __name__ == '__main__': + SOURCE = "sample/xslt.xml" + XSLT = "sample/sample.xslt" + + file = open(SOURCE, "r") + xml = file.read() + file.close() + + file = open(XSLT, "r") + xslt = file.read() + print(Parser.xslt(xml, xslt)) + + file.close() + diff --git a/Backend-libXML/sample/xpath.curl b/Backend-libXML/sample/xpath.curl new file mode 100644 index 0000000..f72790a --- /dev/null +++ b/Backend-libXML/sample/xpath.curl @@ -0,0 +1,5 @@ +#url = "localhost:8081/xpathpost" +url = "localhost:5000/xpath" +request = "POST" +data = "@xpath.json" +header = "Content-Type: application/json" diff --git a/Backend-libXML/sample/xpath.json b/Backend-libXML/sample/xpath.json new file mode 100644 index 0000000..70faa16 --- /dev/null +++ b/Backend-libXML/sample/xpath.json @@ -0,0 +1,6 @@ +{ + "data": "Hamlet2001-05-041falseMacbeth2000-12-131falseHarry Potter and the Sorcerer's Stone2005-04-292trueThe Long Walk2018-07-014trueMisery2018-01-314trueThink and Grow Rich2004-09-106trueThe Law of Success1982-05-096falsePatriot Games1995-10-215falseThe Sum of All Fears1992-09-195falseThe Alchemist2017-02-203falseHamlet1994-06-011falseMeasure for Measure1990-03-231falseHamlet1989-05-051trueHamlet1999-05-301trueThe Law of Success2004-11-266trueRomeo and Juliet1997-02-081trueThe Alchemist2009-08-213true", + "process": "/books/book[name = 'The Law of Success']", + "processor": "saxon", + "version": "2.0" +} diff --git a/Backend-libXML/sample/xsd.curl b/Backend-libXML/sample/xsd.curl new file mode 100644 index 0000000..615fff6 --- /dev/null +++ b/Backend-libXML/sample/xsd.curl @@ -0,0 +1,4 @@ +url = "http://localhost:5000/xsd" +data = "@xsd.json" +header = "Content-Type: application/json" +request = POST diff --git a/Backend-libXML/sample/xsd.json b/Backend-libXML/sample/xsd.json new file mode 100644 index 0000000..91dd2f2 --- /dev/null +++ b/Backend-libXML/sample/xsd.json @@ -0,0 +1,6 @@ +{ + "data": "TestTest3", + "process": " ", + "processor": "saxon", + "version": "1.0" +} diff --git a/Backend-libXML/sample/xslt.curl b/Backend-libXML/sample/xslt.curl new file mode 100644 index 0000000..b5099ff --- /dev/null +++ b/Backend-libXML/sample/xslt.curl @@ -0,0 +1,4 @@ +url = "http://localhost:5000/xslt" +data = "@xslt.json" +header = "Content-Type: application/json" +request = POST diff --git a/Backend-libXML/sample/xslt.json b/Backend-libXML/sample/xslt.json new file mode 100644 index 0000000..f833a40 --- /dev/null +++ b/Backend-libXML/sample/xslt.json @@ -0,0 +1,6 @@ +{ + "data": "Hamlet2001-05-041falseMacbeth2000-12-131falseHarry Potter and the Sorcerer's Stone2005-04-292trueThe Long Walk2018-07-014trueMisery2018-01-314trueThink and Grow Rich2004-09-106trueThe Law of Success1982-05-096falsePatriot Games1995-10-215falseThe Sum of All Fears1992-09-195falseThe Alchemist2017-02-203falseHamlet1994-06-011falseMeasure for Measure1990-03-231falseHamlet1989-05-051trueHamlet1999-05-301trueThe Law of Success2004-11-266trueRomeo and Juliet1997-02-081trueThe Alchemist2009-08-213true", + "process": "", + "processor": "saxon", + "version": "1.0" +} diff --git a/Backend-libXML/server.py b/Backend-libXML/server.py new file mode 100644 index 0000000..b34ce99 --- /dev/null +++ b/Backend-libXML/server.py @@ -0,0 +1,69 @@ +from flask import Flask +from flask import request +from lxml import etree +import json +import time +import Parser + + +app = Flask(__name__) + +@app.route("/xpath", methods=["POST"]) +def xpath(): + request_data = request.get_json() + xml = request_data['data'] + xpath = request_data['process'] + + response = dict() + start = time.time_ns() + try: + response['result'] = Parser.xpath(xml, xpath) + response['status'] = "OK" + except BaseException as e: + response['result'] = str(e) + response['status'] = "ERR" + finally: + exec_time = (time.time_ns() - start) / 10**6 + response['time'] = f"{exec_time:.03f}" + response['processor'] = "libxml2 over lxml" + return json.dumps(response) + +@app.route("/xsd", methods=["POST"]) +def xsd(): + request_data = request.get_json() + xml = request_data['data'] + xsd = request_data['process'] + + response = dict() + start = time.time_ns() + try: + response['result'] = Parser.xsd(xml, xsd) + response['status'] = "OK" + except BaseException as e: + response['result'] = str(e) + response['status'] = "ERR" + finally: + exec_time = (time.time_ns() - start) / 10**6 + response['time'] = f"{exec_time:.03f}" + response['processor'] = "libxml2 over lxml" + return json.dumps(response) + +@app.route("/xslt", methods=["POST"]) +def xslt(): + request_data = request.get_json() + xml = request_data['data'] + xslt = request_data['process'] + + response = dict() + start = time.time_ns() + try: + response['result'] = Parser.xslt(xml, xslt) + response['status'] = "OK" + except BaseException as e: + response['result'] = str(e) + response['status'] = "ERR" + finally: + exec_time = (time.time_ns() - start) / 10**6 + response['time'] = f"{exec_time:.03f}" + response['processor'] = "libxml2 over lxml" + return json.dumps(response) \ No newline at end of file