XSLT1.0中判断字符串中是否包含另一个单词
时间:2007-10-16 来源:shiwudao
有XML文件如下
<?xml version="1.0" encoding="UTF-8"?>
<string> word myword wordmyword </string> 现在需要用XSLT1.0来判断字符串中是否有单词'word',如果用contains函数的话则无法区分是否其他单词中包含'word'子串,而1.0又没有2.0的ends-with()以及matches()函数的支持。 写XLST如下可以判断 ?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="str" select="concat(concat(' ', normalize-space(/string/text()[1])), ' ')"></xsl:variable>
<xsl:if test="contains($str, ' word ')">
<xsl:text>yes</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet> 首先normalize-space去掉首尾空格,tab之类,并把中间的分隔变为一个空格。 然后在头尾合拼上一个空格,确保可以用' word '进行查找。
<string> word myword wordmyword </string> 现在需要用XSLT1.0来判断字符串中是否有单词'word',如果用contains函数的话则无法区分是否其他单词中包含'word'子串,而1.0又没有2.0的ends-with()以及matches()函数的支持。 写XLST如下可以判断 ?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="str" select="concat(concat(' ', normalize-space(/string/text()[1])), ' ')"></xsl:variable>
<xsl:if test="contains($str, ' word ')">
<xsl:text>yes</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet> 首先normalize-space去掉首尾空格,tab之类,并把中间的分隔变为一个空格。 然后在头尾合拼上一个空格,确保可以用' word '进行查找。
相关阅读 更多 +