RE: [xsl] XSLT 2: File path to URI for non-XML file

Subject: RE: [xsl] XSLT 2: File path to URI for non-XML file
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 8 Oct 2008 13:45:43 +0100
> Here is the function I came up with, which will convert an 
> absolute Windows file path into a URI:
> 
> <xsl:function name="my:windowsfilepath_to_uri" as="xs:string">
>    <xsl:param name="filepath" as="xs:string"/>
>    <!-- Conversion steps:
>         1) replace backslashes with slashes
>         2) encode percent sign
>         3) percent-encode URI fragment identifier character
>         4) percent-encode characters illegal in URIs, using iri-to-uri
>         5) add three slashes before drive letter
>         6) prefix result with "file:" -->
>    <xsl:value-of
>      select="concat(
>                'file:',
>                replace(
>                  iri-to-uri(
>                    replace(
>                      replace(
>                        replace($filepath, '\\', '/'),
>                        '%',
>                        '%25'),
>                      '#',
>                      '%23')),
>                  '^([A-Za-z]):',
>                  '///$1:'))"/>
> </xsl:function>
> 
> This conforms to the rules stated in the IEBlog post "File 
> URIs in Windows" [1]. Suggestions for improvements on the 
> function are welcome.
> 

Unfortunately the mapping from Windows filenames to URIs implemented in the
MSDN blog that you reference is not necessarily the same as the mapping in
other bits of software. For example, Java maps UNC filenames (\\server\file)
to file:////server/file rather than file://server/file. Also, if you try to
create a file with %25 in the URI, I've no idea whether the resulting file
will contain "%" or "%25" - you'll have to try it and see. Basically, there
are no standards here, and your mileage depends on the environment you are
running in. I know for a fact that Saxon on .NET sometimes behaves
differently from Saxon on Java, though I can't point you to the specific
details.

Michael Kay
http://www.saxonica.com/

Current Thread