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>
My python code:
import os
import xml.etree.ElementTree as ET
from saxonpy import PySaxonProcessor
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
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)
try:
with PySaxonProcessor(license=False) as proc:
proc.set_cwd(os.getcwd())
xsltproc = proc.new_xslt30_processor()
xsltproc.transform_to_file(source_file=tempfile,
stylesheet_file="transformer.xsl",
output_file=transformedfile)
print(f"{transformedfile} has been created.")
except Exception as e:
print(e)
if __name__ == "__main__":
main()
my problem
I have saxonpy imported. I'm trying to run saxonc's transform_to_file() inside a loop. I'm unable to get the transformed output files. Depending on what I set for tempfile, i get
# I/O error reported by XML parser processing C:\pythonProject\stackoverflow\░╚╒E ⌂:
# unknown protocol: c. Caused by java.net.MalformedURLException: unknown protocol: c
or
Content is not allowed in prolog
(this is definitely not the case, I checked the tempfile with a hexeditor)
or no error but output file containing only:
# <?xml version="1.0" encoding="UTF-8"?>
# <cities/>
but also correct outputs (but I can't reproduce it anymore).
Note: I disabled MAX_PATH when installing python 3.10. Note: using Pycharm with poetry venv
CodePudding user response:
Using SaxonC 11.3 I managed to run the python script above with the minor change:
from saxonpy import PySaxonProcessor
Replaced with:
from saxonc import *
I got the output:
starting code...
output_1.xml has been created.
output_2.xml has been created.
output_3.xml has been created.
These files all have the following content:
<?xml version="1.0" encoding="UTF-8"?>
<cities/>
As I workaround I replaced transform_to_file
with transform_to_string
:
valueStr = xsltproc.transform_to_string(source_file=tempfile,
stylesheet_file="transformer.xsl")
#output_file=transformedfile)
print(valueStr)
This does produce the correct output:
starting code...
source in transformFiletoString=tmp.xml stylsheet=transformer.xsl
<?xml version="1.0" encoding="UTF-8"?>
<cities>
<city name="Copenhagen" isCapital="true"/>
</cities>
output_1.xml has been created.
<?xml version="1.0" encoding="UTF-8"?>
<cities>
<city name="Berlin" isCapital="true"/>
</cities>
output_2.xml has been created.
<?xml version="1.0" encoding="UTF-8"?>
<cities>
<city name="Paris" isCapital="true"/>
</cities>
output_3.xml has been created.
CodePudding user response:
I have now installed SaxonC 1.2.1
I got the correct output with the following 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()