2
votes

I have an XSLT transform that puts   into my output. That is a narrow-non breaking space. Here is one section that results in nnbsp:

<span>
    <xsl:text>§ </xsl:text>
    <xsl:value-of select="$firstsection"/>
    <xsl:text> to </xsl:text>
    <xsl:value-of select="$lastsection"/>
</span>        

The nnbsp in this case, comes in after the § and after the text to.

<span>§&#x202f;1 to&#x202f;8</span>

(interestingly, the space before the to turns out to be a regular full size space)

This occurs in my UTF-8 encoded output, as well as iso-8859-1 (latin1).

How can I avoid the nnbsp? While the narrow space is visually more appropriate, it doesn't work for all the devices that will read this document. I need a plain vanilla blank space.

Is there a transform setting? I use Saxon 9 at the command line.

Should I do a another transform.. using a replace template to replace the nnbsp?

Should I re-do my templates like the one above? Example, if I did a concat() would that be a better coding practice?

UPDATE: For those who may find this question someday... as suggested by Michael Kay, I researched the issue further. Indeed, it turns out narrow-NBSP were in the source XML files (and bled into my templates via cut/paste). I did not know this, and it was hard to discover (hat tip to gVim hex view). The narrows don't exactly jump out at you in a GUI editor. I have no control over production of the source XML, so I had to find a way to 'deal with it.' Eric's answer below turned out to be my preferred way to scrub the narrow-nbsp. SED editing was (and is) an another option to consider, but I like keeping my production in XSLT when possible. So Eric's suggestion has worked well for me.

2
I don't see why you should get that character in there if it is not in the stylesheet text you output or in the variables you output.Martin Honnen
I agree Martin.. but it is happening.Paulb

2 Answers

2
votes

You could use the translate() function to replace your nnbsp by something else, but since you are using Saxon 9 you can rely on XSLT 2.0 features and use a character map which will do that kind of things automatically for you, for instance (assuming that you want to replace them by a non breaking space:

<xsl:output use-character-maps="nnbsp"/>
<xsl:character-map name="nnbsp">
   <xsl:output-character character="&#x202f;" string="&#xa0;"/>   
</xsl:character-map>

Eric

1
votes

The narrow non-breaking space is coming from somewhere: either the source document or the stylesheet. It's not being magically injected by the XSLT processor. If it's in the stylesheet, then get rid of it. If it's in the source document, then transform it away, for example by use of the translate() function.

In fact, pasting your code fragment into a text editor and looking at it in hex, I see that the 202F characters are right there in your code. I don't know how you got them into your stylesheet, but you should (a) remove them, and (b) work out how it happened so it doesn't happen again.