简体   繁体   English

Xml中的标记特定字符串

[英]markup specific strings in Xml

I like to markup some strings in an xml document. 我喜欢在xml文档中标记一些字符串。

For example, I have: 例如,我有:

<p> I like to go to Florida </p>

I need to tag the string "go" and have the output as: 我需要标记字符串“go”并将输出作为:

<p> I like to <something>go</something> to Florida</p>

What is the best way to do this? 做这个的最好方式是什么? I am using Java. 我正在使用Java。 I need to treat the XML file as XML not as text. 我需要将XML文件视为XML而不是文本。 I found some solutions that treat an xml file as a text file and use string.replace but I do not think those are good solutions. 我找到了一些将xml文件视为文本文件并使用string.replace的解决方案,但我不认为这些是很好的解决方案。

Any suggestion is much appreciated. 任何建议都非常感谢。

Thank you, 谢谢,

Try an XSLT 2.0 transformation like this: 尝试像这样的XSLT 2.0转换:

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

<xsl:template match="text()">
  <xsl:analyze-string regex="go">
    <xsl:matching-substring>
      <something><xsl:value-of select="."/></something>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

You can of course extend the regular expression, eg regex="go|come|walk|run"; 你当然可以扩展正则表达式,例如regex =“go | come | walk | run”; if you only want to match whole words, you might want to use tokenize() to split it into words and process each word separately. 如果您只想匹配整个单词,您可能需要使用tokenize()将其拆分为单词并分别处理每个单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM