Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

javascript - Removing all characters from a node XSLT?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...