Question

I want my string to be displayed as it is in pdf which I am generating from XML using XSLT XPATH. My XML contains strings with some white spaces in them but problem is xslt automatically trims those strings. I have checked it on this W3SCHOOL link Try it yourself. Whenever I add some extra space it just automatically gets trimmed which I really don't need in my case. Any help will be appreciated.

--------updates------------

I have tried to replace all spaces in string with   and tried to display it but xslt is not taking   as space. It is considering it just like part of string. I have also tried to use recursion to replace a special character in string with space (I have replaced spaces with a character in xml, now my xml contains that character where I need spaces) but it says named template is not available on recursive call.

<xsl:template
 name="add-spaces">
<!-- COMMENTS is string containing ~ where I need spaces -->
    <xsl:param
         name="text"
     select="COMMENTS" />
      <xsl:if
        test="$text != ''">
         <xsl:variable
           name="letter"
             select="substring($text, 1, 1)" />
         <xsl:when
        test="$letter = '~'">
       <fo:inline>
       <xsl:text>&#160;</xsl:text>
       </fo:inline>
     </xsl:when>
     <xsl:otherwise>
        <fo:inline>
         <xsl:value-of
          select="$letter" />
        </fo:inline>
      </xsl:otherwise>
      <xsl:call-template
       name="add-spaces">
               <xsl:with-param
        name="text"
        select="substring-after($text, $letter)" />
       </xsl:call-template>
</xsl:if>
</xsl:template>
Was it helpful?

Solution 2

To resolve this issue, I just replaced all spaces with a special character say ~ in XML strings and then in xslt I used above template with just little modification (using if instead of when, which I don't know why when didn't work) and I was done. Now in pdf all my strings contains white spaces where I need them. May be below code is an amateur one but for time being it has worked for me.

 <xsl:template name="add-spaces">
    <xsl:param name="text" select="COMMENTS" />
    <xsl:if test="$text != ''">
        <xsl:variable name="letter" select="substring($text, 1, 1)" />
        <xsl:if test="$letter = '~'">
            <xsl:text>&#160;</xsl:text>
        </xsl:if>
        <xsl:if test="$letter != '~'">
            <fo:inline>
                <xsl:value-of select="$letter" />
            </fo:inline>
        </xsl:if>
        <xsl:call-template name="add-spaces">
            <xsl:with-param name="text"
                select="substring-after($text, $letter)" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>
    <!-- Above is template and its call where I displayed strings is-->
<xsl:call-template
name="add-spaces">
<xsl:with-param
  name="text"
  select="COMMENTS" />
</xsl:call-template>

OTHER TIPS

You're generating HTML. It's a problem of HTML that normal spaces are trimmed. You would use &nbsp; for spaces which remain. However, you can't use &nbsp; in your XML without defining it as an entity, but you can use &#160; instead:

<artist>Bob Dylan&#160;</artist>

Or you can define the entitiy in your XML before the root tag:

<!DOCTYPE cds[
<!ENTITY nbsp "&#160;">
]>

and then use

<artist>Bob Dylan&nbsp;</artist>

However, for me it seems that the table looks ugly if you don't have spaces at the beginning and at the end. However, the solution to that is not to add spaces, the solution is to specify paddings and margins in your transformation, e.g.

<td style="padding:0 10 0 10"><xsl:value-of select="catalog/cd/title"/></td>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top