2017-12-21 13 views
1

特定のファイルのルートフォルダへのパスを抽出するにはどうすればよいですか?XSLT絶対パスからの相対パス

私が表現

path/to/one/of/my/file.xml 

を持っている私は、XSLT/XPathを使用して

../../../../../ 

を取得する必要がありますか?

+0

スラッシュの数は不明であると私はXSLT 1.0を使用しています –

+0

パスが 'path/to/one/of/my/file.xml'の場合は' path'が必要ですか? –

+0

あなたが望む相対的なパスは '../../../../'ではないと思います.. ../../../../../ ' - あなたか私はoff-by-oneエラーです。 –

答えて

0

fn:tokenizeあなたは再帰的なテンプレートを使用する必要があり、XPath 2.0の機能であるため、次

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" /> 

    <xsl:template match="/"> 
    <!-- initial call with path to tokenize --> 
    <xsl:call-template name="tokenize"> 
     <xsl:with-param name="str" select="'path/to/one/of/my/file.xml'" /> 
    </xsl:call-template>  
    </xsl:template> 

    <!-- recursive named template -->  
    <xsl:template name="tokenize"> 
    <xsl:param name="str" /> 
    <xsl:param name="result" select="''" /> 
    <xsl:choose> 
     <xsl:when test="substring-after($str,'/')"> 
     <xsl:call-template name="tokenize"> 
      <xsl:with-param name="str" select="substring-after($str,'/')" /> 
      <xsl:with-param name="result" select="concat($result,'../')" /> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$result" /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 

出力:

../../../../../ 
関連する問題