XSLT - 从文本中过滤单个单词

时间:2012-04-13 09:05:42

标签: string xslt

我的字符串如下所示,由XSLT解析

boy "happy family" filetype:pdf girl 

从上面的字符串中,我只需要过滤掉单词“boy”和“girl”并获取另一个字符串

boy girl

我如何实现这一目标?

3 个答案:

答案 0 :(得分:1)

使用XSLT 2.0,replace函数采用正则表达式,因此replace('boy "happy family" filetype:pdf girl', '"[^"]*"|\w+:\w+', '')应该有效。使用XSLT 1.0,我首先要检查您的XSLT 1.0处理器是否支持类似的扩展功能

答案 1 :(得分:0)

对于XSLT 1.0,http://exslt.org/regular-expressions名称空间中有替换(请参阅http://exslt.org/regexp/functions/replace/index.html),但Martin指出您的处理器需要支持扩展。

<xsl:value-of select="{http://exslt.org/regular-expressions}replace(STRING, '&quot;.*&quot;|\w+:\w+', '')"/>

将提供您所要求的内容。

答案 2 :(得分:0)

这是一个XSLT 1.0解决方案,它使用FXSL 提供的标记化(在XSLT 1.0中编写)和XSLT 1.0处理器提供的xxx:node-set()扩展函数使用:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
 exclude-result-prefixes="ext">
  <xsl:import href="strSplit-to-Words.xsl"/>
  <xsl:output indent="yes" omit-xml-declaration="yes"/>

   <xsl:strip-space elements="*"/>
   <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
      <xsl:variable name="vwordNodes">
        <xsl:call-template name="str-split-to-words">
          <xsl:with-param name="pStr" select="/"/>
          <xsl:with-param name="pDelimiters"
                          select="' '"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:for-each select=
       "ext:node-set($vwordNodes)/*
         [not(contains(., '&quot;') or contains(.,':'))
        and
          count(preceding-sibling::*[contains(., '&quot;')]) mod 2 = 0
         ]">

        <xsl:value-of select="concat(., ' ')"/>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

将此转换应用于以下XML文档(提供的文本,包含在单个顶部元素中):

<t>boy &quot;happy family&quot; filetype:pdf girl</t>

产生了想要的正确结果

boy girl 

在以下情况下产生相同的正确输出,更棘手的XML文档

<t>boy &quot; very happy family &quot; filetype:pdf girl</t>