Removing all \n\r characters from a node XSLT?

前端 未结 2 1713
鱼传尺愫
鱼传尺愫 2020-12-10 20:51

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

$LOG: 08880xbpnd $
fhdsafidsfsd
df
sd
fsd
f
相关标签:
2条回答
  • 2020-12-10 21:35

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

    0 讨论(0)
  • 2020-12-10 21:38

    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>
    
    0 讨论(0)
提交回复
热议问题