如何在不解析替换值的情况下用尖括号替换文本?

时间:2015-02-20 16:57:31

标签: xslt xquery

我有这个:

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(临床文档)和文本文件,并将它们与样式表合并在一起。例如,我有:

  

放射学报告
  姓名:JOHN,DOE
  DoB:1982-02-25
  
  注入的文字在这里

文本必须在回车时换行,并且必须在单词级别换行。我确实设法做了后者,但我找不到换行的方法。我想找到&#39; LF&#39;在文件中替换为&lt; BR&gt;这样一旦页面呈现,我就会看到换行符。

2 个答案:

答案 0 :(得分:2)

如果要输出节点而不是简单的字符串,则需要使用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>

答案 1 :(得分:1)

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元素序列化为<br>而不是<br/>

注意:我假设你在这个特定的句子之后想要br,而不是在每次完全停止后你想要一个,这就是Martin Honnen似乎解释这个问题的方式。