Project

Profile

Help

PyXsltProcessor.transform_to_file() not working. » try_saxon.py

Daniel Haley, 2021-03-18 22:41

 
1
import os
2
import sys
3
import saxonc
4

    
5

    
6
def try_to_string(proc):
7
    xsltproc = proc.new_xslt_processor()
8
    xsltproc.set_parameter("before", proc.make_string_value("two"))
9
    xsltproc.set_parameter("after", proc.make_string_value("duece"))
10
    result = xsltproc.transform_to_string(source_file="try_saxon_input.xml",
11
                                          stylesheet_file="try_saxon.xsl")
12
    return bool({'duece' in result})
13

    
14

    
15
def try_to_file(proc, proc_type=None):
16
    output_filename = "try_saxon_output.xml"
17

    
18
    if os.path.isfile(output_filename):
19
        os.remove(output_filename)
20

    
21
    if proc_type == "3.0":
22
        # WORKS
23
        xsltproc = proc.new_xslt30_processor()
24
    else:
25
        # DOES NOT WORK (No file output. No error message.)
26
        xsltproc = proc.new_xslt_processor()
27

    
28
    xsltproc.set_parameter("before", proc.make_string_value("two"))
29
    xsltproc.set_parameter("after", proc.make_string_value("duece"))
30
    xsltproc.set_cwd(os.getcwd())
31
    xsltproc.transform_to_file(source_file="try_saxon_input.xml",
32
                               stylesheet_file="try_saxon.xsl",
33
                               output_file=output_filename)
34
    try:
35
        with open(output_filename) as output_file:
36
            return bool({'duece' in output_file})
37
    except FileNotFoundError:
38
        return False
39

    
40

    
41
def main():
42
    with saxonc.PySaxonProcessor(license=False) as proc:
43
        print(f"Processor version: {proc.version}")
44
        print(f"Did try_to_string() work? - {try_to_string(proc)}")
45
        print(f"Did try_to_file() work? - {try_to_file(proc)}")
46
        print(f"Did try_to_file() (3.0) work? - {try_to_file(proc, '3.0')}")
47

    
48

    
49
if __name__ == '__main__':
50
    sys.exit(main())
(1-1/3)