If you need to join the resulting strings just use .... well ... string-join ...
declare function local:topath($path){ let $pathseg := tokenize($path, "/") let $pathsequence := fold-left($pathseg, (), function($out, $segment){ if($segment = "." or $segment = "") then $out else if($segment = "..") then $out[position() lt count($out)] else ($out, $segment) }) return string-join($pathsequence, "/") };
local:topath("/a/b/c/../../../g")
On 01/04/19 22:02, Andreas Mixich wrote:
Marco Lettere wrote onm 01.04.2019 at 18:01:
declare function local:topath($path){ let $pathseg := tokenize($path, "/") return fold-left($pathseg, (), function($out, $segment){ if($segment = "." or $segment = "") then $out else if($segment = "..") then $out[position() lt count($out)] else ($out, $segment) })}; local:topath("/a/b/c/../../../g")
Beautiful and very close, except for a minor caveat: the "/" are needed, to reconstruct the absolute path-part from the relative. I played around and tried to place some "/" to your function, but all variants placed some "/" wrong or twice.
Last but not least: You asked for the use case, which is described on https://tools.ietf.org/html/rfc3986#section-5.2.4 (note also the sequences shown after the description of the steps)
5.2.4 <https://tools.ietf.org/html/rfc3986#section-5.2.4>. Remove Dot Segments The pseudocode also refers to a "remove_dot_segments" routine for interpreting and removing the special "." and ".." complete path segments from a referenced path. This is done after the path is extracted from a reference, whether or not the path was relative, in order to remove any invalid or extraneous dot-segments prior to forming the target URI. Although there are many ways to accomplish this removal process, we describe a simple method using two string buffers. 1. The input buffer is initialized with the now-appended path components and the output buffer is initialized to the empty string. 2. While the input buffer is not empty, loop as follows: A. If the input buffer begins with a prefix of "../" or "./", then remove that prefix from the input buffer; otherwise, B. if the input buffer begins with a prefix of "/./" or "/.", where "." is a complete path segment, then replace that prefix with "/" in the input buffer; otherwise, C. if the input buffer begins with a prefix of "/../" or "/..", where ".." is a complete path segment, then replace that prefix with "/" in the input buffer and remove the last segment and its preceding "/" (if any) from the output buffer; otherwise, D. if the input buffer consists only of "." or "..", then remove that from the input buffer; otherwise, E. move the first path segment in the input buffer to the end of the output buffer, including the initial "/" character (if any) and any subsequent characters up to, but not including, the next "/" character or the end of the input buffer. 3. Finally, the output buffer is returned as the result of remove_dot_segments. Note that dot-segments are intended for use in URI references to express an identifier relative to the hierarchy of names in the base URI. The remove_dot_segments algorithm respects that hierarchy by removing extra dot-segments rather than treat them as an error or leaving them to be misinterpreted by dereference implementations. The following illustrates how the above steps are applied for two examples of merged paths, showing the state of the two buffers after each step. STEP OUTPUT BUFFER INPUT BUFFER 1 : /a/b/c/./../../g 2E: /a /b/c/./../../g 2E: /a/b /c/./../../g 2E: /a/b/c /./../../g 2B: /a/b/c /../../g 2C: /a/b /../g 2C: /a /g 2E: /a/g STEP OUTPUT BUFFER INPUT BUFFER 1 <https://tools.ietf.org/html/rfc3986#section-1> : mid/content=5/../6 2E: mid /content=5/../6 2E: mid/content=5 /../6 2C: mid /6 2E: mid/6