Whitespace generated by XSL printed as funny chara

2019-07-09 23:58发布

问题:

I have created XSLT over XML which looks bea-u-ti-ful in text editor or browser, but when I send it to the company's dino dot-matrix printer it prints as something like big T with stressed a, so something like "Ta".

I can simply replace it in the static parts, but I have a couple of templates:

<xsl:template name="lpad"><!-- recursive template to right justify and prepend-->
<!-- the value with whatever padChar is passed in   -->
<xsl:param name="padChar"> </xsl:param>
<xsl:param name="padVar"/>
<xsl:param name="length"/>
<xsl:choose>
  <xsl:when test="string-length($padVar) &lt; $length">
    <xsl:call-template name="lpad">
      <xsl:with-param name="padChar" select="$padChar"/>
      <xsl:with-param name="padVar" select="concat($padChar,$padVar)"/>
      <xsl:with-param name="length" select="$length"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="substring($padVar,string-length($padVar) - $length + 1)"/>
  </xsl:otherwise>
</xsl:choose>

So when I try calling them like this I get into trouble:

<xsl:call-template name="rpad">
<xsl:with-param name="padChar"> </xsl:with-param>
<xsl:with-param name="padVar" select="SequenceValue"/><!--  <xsl:with-param name="length" select="9"/>

Previously the padChar parameter was

&#160;

and the engine was able to process the template.

Any ideas are most appreciated!

Cheers!

回答1:

&#160; is a non-breaking space and pretty standard nowadays, but dot matrix printers are old technology and might well not support modern character sets. If you can identify the characters that your printer doesn't handle, then you could try using XSLT 2.0's xsl:character-map declaration to substitute them with something else; alternatively you could remap the characters using some kind of postprocessing (e.g. in sed or awk).



回答2:

I've spent almost a full day trying to crack that and the solution was too simple. What finally worked was to call the template in slightly different way:

<xsl:call-template name="rpad">
<xsl:with-param name="padChar" select="' '" />
<xsl:with-param name="padVar" select="SequenceValue"/>

I hope this helps someone.

Cheers!