从节点XSLT中删除所有\ n \ r \ n字符?

时间:2021-03-17 17:08:19

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

想知道你能帮我吗?我在xml中有节点,如下所示

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

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

我想知道是否有任何方法让所有文本继续到一行,以便它可以传递到一个JavaScript函数?所以它会变成这样

$LOG: 08880xbpnd $fhdsafidsfsddfsdfsdfsdfsd

2 个解决方案

#1


13  

Unfortunately, the normalize-space() function (used in the answer of andynormancx) does more than deleting newlines.

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

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

它删除所有前导和尾随空格,并用一个空格字符替换任何一组内部可拥有的空格。

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).

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

The correct and safe way to do so is by using the standard XPath translate() function:

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

translate(., '
', '')

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

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

Here is an example:

这是一个例子:

<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>

When the above transformation is applied on this source XML document:

在此源XML文档上应用上述转换时:

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

The result is on one line only, as required, and all embedded spaces are left intact:

结果仅在一行上,根据需要,所有嵌入的空格保持不变:

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

#2


7  

The normalize-space() XPath function should do what you want.

normalize-space()XPath函数应该做你想要的。

#1


13  

Unfortunately, the normalize-space() function (used in the answer of andynormancx) does more than deleting newlines.

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

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

它删除所有前导和尾随空格,并用一个空格字符替换任何一组内部可拥有的空格。

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).

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

The correct and safe way to do so is by using the standard XPath translate() function:

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

translate(., '&#xA;', '')

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

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

Here is an example:

这是一个例子:

<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>

When the above transformation is applied on this source XML document:

在此源XML文档上应用上述转换时:

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

The result is on one line only, as required, and all embedded spaces are left intact:

结果仅在一行上,根据需要,所有嵌入的空格保持不变:

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

#2


7  

The normalize-space() XPath function should do what you want.

normalize-space()XPath函数应该做你想要的。