匹配确切的单词并使用xslt替换单词

里根

我正在基于xslt的转换中工作。我需要将一个元素中的单词替换为完全匹配的另一个元素。但目前它也部分匹配。

我的输入是

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<doc>
    <section>
        <key>SERVICE</key>
        <title>ABOUT SERVICES (OUR SERVICE) OF THE CONCERN(SERVICE)</title>
    </section>
    <section>
        <key>SERVICES</key>
        <title>ABOUT NEW SERVICES, TO DO CONCERN(SERVICE)</title>
    </section>
    <section>
        <key>LOOK</key>
        <title>IT LOOKING SO, BUT "LOOK" GOOD</title>
    </section>
</doc>

我需要在标题元素中找到关键字word,然后用<b>关键字</ b>替换。比赛应该是准确的。

但目前,它也替换了所有部分匹配,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <section>
        <key>SERVICE</key>
        ABOUT &lt;b&gt;SERVICE&lt;/b&gt;S (OUR &lt;b&gt;SERVICE&lt;/b&gt;) OF THE CONCERN(&lt;b&gt;SERVICE&lt;/b&gt;)
    </section>
    <section>
        <key>SERVICES</key>
        ABOUT NEW &lt;b&gt;SERVICES&lt;/b&gt;, TO DO CONCERN(SERVICE)
    </section>
    <section>
        <key>LOOK</key>
        IT &lt;b&gt;LOOK&lt;/b&gt;ING SO, BUT "&lt;b&gt;LOOK&lt;/b&gt;" GOOD
    </section>
</doc>

我的xslt是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="xml"/>
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="title">
    <xsl:variable name="k-word-text">
      <xsl:text>&lt;b&gt;</xsl:text>
      <xsl:value-of select="preceding-sibling::key"/>
      <xsl:text>&lt;/b&gt;</xsl:text>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="matches(., preceding-sibling::key)">
        <xsl:value-of select="replace(., preceding-sibling::key, $k-word-text)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

低于我的预期输出

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <section>
        <key>SERVICE</key>
        ABOUT SERVICES (OUR &lt;b&gt;SERVICE&lt;/b&gt;) OF THE CONCERN(&lt;b&gt;SERVICE&lt;/b&gt;)
    </section>
    <section>
        <key>SERVICES</key>
        ABOUT NEW &lt;b&gt;SERVICES&lt;/b&gt;, TO DO CONCERN(SERVICE)
    </section>
    <section>
        <key>LOOK</key>
        IT LOOKING SO, BUT "&lt;b&gt;LOOK&lt;/b&gt;" GOOD
    </section>
</doc>
C队

一种解决方案是创建一个变量来保存您要匹配的文本,但要包含正则表达式以确保该键不在字母之前或之后

<xsl:variable name="k-match-text">
  <xsl:text>(^|[^A-Za-z])</xsl:text>
  <xsl:value-of select="preceding-sibling::key"/>
  <xsl:text>($|[^A-Za-z])</xsl:text>
</xsl:variable>

请注意,这里实际上不需要xsl:choosewith contains,就好像文本不包含键一样,该replace语句将使文本保持不变。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="xml"/>
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="title">
    <xsl:variable name="k-word-text">
      <xsl:text>$1&lt;b&gt;</xsl:text>
      <xsl:value-of select="preceding-sibling::key"/>
      <xsl:text>&lt;/b&gt;$2</xsl:text>
    </xsl:variable>
    <xsl:variable name="k-match-text">
      <xsl:text>(^|[^A-Za-z])</xsl:text>
      <xsl:value-of select="preceding-sibling::key"/>
      <xsl:text>($|[^A-Za-z])</xsl:text>
    </xsl:variable>
    <xsl:value-of select="replace(., $k-match-text, $k-word-text)"/>
  </xsl:template>
</xsl:stylesheet>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

SQL Server:更新以匹配并仅替换确切的单词

Java-字符串替换确切的单词

在 2 个匹配模式后打印确切的单词

替换确切的单词,而不用str_replace替换不完全的单词

用表格中的单词列表仅替换确切的单词

ReGex匹配确切的单词,而忽略包含它作为子字符串的单词?

在列表中查找确切的单词

如何搜索确切的单词?

确切的单词冲突单词与破折号

在 xslt 中以逗号分隔的列表中查找确切的单词

用tr命令在shell中用另一个替换确切的单词

即使被下划线或其他符号包围,也替换确切的单词

如何用正则表达式python匹配确切的单词?

如何用正则表达式匹配确切的单词

正则表达式可以匹配确切的句子和单个单词吗?

在 SphinxQL 中按顺序查找确切的单词

如何从字符串中过滤确切的单词?

在Vim命令模式下搜索确切的单词

如何在大邮件中搜索确切的单词?

正则表达式查找确切的单词

Python:从网址中提取确切的单词

如何使用Excel筛选器“包含”在列中搜索确切的单词?

使用 python 从字符串中提取确切的单词

使用正则表达式或哈希图查找确切的单词

如何在Java中使用正则表达式找到确切的单词?

如何使用preg_match()或其他方式找到确切的数字或单词

如何在C#中使用Open XML从Word文档中查找确切的单词?

如何使用搜索模式文件在csv的最后一列中搜索确切的单词?

如何使用Python 3在列表中找到确切的单词序列?