2012-08-13 82 views
0

我試圖找到一個單詞並將其替換爲一個鏈接。例如:搜索並插入鏈接

<root> 
<description> 
Uniquely cultivate optimal supply chains before multidisciplinary infrastructures. 
</description> 
</root> 

在xsl中,我試圖搜索一個特定的單詞並用鏈接替換該單詞。例如,我要替換「培養」與cultivate字使頁面上的最終輸出將是:

Uniquely <a href="http://google.com">cultivate</a> optimal supply chains before multidisciplinary infrastructures. 

我快到使用替代像函數這個問題:

<xsl:variable name="description" select="description" /> 

,然後

replace($description, "(.*)(cultivate.*)", "$1test$2") 

這只是在培養之前添加單詞測試。我真的很感激任何幫助。

--UPDATE-- 我是能夠解決本次發行採用替換這樣

<xsl:variable name="description" select="description" /> 
Description: <xsl:value-of disable-output-escaping="yes" select="replace($description, '(.*)(cultivate)(.*)', '$1&lt;a href=http://google.com &gt;cultivate&lt;/a&gt;$3')" /> 

回答

1

使用analyze-stringhttp://www.w3.org/TR/xslt20/#regex-examples例如

<xsl:template match="description"> 
    <xsl:analyze-string select="." regex="cultivate"> 
    <xsl:matching-substring> 
     <a href="http://example.com"> 
     <xsl:value-of select="."/> 
     </a> 
    </xsl:matching-substring> 
    <xsl:non-matching-substring> 
    <xsl:value-of select="."/> 
    </xsl:non-matching-substring> 
</xsl:analyze-string> 
</xsl:template> 
+0

非常感謝。它完美的作品。現在,如果我想在同一個句子中添加一個鏈接,例如添加鏈接到單詞「供應」? – Vinit 2012-08-13 18:27:22

+1

那麼你在正則表達式模式中加入了一個替代方法,例如' ...'。或者你可以在'non-matching-substring'部分放入​​另一個'analyze-string'來進一步處理其他模式的非匹配輸入。 – 2012-08-14 10:15:22

+0

非常感謝你! – Vinit 2012-08-14 16:58:31