Good evening.

Ptoblem solved.

It was an issue with namespace.

When converting JSON to XML, and not saving it to a file, or converting
to string and then parse to XML element, then it is required to
explicitly set an xmlns to each created tag.

BEFORE

root = ET.Element("map", xmlns=xmlns["xf"])
descendant = ET.SubElement(ancestor, tag_name)

AFTER

root = ET.Element("map", xmlns=xmlns["xf"])
descendant = ET.SubElement(ancestor, f"{{{xmlns["xf"]}}}{tag_name}")

The problem was solved.

Regards,
Schimon

On Tue, 2 Dec 2025 16:34:55 +0200
Schimon Jehudah via lxml - The Python XML Toolkit <[email protected]>
wrote:

> 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 <https://schapps.woodpeckersnest.eu/slixfeed/>
#
# SPDX-License-Identifier: MIT

# -*- coding: utf-8 -*-

import json
from json.decoder import JSONDecodeError
import lxml.etree as ET
from slixfeed.parser.uri import ParserUri
from slixfeed.utility.datetime import UtilityDateTime
from slixfeed.utility.logger import UtilityLogger
from slixfeed.utility.uri import UtilityUri
import sys
from typing import Any

json_types = dict(zip(
    map(type, [False,   {1:10}, 1,     0.1,   [],   "",    None]),
               "boolean map     number number array string null".split()
))

logger = UtilityLogger(__name__)

xmlns = {
  "tf" : "http://digdeper.i2p/focus";,
  "xf" : "http://www.w3.org/2005/xpath-functions"}

class UtilityFocuscript:

    def convert_json_to_xml(json_data: dict[str, Any]) -> ET._Element:
        function_name = sys._getframe().f_code.co_name
        logger.debug(f"{function_name}		Start")
        def build_xml_element(ancestor: ET.Element, data: Any) -> None:
            if isinstance(data, dict):
                for key, value in data.items():
                    tag_name = json_types[type(value)]
                    descendant = ET.SubElement(ancestor, f"{{{xmlns["xf"]}}}{tag_name}", key=key)
                    build_xml_element(descendant, value)
            elif isinstance(data, list):
                for item in data:
                    tag_name = json_types[type(item)]
                    item_element = ET.SubElement(ancestor, f"{{{xmlns["xf"]}}}{tag_name}")
                    build_xml_element(item_element, item)
            else:
                ancestor.text = str(data)
        root = ET.Element("map", xmlns=xmlns["xf"])
        build_xml_element(root, json_data)
        return root
_______________________________________________
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]

Reply via email to