I have a sample xml file like this,
<doc>
<p>text1 text2 </p>
<p>text1 text2 </p>
<p>text1 text2 </p>
</doc>
this sample xml, first <p> has space whitespace character ( ), second <p> has tab whitespace whitespace character (	) and third <p> has space non-breaking whitespace character ( ).
I need to remove the any white spaces appearing just before closing tag.
So, expected output should be,
<doc>
<p>text1 text2</p>
<p>text1 text2</p>
<p>text1 text2</p>
</doc>
By using xslt normalize-space() I can remove unnecessary spaces and tab characters but not non-breaking whitespace characters.
<xsl:template match="p/text()">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
Any suggestions how can I normalize all white spaces including non-breaking white spaces in xslt?
You could do:
<xsl:value-of select="normalize-space(translate(., ' ', ' '))"/>
This will work in XSLT 1.0 and 2.0 alike.
In XSLT 2.0, you could also use regex - for example:
<xsl:value-of select="replace(., '[\t\p{Zs}]', '')"/>
will remove the horizontal tab character as well as any character in the Unicode Space_Separator category, which includes not only the space and non-breaking space characters but also other space characters. Documentation is hard to find, but I believe this is currently the complete list: (extracted from http://www.unicode.org/Public/UNIDATA/UnicodeData.txt):
  SPACE
  NO-BREAK SPACE
  OGHAM SPACE MARK
  EN QUAD
  EM QUAD
  EN SPACE
  EM SPACE
  THREE-PER-EM SPACE
  FOUR-PER-EM SPACE
  SIX-PER-EM SPACE
  FIGURE SPACE
  PUNCTUATION SPACE
  THIN SPACE
  HAIR SPACE
  NARROW NO-BREAK SPACE
  MEDIUM MATHEMATICAL SPACE
  IDEOGRAPHIC SPACE
𐲰 OLD HUNGARIAN CAPITAL LETTER EZS
𐳰 OLD HUNGARIAN SMALL LETTER EZS
𖼶 MIAO LETTER ZSHA
𖼼 MIAO LETTER ZSA
𖼾 MIAO LETTER ZZSA
𖽁 MIAO LETTER ZZSYA
However, testing with Saxon 9.5 shows that the last 6 characters are not recognized: http://xsltransform.net/ncntCSo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With