Project

Profile

Help

Problems passing a path of the form "C:/xyz/abc" to result-document

Added by evan massena almost 12 years ago

Hello.

I've been moving code that was running on a unix-like platform to Windows and have come across a problem for which I can't find a good solution.

I have the following code (again) (Transformation just contains some absolute paths as Strings):

public Transform(
    final Transformation transformation)
    throws SaxonApiException,
      IOException
  {
    assert transformation != null;
    transformation.check();
    this.transformation = transformation;

    Transform.createOutputDirectory(transformation);

    this.style_file = new File(transformation.getStylesheetFile());
    if (this.style_file.isFile() == false) {
      final String path = this.style_file.toString();
      throw new FileNotFoundException(path);
    }
    this.style_source = new StreamSource(this.style_file);

    this.input_file = new File(transformation.getDocumentFile());
    if (this.input_file.isFile() == false) {
      final String path = this.input_file.toString();
      throw new FileNotFoundException(path);
    }
    this.input_source = new StreamSource(this.input_file);

    this.output_path =
      transformation.getOutputDirectory()
        + File.separatorChar
        + transformation.getOutputFile();
    this.output_file = new File(this.output_path);
    this.output_serial = new Serializer(this.output_file);

    this.config = new Configuration();
    this.config.setXIncludeAware(true);
    this.config.setValidation(false);
    this.config.setLineNumbering(true);
    this.processor = new Processor(this.config);

    this.xslt = this.processor.newXsltCompiler();
    this.xslt_exec = this.xslt.compile(this.style_source);
    this.xslt_trans = this.xslt_exec.load();
    this.xslt_trans.setDestination(this.output_serial);
    this.xslt_trans.setMessageListener(this);
    this.xslt_trans.setErrorListener(this);
    this.xslt_trans.setSource(this.input_source);

    final Properties parameters = transformation.getStylesheetParameters();
    for (final Entry e : parameters.entrySet()) {
      final Object okey = e.getKey();
      if ((okey instanceof String) == false) {
        throw new IllegalArgumentException("Key "
          + okey
          + " is not a string (is "
          + okey.getClass().getCanonicalName()
          + ")");
      }
      final String key = (String) e.getKey();
      final Object oval = e.getValue();
      if ((oval instanceof String) == false) {
        throw new IllegalArgumentException("Key "
          + key
          + " has non-string value (is "
          + oval.getClass().getCanonicalName()
          + ")");
      }
      final String val = (String) e.getValue();
      this.xslt_trans.setParameter(new QName(key), new XdmAtomicValue(val));
      this.xslt_trans.transform();
    }

Given the following XSLT stylesheet and XML input:


  

  

  
    
    
      

Hello



  Hello

When passing "C:\MinGW\msys\1.0\home\Administrator\dev\transformed.xml" as the $file_output parameter to the stylesheet, I'll receive the following error:

fatal: Resolved URL is malformed; SystemID: file:/C:/MinGW/msys/1.0/home/Administrator/dev/result.xsl; Line#: 16; Column#: -1

Unfortunately, it seems as though something in Saxon (possibly in the Java standard library) is interpreting 'C:' as a protocol. Prefixing with 'file://', however, gives:

fatal: Cannot write to URI file://C:%5CMinGW%5Cmsys%5C1.0%5Chome%5CAdministrator%5Cdev%5Ctransformed.xml (URI has an authority component); SystemID: file:/C:/MinGW/msys/1.0/home/Administrator/dev/result.xsl; Line#: 16; Column#: -1

So that's clearly not the answer, either. What's the correct way to deal with paths in this situation? For various reasons, I can't use relative paths (and I suspect they'd not be any easier to deal with, anyway).


Replies (2)

RE: Problems passing a path of the form "C:/xyz/abc" to result-document - Added by Michael Kay almost 12 years ago

The href attribute of xsl:result-document is specified to expect a URI, not a filename.

In the Windows world there is a lot of software around that accepts filenames in places where a URI is expected. However, a filename is not a URI and the XSLT specification gives no license to accept one here. Since you are passing the value as a stylesheet parameter, the best way is to generate a URI in your Java code, using (perhaps)

this.output_file = new File(this.output_path).toURI().toString();

    (1-2/2)

    Please register to reply