Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XSLT, How do I display a single space between my two XML elements? &nsbp and spacing do not fix it

Tags:

xml

xhtml

xslt

I want to display a space between the first name and last name, such that the output displays

Name: Richard Simmons

But right now, I am getting

Name: RichardSimmons

For some reason, nothing I do can add a space into the display.

I can post the full code but I suspect the spacing has to do with how I call "apply-templates" in my XSLT.

Here is the XSLT that displays name

<xsl:template match="scout">
<div style="border-style:solid; width: 60%; margin: 0% 0% 20px 35%;">
  <!--NAME-->
  <p style="font-weight: bold">
    Name: <span style="font-weight: normal"><xsl:apply-templates select="firstName"/></span><!--I want to put a space here-->
          <span style="font-weight: normal"><xsl:apply-templates select="lastName"/></span>
  </p>
<!-- More stuff-->
</div>
</xsl:template>

<xsl:template match="firstName">
    <xsl:value-of select="node()"/> 
</xsl:template>

<xsl:template match="lastName">
    <xsl:value-of select="node()"/>
</xsl:template>

I have tried simply adding a space and including "&nbsp" with no success.

EDIT: Relevant XML if anyone's interested

<bsa>
  <council name="Movies">
    <troop name="Pixar" number="a113">
      <scout>
        <firstName>Philip</firstName>
        <lastName>Sherman</lastName>
        <address>
          <street>42 Wallaby Way</street>
          <city>Sydney</city>
          <state>New South Wales</state>
        </address>
        <phone>77437626</phone>
        <rank date-earned="2004">Eagle</rank>
        <meritbadge date-earned="2004">Fish in my hair!</meritbadge>
      </scout>
    </troop>
  </council>
  </bsa>
like image 929
Cameron Avatar asked Oct 31 '25 13:10

Cameron


1 Answers

To add a space between first name and last name, you can use <xsl:text> </xsl:text> -notice a space inside the element-, OR, concat() f.e concat(' ', lastName), OR, any other variant of the two that you see suitable.

<!-- example using xsl:text -->
<xsl:template match="lastName">
    <xsl:text> </xsl:text>
    <xsl:value-of select="."/>
</xsl:template>

<!-- example using concat() -->
<xsl:template match="lastName">
    <xsl:value-of select="concat(' ', .)"/>
</xsl:template>

Also, notice how I used . to reference current context element in the two examples shown above.

like image 134
har07 Avatar answered Nov 02 '25 12:11

har07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!