XSLT now return prettified output

This commit is contained in:
2023-03-03 10:30:15 +01:00
parent 81737ed759
commit a46d7349a8
2 changed files with 8 additions and 8 deletions

View File

@@ -2,7 +2,7 @@ from lxml import etree
from io import BytesIO from io import BytesIO
def format(source: str, prettify: bool) -> str: def formatXML(source: str, prettify: bool) -> str:
"""Method used to format XML """Method used to format XML
:param source: XML to format :param source: XML to format
@@ -85,17 +85,17 @@ def xsd(source: str, xsd: str) -> bool:
def xslt(source: str, xslt: str) -> str: def xslt(source: str, xslt: str) -> str:
""" """
Method used to transformate XML string using XSLT Method used to transform XML string using XSLT
:param source: XML string to transform :param source: XML string to transform
:param xslt: XSLT string used to transformate XML :param xslt: XSLT string used to transform XML
:return: Result of transformation :return: Result of transformation
""" """
xslt_input = BytesIO(xslt.encode("utf-8")) xslt_input = BytesIO(xslt.encode("utf-8"))
xslt_transform = etree.XSLT(etree.parse(xslt_input).getroot()) xslt_transform = etree.XSLT(etree.parse(xslt_input))
document_input = BytesIO(source.encode("utf-8")) document_input = BytesIO(source.encode("utf-8"))
xml = etree.parse(document_input).getroot() xml = etree.parse(document_input).getroot()
result = xml.xslt(xslt_transform).decode() transformed = str(xslt_transform(xml))
return result return formatXML(transformed, True)

View File

@@ -37,9 +37,9 @@ def process_xml(request: request, type: str) -> str:
elif (type == "xpath"): elif (type == "xpath"):
response_json['result'] = Parser.xpath(data, process) response_json['result'] = Parser.xpath(data, process)
elif (type == "prettify"): elif (type == "prettify"):
response_json['result'] = Parser.format(data, True) response_json['result'] = Parser.formatXML(data, True)
elif (type == "minimize"): elif (type == "minimize"):
response_json['result'] = Parser.format(data, False) response_json['result'] = Parser.formatXML(data, False)
else: else:
raise ValueError("Valid operation types are: xsd, xslt, xpath") raise ValueError("Valid operation types are: xsd, xslt, xpath")