最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing all nr characters from a node XSLT? - Stack Overflow

programmeradmin5浏览0评论

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

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

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

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
Share Improve this question edited Feb 16, 2009 at 10:31 andynormancx 13.8k6 gold badges38 silver badges52 bronze badges asked Feb 16, 2009 at 9:41 Matthew DeloughryMatthew Deloughry 3022 gold badges7 silver badges26 bronze badges 1
  • Unfortunately, the normalize-space() function (used in the answer of andynormancx) does more than deleting newlines. It deletes all leading and trailing whitespace and it replaces any group of inner contigious whitespace with a single space character. A better solution is to use translate() – Dimitre Novatchev Commented Feb 16, 2009 at 14:37
Add a comment  | 

2 Answers 2

Reset to default 15

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

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

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

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:

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

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

发布评论

评论列表(0)

  1. 暂无评论