【问题标题】:How can I replace text with angle bracket without parsing the replace value?如何在不解析替换值的情况下用尖括号替换文本?
【发布时间】:2015-02-20 16:57:31
【问题描述】:

我有这个:

replace("Both cruciate ligaments are well visualized and are intact.", 
        ".", 
        ".<br>")

但我不想输出转义的尖括号,而是输出实际的括号。当我运行我得到的代码时:

Both cruciate ligaments are well visualized and are intact.<br>

我想要:

Both cruciate ligaments are well visualized and are intact.<br>

我怎样才能做到这一点?由于出现错误,我无法直接使用尖括号作为替换值。


编辑

我有一个样式表,它接收一个插入到 HTML 文件中的文本文件(来自样式表)。我获取一个 XML(临床文档)和一个文本文件,并将它们与样式表合并在一起。所以例如我有:

放射学报告
姓名:约翰,美国能源部
出生日期:1982-02-25

注入的文本在这里

文本必须在回车时换行,并且必须在单词级别换行。我确实设法做到了后者,但我没有找到换行的方法。我想在文件中找到“LF”替换为
以便在呈现页面后我可以看到换行符。

【问题讨论】:

  • 根据您的实现,无论显示此文本的内容都可能取消尖括号的转义。
  • 您想用&lt;br/&gt; 元素替换文字点. 吗?还是什么角色?我想知道您的带有replace 调用的第二个参数的样本是否不需要将点转义为'\.'

标签: xslt xquery


【解决方案1】:

如果你想输出节点而不是简单的字符串,你需要使用xsl:analyze-string。这是一个例子:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

<xsl:template match="text">
  <xsl:analyze-string select="." regex="\.">
    <xsl:matching-substring>
      <xsl:value-of select="."/><br/>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

输入是

<text>Both cruciate ligaments are well visualized and are intact.</text>

转换结果为

Both cruciate ligaments are well visualized and are intact.<br>

【讨论】:

    【解决方案2】:

    Martin Honnen 的回答是一个非常好的方法。

    使用简单的模板来查找有问题的文本是另一种方式:

    <xsl:variable name="magic-string"
      select='"Both cruciate ligaments are well visualized and are intact."'/>
    ...
    <xsl:template match="text()
      [contains(.,$magic-string)]">
      <xsl:value-of select="substring-before(.,$magic-string)"/>
      <xsl:value-of select="$magic-string"/>
      <br/>      
      <xsl:value-of select="substring-after(.,$magic-string)"/>
    </xsl:template>
    

    在任何一种情况下,使用 HTML 输出方法将空的 br 元素序列化为 &lt;br&gt; 而不是 &lt;br/&gt;

    注意:我假设您在此特定句子后需要一个 br,而不是在每次出现句号后都需要一个,这是 Martin Honnen 似乎解释问题的方式。

    【讨论】:

    • 真聪明!是的,我需要在文本文件中进行搜索和替换。
    猜你喜欢
    • 2015-07-30
    • 2020-05-21
    • 2013-08-14
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多