问题描述
想知道你能帮我吗?我在xml中有节点,如下所示
wondered if you could help me please? I have node in xml that is as followed
我想知道是不是要将所有文本转到一行,以便它可以传递给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
推荐答案
很遗憾,函数(在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(., '
', '')
返回从当前节点的字符串值中获取的字符串,其中删除任何换行符。
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(.,'
','')"/>
</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中删除所有\ n \\ n \\ n \\ r \\ n字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!