Project

Profile

Help

Question on apply_templates_returning_file

Added by Martin Honnen almost 5 years ago

Using the Python saxonc module of Saxon HEC 1.2.1 on Windows 10 I am struggling to use apply_templates_returning_file of XSLT 3 processor, it seems that any input and source file names passed in a resolved relative to the current working directory while the output_file is attempted to be written file:/C:/output_file where with usual Windows settings the access is denied.

So to explain it with some code, running

import saxonc

with saxonc.PySaxonProcessor() as proc:
    print(proc.version)

    xslt30_processor = proc.new_xslt30_processor()

    result = xslt30_processor.apply_templates_returning_string(source_file = "input-sample1.xml", stylesheet_file = "identity.xsl")

    print(result)

in a local user directory where the files input-sample1.xml and identity.xsl are present runs fine and seems to resolve those local file names given in the arguments automatically to the current working directory.

When I try to use an output_file keyword argument, as in

import saxonc

with saxonc.PySaxonProcessor() as proc:
    print(proc.version)

    xslt30_processor = proc.new_xslt30_processor()

    xslt30_processor.apply_templates_returning_file(source_file = "input-sample1.xml", stylesheet_file = "identity.xsl", output_file = "output_file_test1.xml")

I get an error

Error Failed to create output file file:/C:/output_file_test1.xml: access denied

Is that supposed to work that way? Why are input and XSLT file names resolved relatively to the current working directory but the output file is written to the root directory and not to the current working directory?


Replies (5)

Please register to reply

RE: Question on apply_templates_returning_file - Added by O'Neil Delpratt almost 5 years ago

Hi,

Thanks for reporting this issue. This is not right that the output files gets written to the root directory it should be to the cwd. I will raise a bug against it.

What is happening is the resolving is done against what is set in the cwd, which you can set yourself. If the cwd is not set, then it writes the file to the root directory which is probably not the right thing to do.

As a workaround you can do the following:

xslt30_processor = proc.new_xslt30_processor()
xslt30_processor.set_cwd("C:/pathlocation")

You can also set the cwd on the SaxonProcessor object which would filter to all created Processors created. i.e. proc.set_cwd('C:/pathlocation')

RE: Question on apply_templates_returning_file - Added by O'Neil Delpratt almost 5 years ago

Just to add the cwd would work work for locating any relative filenames for stylesheets, source files given.

RE: Question on apply_templates_returning_file - Added by O'Neil Delpratt almost 5 years ago

On second thoughts we think the following should apply:

  1. When executing processor on the commandline then the output files should be written to the cwd to where the command has been executed.
  2. When executing processor via an API then the user should be more explicit as to where the file should be written. Therefore setting the cwd is required.

At the moment given relative file names the user can set a different location to the writing of the file and where the stylesheet file lives. We will look into this issue.

RE: Question on apply_templates_returning_file - Added by Martin Honnen almost 5 years ago

I can live with setting the cwd, doing an import os and then

    xslt30_processor.set_cwd(os.getcwd())

seems to solve it, at least in standalone test cases.

Note, however, that transform_to_file suffers from the same problem of trying to write to file:///C:/ and there even the documentation at https://www.saxonica.com/saxon-c/doc/html/saxonc.html#PyXslt30Processor-transform_to_file has a simple example with only relative file names:

xsltproc.transform_to_file(source_file="cat.xml", stylesheet_file="test1.xsl", output_file="result.xml")

RE: Question on apply_templates_returning_file - Added by O'Neil Delpratt almost 5 years ago

Hi,

The transform_to_file requires the cwd to be set too.

    (1-5/5)

    Please register to reply