如果元素包含字母,如何编写 xslt? [英] How to write xslt if element contains letters?

查看:23
本文介绍了如果元素包含字母,如何编写 xslt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sth 应该是全字母或正则表达式 [A-Z].如何将 xslt 与正则表达式结合使用?

Sth should be all letters or in regular expressions [A-Z]. How to combine xslt with regular expressions?

 <xsl:if test="string-contains(//ns0:elem/value, 'sth')">


        </xsl:if>

推荐答案

XPath/XSLT 1.0 不支持正则表达式,但可以使用基本字符串函数进行简单验证.

XPath/XSLT 1.0 does not support regular expressions, but simple validation can be performed using the basic string functions.

白名单

XPath 1.0 translate 函数可以用来模拟白名单:

The XPath 1.0 translate function can be used to simulate a whitelist:

<xsl:variable name="alpha" 
              select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:if test="string-length(translate(., $alpha, '')) &gt; 0">
    <!-- context node contains non-alpha characters -->
</xsl:if>

测试使用 translate 首先删除所有大写和小写字母.如果结果字符串的长度不为零,则原始字符串必须包含其他字符.

The test uses translate to first remove all upper- and lower-case letters. If the resulting string's length is non-zero, then the original string must have contained additional characters.

请注意,上面的表达式可以简化为:

Note that the expression above could be simplified to:

<xsl:if test="translate(., $alpha, '')">

... 因为任何非空字符串的计算结果为真.

... because any non-empty string evaluates to true.

黑名单

使用double-translate方法将$alpha当成黑名单:

Use the double-translate method to treat $alpha as a blacklist:

<xsl:if test="translate(., translate(., $alpha, ''), '')">
    <!-- context-node contains characters on the blacklist (in $alpha) -->
</xsl:if>

内部 translate 返回一个去除了所有字母字符的字符串,然后将其用作第二个 translate 调用的模板,从而生成一个仅包含字母字符.如果这个字符串不为零,那么我们在黑名单上找到了一个字符.这是一个经典的方法.例如,请参阅上一个关于 SO 的问题:

The inner translate returns a string with all its alpha characters removed, which is then used as the template to the second translate call, resulting in a string containing only the alpha characters. If this string is non-zero, then we found a character on the blacklist. This is a classic approach. See, for example, this previous question on SO:

黑名单测试也可以这样执行:

A blacklist test could also be performed like this:

not(string-length(translate(., $alpha, ''))=string-length())

如果删除所有黑名单字符后的字符串长度不等于原始字符串的长度,则该字符串必须包含黑名单中的字符.

If the length of the string after removing all of the blacklisted characters is not equal to the length of the original string, then the string must have contained a character on the blacklist.

总结

黑名单和白名单实际上是同一枚硬币的两个方面.下面一起演示它们的用法:

Blacklists and whitelists are really two sides of the same coin. The following demonstrates their usage together:

<xsl:if test="translate(., $alpha, '')">
    [contains some characters not on the list]
</xsl:if> 
<xsl:if test="not(translate(., $alpha, ''))">
    [contains only characters on the list]
</xsl:if> 
<xsl:if test="translate(., translate(., $alpha, ''), '')">
    [contains some characters on the list]
</xsl:if>
<xsl:if test="not(translate(., translate(., $alpha, ''), ''))">
    [contains only characters not on the list]
</xsl:if>

这篇关于如果元素包含字母,如何编写 xslt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆