我。你可以在 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" 字词。
二。 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 文档(上图)时,会产生相同的正确结果:
<s>He has high blood pressure.</s>
<s>He has high Blood Pressure.</s>
<s>He has high Blood
Pressure.</s>
解释:
转换为小写。
使用 double-translate 方法将任何非字母字符替换为空格。
然后使用normalize-space() 将任意一组相邻的空格替换为单个空格。
然后用空格包围这个结果。
最后,验证当前结果是否包含字符串" blood pressure "。