【问题标题】:Get the specific word in text in HTML page获取 HTML 页面中文本中的特定单词
【发布时间】:2011-09-30 12:11:04
【问题描述】:

如果我有以下 HTML 页面

<div>
 <p> 
  Hello world!
 </p>
 <p> <a href="example.com"> Hello and Hello again this is an example</a></p>
</div>

我想获取特定的词,例如“你好”,并将其更改为“欢迎”,无论它们在文档中的哪个位置

你有什么建议吗?无论您使用哪种类型的解析器,我都很乐意得到您的答案?

【问题讨论】:

  • 好问题,+1。有关两个完整的 XSLT(1.0 和 2.0)解决方案和简要说明,请参阅我的答案。
  • 有什么理由不能进行简单的文本搜索和替换吗?此外,HTML 是否也兼容 XML,例如XHTML?如果不是,那么任何 XML 解决方案都无法正常工作。
  • @Flynn1179:OP 提到了一些 htmlcleaner,这意味着他可以将 html 清理成格式良好的 XML。我们不应该仅仅因为我们不了解所有情况而排除使用最好的 XML 处理技术的可能性。

标签: xquery html-parsing jericho-html-parser htmlcleaner


【解决方案1】:

使用 XSLT 很容易做到这一点。

XSLT 1.0 解决方案

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/> 

 <xsl:param name="pTarget" select="'hello'"/>
 <xsl:param name="pReplacement" select="'welcome'"/>

 <xsl:variable name="vtargetLength" select=
 "string-length($pTarget)"/>

 <xsl:variable name="vUpper" select=
  "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
 <xsl:variable name="vLower" select=
  "'abcdefghijklmnopqrstuvwxyz'"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="text()" name="replace">
  <xsl:param name="pText" select="."/>

  <xsl:variable name="vLowerText" select=
  "translate($pText,$vUpper,$vLower)"/>

  <xsl:choose>
   <xsl:when test=
   "not(contains(concat(' ', $vLowerText, ' '),
                 concat(' ',$pTarget,' ')
                 )
        )">
    <xsl:value-of select="$pText"/>
   </xsl:when>

   <xsl:otherwise>
    <xsl:variable name="vOffset" select=
    "string-length(
          substring-before(concat(' ', $vLowerText, ' '),
                           concat(' ', $pTarget,' ')
                           )
                   )"/>
    <xsl:value-of select="substring($pText, 1, $vOffset)"/>
    <xsl:value-of select="$pReplacement"/>

    <xsl:call-template name="replace">
      <xsl:with-param name="pText" select=
      "substring($pText, $vOffset + $vtargetLength+1)"/>
    </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose> 
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时

<div>
 <p>
  Hello world!
 </p>
 <p> <a href="example.com"> Hello and Hello again this is an example</a></p>
</div>

产生想要的正确结果

<div>
   <p>
  welcome world!
 </p>
   <p>
      <a href="example.com"> welcome and welcome again this is an example</a>
   </p>
</div>

我的假设是匹配和替换不区分大小写(即“hello”和“heLlo”都应该替换为“welcome”)。如果需要区分大小写的匹配,则可以大大简化转换。

XSLT 2.0 解决方案

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pTarget" select="'hello'"/>
 <xsl:param name="pReplacement" select="'welcome'"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="text()[matches(.,$pTarget, 'i')]">
   <xsl:variable name="vEnlargedRep" select=
   "replace(concat(' ',.,' '),
            concat(' ',$pTarget,' '),
            concat(' ',$pReplacement,' '),
             'i')"/>
    <xsl:variable name="vLen" select="string-length($vEnlargedRep)"/>

    <xsl:sequence select=
     "substring($vEnlargedRep,2, $vLen -2)"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档(如上所示)时,再次生成所需的正确结果

<div>
   <p>
  welcome world!
 </p>
   <p> 
      <a href="example.com"> welcome and welcome again this is an example</a>
   </p>
</div>

说明:使用标准 XPath 2.0 函数 matches()replace() 指定为第三个参数 "i" --不区分大小写操作的标志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多