一,你可以做这样的事情在XSLT 2.0:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="s">
<xsl:variable name="vWords" select=
"tokenize(lower-case(string(.)),
'[\s.?!,;—:\-]+'
) [.]
"/>
<xsl:sequence select=
" for $current in .,
$i in 1 to count($vWords)
return
if($vWords[$i] eq 'blood'
and
$vWords[$i+1] eq 'pressure'
)
then .
else()
"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
当这个XSLT 2.0变换应用到下面的XML文档(在这个问题没有提供这样的文件! ):
<t>
<s>He has high blood pressure.</s>
<s>He has high Blood Pressure.</s>
<s>He has high Blood
Pressure.</s>
<s>He was coldblood Pressured.</s>
</t>
有用,正确的结果(只包含` “血液” 和 “压力”(不区分大小写的元素和作为两个相邻字)产生:
<s>He has high blood pressure.</s>
<s>He has high Blood Pressure.</s>
<s>He has high Blood
Pressure.</s>
说明:
使用tokenize()
功能分裂上的NN-字母字符的字符串,用旗为不区分大小写和多在线模式。
通过tokenize()
结果迭代找到一个"blood"
字由"pressure"
字紧随其后。
II。一个XSLT 1.0溶液:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vUpper" select=
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="vLower" select=
"'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vSpaaaceeees" select=
"' '
"/>
<xsl:variable name="vAlpha" select="concat($vLower, $vUpper)"/>
<xsl:template match="s">
<xsl:variable name="vallLower" select="translate(., $vUpper, $vLower)"/>
<xsl:copy-of select=
"self::*
[contains
(concat
(' ',
normalize-space
(translate($vallLower, translate($vallLower, $vAlpha, ''), $vSpaaaceeees)),
' '
),
' blood pressure '
)
]
"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
当该变换是在相同的XML文档(上文)施加相同correst结果产生:
<s>He has high blood pressure.</s>
<s>He has high Blood Pressure.</s>
<s>He has high Blood
Pressure.</s>
说明:
转换为小写。
使用双翻译方法将任何非alpha字符替换为空格。
然后使用normalize-space()
用一个空格替换任何一组相邻空格。
然后用空格围住这个结果。
最后,验证当前结果是否包含字符串" blood pressure "
。
伟大的回应Dimitre,谢谢。通过我的代码后,我实际上产生了正确的结果。我使用的表单发布数据,我认为是造成这个问题。再次感谢 – rossjha 2012-03-13 10:01:21