首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XSLT1.0和如何在文本中找到一些关键字

XSLT1.0和如何在文本中找到一些关键字
EN

Stack Overflow用户
提问于 2013-02-13 19:11:16
回答 1查看 641关注 0票数 2

我想使用XSLT1.0在文本中找到一些keyWords

  • contentText :海洋天气预报、警告、概要和冰情。数百个陆地和浮标站的观测跨越和海洋天气预报,警告,概要和冰条件。数百个陆地和浮标站的观测。
  • KeyWords:“海洋天气,海洋天气,海洋天气”
  • 划界者:

下面的代码只是一个关键字works...but,我想找到多个KeyWords (“海洋天气,海洋天气,海洋天气”)

代码语言:javascript
运行
复制
<xsl:choose>
  '<xsl:when test="contains($contentText,$keyWordLower)">
    <xsl:value-of select="substring-before($contentText,$keyWordLower)" disable-output-escaping="yes"/>
    <span class="texthighlight">
      <xsl:value-of select="$keyWordLower" disable-output-escaping="yes"/>
    </span>
    <!--Recursive call to create the string after keyword-->
    <xsl:call-template name="ReplaceSections">
      <xsl:with-param name="contentText" select="substring-after($contentText,$keyWordLower)"/>
      <xsl:with-param name="keyWordLower" select="$keyWordLower"/>
    </xsl:call-template>
  </xsl:when> <xsl:otherwise>
    <xsl:value-of select="$contentText"/>
  </xsl:otherwise>
</xsl:choose>
EN

回答 1

Stack Overflow用户

发布于 2013-02-14 04:18:17

这个转换

代码语言:javascript
运行
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pKeyWords">
  <kw>Marine weather</kw>
  <kw>marine weather</kw>
  <kw>Marine Weather</kw>
 </xsl:param>

 <xsl:variable name="vKeyWords" select=
 "document('')/*/xsl:param[@name='pKeyWords']/*"/>

 <xsl:template match="/*">
  <t><xsl:apply-templates/></t>
 </xsl:template>

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

  <xsl:if test="not($vKeyWords[contains($pText,.)])">
   <xsl:value-of select="$pText"/>
  </xsl:if>

  <xsl:apply-templates select="$vKeyWords[contains($pText,.)]">
   <xsl:sort select="string-length(substring-before($pText,.))"
    data-type="number"/>
   <xsl:with-param name="pText" select="$pText"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="kw">
   <xsl:param name="pText"/>
   <xsl:if test="position()=1">
    <xsl:value-of select="substring-before($pText, .)"/>
    <span class="texthighlight">
     <xsl:value-of select="."/>
    </span>
    <xsl:call-template name="highlightKWs">
     <xsl:with-param name="pText" select="substring-after($pText, .)"/>
    </xsl:call-template>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>

应用于以下XML文档时:

代码语言:javascript
运行
复制
<t>Marine weather forecasts,
warnings, synopsis, and ice conditions.
Hundreds of land and buoy station observations
across and marine weather forecasts, warnings,
synopsis, and ice conditions. Hundreds of land
and buoy station observations across.</t>

生成想要的、正确的结果:

代码语言:javascript
运行
复制
<t>
   <span class="texthighlight">Marine weather</span> forecasts,
warnings, synopsis, and ice conditions.
Hundreds of land and buoy station observations
across and <span class="texthighlight">marine weather</span> forecasts, warnings,
synopsis, and ice conditions. Hundreds of land
and buoy station observations across.</t>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14861148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档