I have write a script which does the transformation task, based on
lxml, and it works as expected. See attached file.
So, my question differs.
Is it possible, that the XSLT processor of lxml would malfunction when
it has no XML declaration?
<?xml version="1.0" encoding="UTF-8"?>
This is a part of a Focuscript which has an embedded XSLT stylesheet
which is extracted by a Focuscript manager for further processing.
<stylesheet>
<xsl:transform version="1.0"
xmlns="http://www.w3.org/2005/Atom"
xmlns:xpath="http://www.w3.org/2005/xpath-functions"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- CODE TRUNCATED -->
</xsl:transform>
</stylesheet>
When this stylesheet is extracted, it has no XML declaration, though it
is being extracted as parsed XML data by module lxml.etree.
data = ET.parse(focusfile)
fcs_elem = data.find(xmlns + "stylesheet")
xsl_elem = fcs_elem.find("{http://www.w3.org/1999/XSL/Transform}transform")
return xsl_elem
Schimon
On Tue, 2 Dec 2025 15:39:12 +0200
Schimon Jehudah via lxml - The Python XML Toolkit <[email protected]>
wrote:
> Greetings.
>
> I am working on a document uniformication framework, and it seems that
> the XSLT processor of lxml works differently than command xsltproc of
> libxslt.
>
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025 Schimon Jehudah
#
# SPDX-License-Identifier: MIT
# -*- coding: utf-8 -*-
"""
Transform XML documents.
"""
import lxml.etree as ET
import os
import sys
from typing import Any
def process_xml(data: ET._Element, stylesheet: ET._Element) -> ET._XSLTResultTree:
"""Process an XML document by a specified XSLT stylesheet."""
transform = ET.XSLT(stylesheet)
return transform(data)
#newdom = transform(data)
#xml_data_bytes = ET.tostring(newdom, pretty_print=True)
#xml_data_str = xml_data_bytes.decode("utf-8")
xml_data_bytes = memoryview(newdom)
xml_data_str = str(memoryview(newdom), "UTF-8")
return xml_data_str
def main() -> None:
"""Start software."""
if len(sys.argv) > 2:
if sys.argv[1] in ("-h", "--help"):
print("Transform an XML document by an XSLT stylesheet.")
elif os.path.exists(sys.argv[1]) and os.path.exists(sys.argv[2]):
xsl_file = sys.argv[1]
xml_file = sys.argv[2]
xsl_data = ET.parse(xsl_file)
xml_data = ET.parse(xml_file)
xml_proc = process_xml(xml_data, xsl_data)
xml_proc_str = ET.tostring(xml_proc, encoding="UTF-8")
sys.stdout.write(xml_proc_str.decode())
else:
print(f"Filename \"{sys.argv[1]}\" or \"{sys.argv[2]}\" does not exist.")
sys.exit(1)
else:
print("Please input location of XSLT and XML files as arguments.")
sys.exit(1)
if __name__ == "__main__":
main()
_______________________________________________
lxml - The Python XML Toolkit mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/lxml.python.org
Member address: [email protected]