I wrote a little XSLT where I added normalize-space()
function to strip unnecessary spaces:
http://xsltransform.net/bnnZWM
<xsl:template match="page/pageFunctionResult/*/text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
The XSLT itself works, except that some spaces are not normalized:
<category> TEST </category>
I don't understand why normalize-space()
can't remove these spaces.
As noted in comments, the characters are really NON-BREAKING SPACE characters (#160). To handle them as regular spaces, use:
<xsl:value-of select="normalize-space(translate(., ' ', ' '))"/>
The normalize-space() function strips whitespace:
[3] S ::= (#x20 | #x9 | #xD | #xA)+
The characters surrounding TEXT
in your linked example are not one of these characters (as @har07 points out in the comments). Per @michael.hor257k's clever use of string-to-codepoints()
,
<xsl:template match="page/pageFunctionResult[1]/category[1]">
<xsl:value-of select="string-to-codepoints(substring(., 1, 1))"/>
</xsl:template>
we can see that they are NO-BREAK SPACE characters (#xA0), aka
.
To remove
, you'll need something more than normalize-space()
....
See @michael.hor257k's answer. (+1)
If you want to cover
along with other types of whitespace characters, use replace()
with a category escape ahead of normalize-space()
:
<xsl:value-of select="normalize-space(replace(., '\p{Z}', ' '))"/>
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