Project

Profile

Help

Bug #5537

closed

Xslt30Processor.transform_to_file not giving the correct output

Added by O'Neil Delpratt almost 2 years ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Category:
Python
Start date:
2022-05-24
Due date:
% Done:

100%

Estimated time:
Found in version:
1.2.1
Fixed in version:
11.4
Platforms:

Description

Reported by a user here: https://stackoverflow.com/questions/72359679/saxoncs-transform-to-file-executed-in-a-loop-doesnt-transform-but-gives-no

The following stylesheet and python script does not create the correct output in the files created.

Stylesheet:



My transformation stylesheet file contains:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <cities>
            <xsl:for-each select="cities/country">
                <city name="{@capital}" isCapital="true"/>
            </xsl:for-each>
        </cities>
    </xsl:template>
</xsl:stylesheet>

Python script:

import os
import xml.etree.ElementTree as ET
from saxonpy import *

def main():
    print('starting code...')
    source_XML = '''
        <data>
            <country name="Denmark" capital="Copenhagen"/>
            <country name="Germany" capital="Berlin"/>
            <country name="France" capital="Paris"/>
        </data>
    '''
    parentroot = ET.fromstring(source_XML)
    children = list(parentroot)

    # create individual raw xmls
    try:
        with PySaxonProcessor(license=False) as proc:
            proc.set_cwd(os.getcwd())
            xsltproc = proc.new_xslt30_processor()
            cnt = 0
            for child in children:
                cnt = cnt + 1
                childroot = ET.Element("cities")
                childroot.append(child)
                tempfile_tree = ET.ElementTree(childroot)
   
                # tempfile = "C:\\pythonProject\\stackoverflow\\tmp.xml"
                # tempfile = "C:\\gaga\\tmp.xml"
                # tempfile = os.path.abspath("tmp.xml")
                tempfile = "tmp.xml"

                transformedfile = f"output_{cnt}.xml"
                with open(tempfile, 'wb') as f:
                    tempfile_tree.write(f, encoding='utf-8', xml_declaration=True)

                    xsltproc.set_property("s",tempfile)
                    xsltproc.transform_to_file(source_file=tempfile,
                                           stylesheet_file="transformer.xsl",
                                           output_file=transformedfile)
                    #print(valueStr)
                    print(f"{transformedfile} has been created.")
    except Exception as e:
        print(e)

if __name__ == "__main__":
    main()

Please register to edit this issue

Also available in: Atom PDF