我想按需生成正則表達式模式,但我失敗了。也許有人知道爲什麼,可以幫助。XSL:如何根據需要生成正則表達式模式?
我想什麼實現是定義文本是(如實施例)中標記的輸出粗體具有元件
源XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<strong>Empire</strong>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
</cd>
<cd>
<strong>your</strong>
<strong>heart</strong>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
</cd>
</catalog>
XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:call-template name="addBold">
<xsl:with-param name="text" select="title" />
<xsl:with-param name="replace"><xsl:variable name="temp"><xsl:for-each select="strong">|<xsl:value-of select="." /></xsl:for-each></xsl:variable>(<xsl:value-of select='substring-after($temp,"|")' />)</xsl:with-param>
</xsl:call-template>
</td>
<td>
<xsl:value-of select="artist" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="addBold">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:analyze-string select="$text" regex="$replace">
<xsl:matching-substring>
<b><xsl:value-of select="regex-group(1)" /></b>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
然後$replace
參數將包含例如。 (your|heart)
。但它在xsl:analyze-string
元素中從未匹配。
如果我更換$replace
用硬編碼「(your|heart)
」它總是能正常工作..
有我錯過了一個重要的事情嗎?像我不能使用變量/參數作爲模式?還是我需要確保它的格式正確?我在呼叫模板段落中做了這些。
非常感謝...... 我已經想通了一樣,但隨後得到了另一個奇怪的錯誤 但之後我再次在一個新的「安裝」上完成了所有的事情,正如你所建議的,額外的{和}一切都很順利...... – user1779882