2012-01-13 157 views
1

我的xsl變量中有一個很長的網址。 例如。 @url =「http://stackoverflow.com/questions/ask/2434/35454」基於xsl/xslt中的特定索引獲取子字符串

我需要一個基於第三個索引「/」的子字符串。即我只想顯示 http://stackoverflow.com

xsl中有一個子字符串(字符串,開始,長度)函數,但我如何找到長度部分。我找不到任何indexof功能。

<xsl:value-of select="substring(url,1,length)"/> 

我的網址是假設 - 「http://stackoverflow.com/questions/ask/2434/35454」 輸出我要的是http://stackoverflow.com

請提出了一些解決方案。

回答

1

使用

concat(substring-before(.,'//'), 
      '//', 
      substring-before(substring-after(., '//'), 
          '/' 
          ) 
     ) 

完整的代碼示例

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/*"> 
    <xsl:value-of select= 
    "concat(substring-before(.,'//'), 
      '//', 
      substring-before(substring-after(., '//'), 
          '/' 
          ) 
     ) 
    "/> 
</xsl:template> 
</xsl:stylesheet> 

當這一轉型是這個XML文檔施加:

<t>http://stackoverflow.com/questions/ask/2434/35454</t> 

的希望,正確的結果產生

http://stackoverflow.com 
+0

所以,如果我的URL存儲在$網址我必須這樣使用。 concat(substring-before(url,'//'),'//',substring-before(substring-after(url,'//'),'/') – 2012-01-13 05:49:09

+0

@AbhiRoczz ...是的, t忘記變量名稱前面的'$'字符 – 2012-01-13 05:56:50

+0

謝謝,我會標記你的答案正確..一旦我測試它.. :) – 2012-01-13 06:05:19