我制作了一个模板,用于在特定字符后插入不可见的whitspace。如果第二个子字符串是“。”,则此模板插入和空白。或“A”。我现在要做的是,它在每个大写字母之前是不可思议的空格,意味着我必须像正则表达式一样用[A-Z]替换“A”。我尝试过一些东西,但没有任何效果。我如何在xsl中使用正则表达式?
<xsl:template name="intersperse-with-zero-spaces">
<xsl:param name="str" />
<xsl:variable name="spacechars">
	

      
     ​
</xsl:variable>
<xsl:if test="string-length($str) > 0">
<xsl:variable name="c1" select="substring($str, 1, 1)" />
<xsl:variable name="c2" select="substring($str, 2, 1)" />
<xsl:value-of select="$c1" />
<xsl:if test="$c1 = '.'">
<xsl:if test="$c2 != '' and not(contains($spacechars, $c1) or
contains($spacechars, $c2))">
<xsl:text>​</xsl:text>
</xsl:if>
</xsl:if>
<xsl:if test="$c1 != ''">
<xsl:if test="$c2 = 'A' and
not(contains($spacechars, $c1) or
contains($spacechars, $c2))">
<xsl:text>​</xsl:text>
</xsl:if>
</xsl:if>
<xsl:call-template name="intersperse-with-zero-spaces">
<xsl:with-param name="str" select="substring($str, 2)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="intersperse-with-zero-spaces">
<xsl:param name="str" />
<xsl:variable name="spacechars">
	

      
     ​
</xsl:variable>
<xsl:if test="string-length($str) > 0">
<xsl:variable name="c1" select="substring($str, 1, 1)" />
<xsl:variable name="c2" select="substring($str, 2, 1)" />
<xsl:value-of select="$c1" />
<xsl:if test="$c1 = '.'">
<xsl:if test="$c2 != '' and not(contains($spacechars, $c1) or
contains($spacechars, $c2))">
<xsl:text>​</xsl:text>
</xsl:if>
</xsl:if>
<xsl:if test="$c1 != ''">
<xsl:if test="$c2 = 'A' and
not(contains($spacechars, $c1) or
contains($spacechars, $c2))">
<xsl:text>​</xsl:text>
</xsl:if>
</xsl:if>
<xsl:call-template name="intersperse-with-zero-spaces">
<xsl:with-param name="str" select="substring($str, 2)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
答案 0 :(得分:1)
这取决于您使用的是哪个XSLT引擎。例如,如果使用Micrsoft xslt引擎运行,则可以在xslt转换中包含自定义c#代码。
<msxsl:script implements-prefix='yourprefix' language='CSharp>
public string book(string abc, string xyz)
{ if ((abc== abc)&&(abc== xyz)) return bar+xyz;
else return null;
}
</msxsl:script>
http://msdn.microsoft.com/en-us/library/533texsx(v=vs.110).aspx
答案 1 :(得分:0)
XSLT 1.0处理器不支持正则表达式匹配或内置字符串替换。某些XSLT 1.0处理器支持扩展,如http://www.exslt.org/regexp/functions/replace/index.html。如果您使用Xalan,则可以检查http://xml.apache.org/xalan-j/extensionslib.html支持哪些功能。
在Java世界中,我建议转移到XSLT 2.0,例如Saxon 9作为XSLT处理器,然后你可以利用函数matches
的XPath和XSLT 2.0支持, tokenize
,replace
和xsl:analyze-string
指示。