[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Escaping special characters for *nix file path


Subject: Re: [xsl] Escaping special characters for *nix file path
From: Lighton Phiri <lighton.phiri@xxxxxxxxx>
Date: Sun, 29 Jul 2012 06:30:45 +0200

> Well, spaces and quotes are allowed in Unix (and Linux) filenames, so
> I'm not sure what you are doing here.

Yes they are allowed, but special characters need to be escaped for
one to access a file path. One of the things I need to do is write
content to the directory paths that have special characters. For
instance

phiri@PHRLIG001:~$ touch ../data/Sites/3218AD Eland\'s
Bay/Bobbejaansberg/BB15/testFile.txt
touch: cannot touch `Bay/Bobbejaansberg/BB15/testFile.txt': No such
file or directory

But this works
phiri@PHRLIG001:~$ touch ../data/Sites/3218AD\ Eland\'s\
Bay/Bobbejaansberg/BB15/testFile.txt

>This is crazy.
I ran out of ideas :(

>replace($filename, "[ '\\`&;]", "\\&")
>will probably do what you want.
>Or use \s instead of space if there might be newlines.
>You might also need to replace " with \"

I am getting an error when I try what you suggested.

<xsl:value-of select="{replace($filename, "[ '\\`&;]", "\\&")}" />

Error on line 81 column 48 of experiments.xsl:
  SXXP0003: Error reported by XML parser: Element type "xsl:value-of"
must be followed by
  either attribute specifications, ">" or "/>".
Failed to compile stylesheet. 1 error detected.

My file paths have special characters in them and those characters
include 'square brackets', 'parentheses', 'ampersands', etc. all of
which are interpreted differently by the regular expression given by
second argument of replace function.

Lighton Phiri
http://lightonphiri.org


On 29 July 2012 05:39, Liam R E Quin <liam@xxxxxx> wrote:
> On Sun, 2012-07-29 at 04:43 +0200, Lighton Phiri wrote:
>
>> /data/Sites/3218AD Eland's Bay/Bobbejaansberg/BB15/
>>
>> For path above to be valid in *nix, I need to escape 'spaces', 'single
>> quote' with 'backslash' to get output below
>>
>> /data/Sites/3218AD\ Eland\'s\ Bay/Bobbejaansberg/BB15/
>
> Well, spaces and quotes are allowed in Unix (and Linux) filenames, so
> I'm not sure what you are doing here.
>
>>
>> I've been trying to play around with the 'replace' function and
>> 'disable-output-escaping', but can't seem to make it work --see
>> snippet below.
>>
>>  <xsl:value-of select="replace(replace(replace(replace(replace($filename,
>> ' ', '\ '), '\&#40;', '\&#40;'), '&#41;', '\&#41;'), '&#91;',
>> '\&#91;'), '&#93;', '\&#93;')" disable-output-escaping="yes" />
>
> This is crazy.
>
> replace($filename, "[ '\\`&;]", "\\&")
> will probably do what you want.
>
> Or use \s instead of space if there might be newlines.
>
> You might also need to replace " with \"
>
> I'm guessing you are passing the filename to a program, since you for
> sure don't want to put backslashes in a path.
>
> Also watch for filenames including things
> like ../../../../../../etc/passwd :-)
>
> Liam
>
> --
> Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
> Pictures from old books: http://fromoldbooks.org/
> Ankh: irc.sorcery.net irc.gnome.org freenode/#xml


Current Thread
Keywords
xml