XPATH 2.0 には、文字列内の部分文字列を別の文字列に置き換えることができる関数があります。xalan を使用してこれを行いたいと思います。残念ながら、これは EXSLT メソッド str:replace をサポートしておらず、XSLT 1.0 スタイルシートしか使用していません。exslt.org の関数をインクルードしてもうまくいかないようです。関数スタイルを使用しようとすると、str:replace が見つからないというエラーが表示されます。テンプレート スタイルを使用しようとすると、サポートされているにもかかわらず、ノード セットが見つからないというエラーが表示されます。translate は単なる文字の入れ替えであるため、役に立ちません。何か案は?
4196 次
1 に答える
3
xslt 2.0 replace を模倣できる独自の関数を作成できます。
<xsl:template name="replace">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="replace">
<xsl:with-param name="text"
select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
このように呼び出す場合:
<xsl:variable name="replacedString">
<xsl:call-template name="replace">
<xsl:with-param name="text" select="'This'" />
<xsl:with-param name="replace" select="'This'" />
<xsl:with-param name="by" select="'That'" />
</xsl:call-template>
結果の $replacedString の値は「That」になります
于 2011-10-06T16:20:22.727 に答える