user3079114
user3079114

Reputation: 69

How to add space between two variables?

I have a problem to add space between the firstName and lastName fields. I'm trying to display the first name and last name on one line, but there is no space between them. How can I add a space? These fields are from my database on SQL Server. Thank you.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
    <style type="text/css">
        .tutor
        {
            color: #000000;
            font-family: "Arial";
            font-size: large;
        }        
        .data
        {
            font-family: "Arial";
            font-size: large;
        }        
        .heading
        {
            font-family: "Arial";
            font-size: xx-large;
            font-weight: bold;
            color: #ABCDEF;
        } 

    <title>Tutor Contact List</title>
    </style>
</head>
<body>
    <div class="heading">Tutor Contact List</div><br/>
    <xsl:for-each select="list/tutorcontact">
        <div class="tutor">Tutor Name:  
            <xsl:value-of select="lastname"/>
            <xsl:value-of select="firstname"/></div>
        <div class="data"><b>Phone:</b>  
            <xsl:value-of select="phone"/></div>
        <div class="data"><b>E-Mail:</b>  
            <xsl:value-of select="email"/></div><br />
    </xsl:for-each>
</body>
</html>
</xsl:template>


</xsl:stylesheet>

Upvotes: 0

Views: 2249

Answers (4)

Ian Roberts
Ian Roberts

Reputation: 122364

<div class="tutor">Tutor Name:  
    <xsl:value-of select="lastname"/>
    <xsl:value-of select="firstname"/></div>

In an XSLT stylesheet text nodes that consist only of whitespace are ignored by default except

  • inside <xsl:text> or
  • inside an element that has xml:space="preserve"

So the newline and spaces between Tutor Name: and the lastname will be preserved (because while that text node contains whitespace, it doesn't contain only whitespace) but the ones between the two value-of elements will be ignored. If you want to have white space in the output at a place where it would be ignored in the stylesheet then you need to make it explicit with

<div class="tutor">Tutor Name:  
    <xsl:value-of select="lastname"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="firstname"/></div>

but personally I'd just use the approach suggested by OJay and just use one value-of with concat(lastname, ' ', firstname).

Upvotes: 1

Razor
Razor

Reputation: 9835

Use the Unicode non-breaking space : &#160; or &#32;

Upvotes: 0

OJay
OJay

Reputation: 4921

Try the XPATH concat function

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
    <style type="text/css">
        .tutor
        {
            color: #000000;
            font-family: "Arial";
            font-size: large;
        }        
        .data
        {
            font-family: "Arial";
            font-size: large;
        }        
        .heading
        {
            font-family: "Arial";
            font-size: xx-large;
            font-weight: bold;
            color: #ABCDEF;
        } 

    <title>Tutor Contact List</title>
    </style>
</head>
<body>
    <div class="heading">Tutor Contact List</div><br/>
    <xsl:for-each select="list/tutorcontact">
        <div class="tutor">Tutor Name:  
            <xsl:value-of select="concat(firstname,' ',lastname)"/>
        </div>
        <div class="data"><b>Phone:</b>  
            <xsl:value-of select="phone"/></div>
        <div class="data"><b>E-Mail:</b>  
            <xsl:value-of select="email"/></div><br />
    </xsl:for-each>
</body>
</html>
</xsl:template>


</xsl:stylesheet>

Upvotes: 2

bcr
bcr

Reputation: 2073

How about:

<div class="tutor">Tutor Name:  
        <xsl:value-of select="lastname"/>&nbsp;
        <xsl:value-of select="firstname"/></div>

Upvotes: 0

Related Questions