SPFE Documentation | Collections > SPFE Function Reference > relative-from-absolute-path

Function: relative-from-absolute-path

relative-from-absolute-path(path as item()*, base-path as item()*, prefix as xs:string) as xs:string

Description

The relative-from-absolute-path function takes a full system path and returns the portion of that path relative to a base path. For example, given the path

C:/Users/Joe/spfe-open-toolkit/spfe-ot/1.0/scripts/common/utility-functions.xsl

and the base path

C:/Users/Joe/spfe-open-toolkit/spfe-ot

it returns the part of the path after the base path:

/1.0/scripts/common/utility-functions.xsl

Optionally, you can specify a prefix to be added to the returned path:

<xsl:value-of select="sf:relative-from-absolute-path(source-file, $config/config:spfeot-home,'$SPFEOT_HOME')"/>
                

In this example, source-file is an element containing the full path of a source file, $config/config:spfeot-home pulls the local path of the spfe open toolkit on the users machine from the config file, and '$SPFEOT_HOME' is a prefix string. Assuming that the value of source-file is file:/C:/Users/Joe/spfe-open-toolkit/spfe-ot/1.0/scripts/common/utility-functions.xsl, the return value will be:

$SPFEOT_HOME/1.0/scripts/common/utility-functions.xsl

The function does its best to normalize both the path and base-path parameters before comparing them. Any protocol part is removed from both strings (file:/ in the example above), directory separators are normalized to forward slashes, and URL decoding is applied (so that if spaces are represented by %20, they will be replaced by regular spaces. This allows you to compare paths in different formats and encodings wihtout having to normalize them first.

As the example shows, the principal motivation for this function is to assist in generating documentation from source code. If you write an extraction routine that captures the file name of the file it is extracting from, that name will contain the path of the local machine on which the extraction is run. You can use this function to remove the local portion of the file path and substitute a placeholder such as $SPFEOT_HOME in the example above.

Return value

Return type: xs:string

The function returns the portion of the path parameter after the end of the base-path parameter, prefixed with the value of the prefix parameter.

Source file

$SPFEOT_HOME/1.0/scripts/common/utility-functions.xsl

Parameters

path

Type: item()*

An absolute path from which the relative path is to be extracted.

base-path

Type: item()*

An absolute path which will be subtracted from the path parameter to form the relative path.

prefix

Type: xs:string

The prefix to be added to the start of the returned string.

Definition

            <xsl:function name="sf:relative-from-absolute-path" as="xs:string">
		               <xsl:param name="path"/>
		               <xsl:param name="base-path"/>
		               <xsl:value-of select="sf:relative-from-absolute-path($path, $base-path, '')"/>
	           </xsl:function>
         
            <xsl:function name="sf:relative-from-absolute-path" as="xs:string">
		               <xsl:param name="path" as="xs:string"/>
		               <xsl:param name="base-path" as="xs:string"/>
		               <xsl:param name="prefix" as="xs:string"/>

		               <xsl:variable name="normalized-path" select=" sf:path-after-protocol-part(translate(sf:pct-decode($path), '\', '/'))"/>
		               <xsl:variable name="normalized-base-path" select="sf:path-after-protocol-part(translate(sf:pct-decode($base-path), '\', '/'))"/>
		               <xsl:value-of select="concat($prefix,substring-after($normalized-path, $normalized-base-path))"/>
	           </xsl:function>