How to remove extra spaces of element values ​​in xslt

I want to highlight spaces in element values.

Source:

   <sss>
      HI:
      HELLO:
      HOW:
    </sss>

      

Output:

    <sss>HI:HELLO:HOW

      

I tried this

    <xsl:strip-space elements="*"/>

      

but this does not affect the output.

+3


source to share


2 answers


If you want to strip a character or more, use a translation listing the character (s) as the second argument and an empty string as the third translate(., ' ', '')

eg.



<xsl:template match="sss">
  <xsl:copy>
    <xsl:value-of select="translate(normalize-space(), ' ', '')"/>
  </xsl:copy>
</xsl:template>

      

+4


source


To get the result with all spaces removed, use the standard XPath function translate()

translate(., ' &#xA;&#xD;&#xC;&#9;', '')

      

No need to use normalize-space()

.

The pattern required to close this gap is simply :



<xsl:template match="sss/text()">
    <xsl:value-of select="translate(., ' &#xA;&#xD;&#9;', '')"/>
</xsl:template>

      

The full transformation also includes an identity rule, so if the XML document contains no elements sss

, they will be copied "as is":

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="sss/text()">
     <xsl:value-of select="translate(., ' &#xA;&#xD;&#xC;&#9;', '')"/>
 </xsl:template>
</xsl:stylesheet>

      

0


source







All Articles