Re: Newbie Question

Discussion in 'XML' started by Joseph J. Kesselman, Jul 1, 2008.

  1. rhino wrote:
    > <p><xsl:value-of select="firstname"/><xsl:value-of select="lastname"/></p>
    > So, how do I get a blank space to appear between firstname and lastname?

    The most straightforward solution is:

    <p><xsl:value-of select="firstname"/><xsl:text> </xsl:text><xsl:value-of

    This is necessary because whitespace in stylesheets is normally
    discarded unless it's next to non-whitespace text. Wrapping the literal
    whitespace in <xsl:text> tells the processor "I really want this to be
    output exactly as it appears here."

    There are other ways to do it, of course -- anything that generates a
    whitespace value will accomplish the task -- but this is the one that
    will make the stylesheet easiest for a human to read.

    FWIW, we've seen a number of cases where the tutorials at w3schools have
    been ... well, let's be charitable and say "incomplete". They may have
    improved since then, but you might want to consider looking at other
    sources as well or instead. My standard recommendation is the
    DeveloperWorks XML site,, but I admit to being
    Joseph J. Kesselman, Jul 1, 2008
    1. Advertisements

  2. rhino wrote:
    > I also tried putting multiple
    > spaces within the <xsl:text> </xsl:text> block and was surprised that only
    > one space was displayed no matter how many I put there.

    That's not XSLT behavior, but browser behavior. Remember, once you
    generate the HTML it's processed like any other HTML document. and
    browsers are free to readjust whitespace as they see fit.

    In general, XML tools consider to be just another way of writing
    the space character, so it's slightly surprising when you say that
    change produced different results on screen. I strongly suspect other
    XSLT processors would handle it differently, so I wouldn't recommend
    relying on this trick.

    The usual HTML solution when you need a specific number of spaces and
    aren't willing to go all the way to <pre> markup is to use the
    non-breaking-space character. XSLT doesn't normally know the entity name
    &nbsp;, but you can specify that character using the numeric character
    reference  .
    Joseph J. Kesselman, Jul 1, 2008
    1. Advertisements

  3. rhino wrote:
    > A clearer error message that actually said something like "&nbsp; is not
    > permitted in XSLT documents"

    Actually, the issue is at a slightly different level: The &nbsp; entity
    name is not defined by default in XML, and XSLT is an XML language. If
    you want this available, it has to be defined in the Document Type
    Declaration (DTD) for the specific document -- the stylesheet in this
    case -- either in the DTD file or in the Internal Subset.

    (And yes, I do specifically mean DTD. XML Schemas chose not to support
    the declaration of entities, for a number of reasons largely having to
    do with wanting to push folks toward purer XML syntax.)

    This is one reason I hesitate to point folks toward "simplified"
    tutorials... all too often, what gets left out turns out to be important
    when the first real-world task comes along.
    Joseph J. Kesselman, Jul 1, 2008
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry C.
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Dave Angel
    Apr 7, 2013
  4. rusi
  5. Miki Tebeka
    Miki Tebeka
    Apr 7, 2013

Share This Page