从节点 XSLT 中删除所有 字符? [英] Removing all characters from a node XSLT?

查看:24
本文介绍了从节点 XSLT 中删除所有 字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道你能不能帮我一下?我在xml中有如下节点

wondered if you could help me please? I have node in xml that is as followed

$LOG: 08880xbpnd $
fhdsafidsfsd
df
sd
fsd
f
sd
fsd
$LOG: 08880xbpnd $
fhdsafidsfsd
df
sd
fsd
f
sd
fsd

我想知道有没有办法让所有的文本都放在一行上,这样它就可以传递给一个 javascript 函数?所以它会变成这样

I was wondering is there anyway to make all the text go on to one line so that it then can be passsed through to a javascript function? so it would turn out like this

$LOG: 08880xbpnd $fhdsafidsfsddfsdfsdfsdfsd
$LOG: 08880xbpnd $fhdsafidsfsddfsdfsdfsdfsd

推荐答案

不幸的是,normalize-space() 函数(在 andynormancx 的回答中使用)不仅仅是删除换行符.

它删除所有前导和尾随空格,并用单个空格字符替换任何一组内部连续的空格.

It deletes all leading and trailing whitespace and it replaces any group of inner contigious whitespace with a single space character.

在许多情况下,我们只想删除一种类型的空白字符(如在当前情况下——换行(CR+LF 在 XML 解析器读取时自动规范化为仅 LF).

In many cases we want to deleteonly one type of a white-space character (as in the current case -- new lines (CR+LF is automatically normalized on reading by the XML parser to just LF).

正确且安全的方法是使用标准的 XPath translate() 函数:

translate(., '
', '')

返回从当前节点的字符串值获得的字符串,其中删除了任何换行符.

returns a string obtained from the string-value of the current node in which any newline character is deleted.

这是一个例子:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

  <xsl:template match="text()">
    <xsl:value-of 
     select="translate(.,'&#xA;','')"/>
  </xsl:template>
</xsl:stylesheet>

当对这个源 XML 文档应用上述转换时:

<t>
$LOG: 08880xbpnd $
"embedded    blanks    must    stay"
df
sd
fsd
f
sd
fsd
</t>

结果只在一行,按要求,所有嵌入的空格都保持不变:

<t>$LOG: 08880xbpnd $"embedded    blanks    must    stay"dfsdfsdfsdfsd</t>

这篇关于从节点 XSLT 中删除所有 字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆