Connecting Tech Pros Worldwide Forums | Help | Site Map

Newbie Question

rhino
Guest
 
Posts: n/a
#1: Jul 1 '08
I am beginning to learn XML and XSLT from the w3schools tutorial and I'm
having a lot more trouble doing something simple than I should be, namely
writing a single blank space into a document. I am trying to separate two
strings with a space and nothing I'm doing works.


Here is my XSLT, which I think you'll agree is really simple::
<?xml version="1.0" encoding="Windows-1252"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="attendance">

<html>

<body>

<xsl:for-each select="meeting">

<h2><xsl:value-of select="date/year"/>-<xsl:value-of
select="date/month"/>-<xsl:value-of select="date/day"/></h2>

<xsl:for-each select="attendee">

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

</xsl:for-each>

<p>Total Attendees: <xsl:value-of select="count(attendee)"/></p>

</xsl:for-each>

</body>

</html>

</xsl:template>

</xsl:stylesheet>


It works perfectly except that I can't figure out how on earth to put a
single blank between the firstname and lastname within the for-each loop for
attendee. I've tried putting a single blank and multiple blanks into the
document via the space bar but, as normal for HTML, the white space gets
squeezed out. I tried using &nbsp; and then the document wouldn't appear at
all. In fact, I got a message that there was no document at all! I
eventually stumbled on this page,
http://www.w3schools.com/xml/xml_syntax.asp, which implies but doesn't say
explicity, that ONLY the five entitites stated there are permissible.
Presumably, that is why the document wasn't perceived when I used &nbsp; in
the XSLT.

So, how do I get a blank space to appear between firstname and lastname? I
have to believe this is possible but I have no idea how it could be done.

Can someone help me out? I'd be quite happy if someone could point me to a
topic in a tutorial that might show me how that could be done.

--
Rhino



Joseph J. Kesselman
Guest
 
Posts: n/a
#2: Jul 1 '08

re: Newbie Question


rhino wrote:
Quote:
<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
select="lastname"/></p>

This is necessary because whitespace in stylesheets is normally
discarded unless it's next to non-whitespace text. Wrapping the literal
whitespace in <xsl:texttells 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, http://www.ibm.com/xml, but I admit to being
biased.
rhino
Guest
 
Posts: n/a
#3: Jul 1 '08

re: Newbie Question



"Joseph J. Kesselman" <keshlam-nospam@comcast.netwrote in message
news:486a8599$1@kcnews01...
Quote:
rhino wrote:
Quote:
><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
select="lastname"/></p>
>
This is necessary because whitespace in stylesheets is normally discarded
unless it's next to non-whitespace text. Wrapping the literal whitespace
in <xsl:texttells 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, http://www.ibm.com/xml, but I admit to being
biased.
Thank you, Joseph! I was just going to post back to say that I'd figured out
my problem via a Google search; I used the following and it worked fine:
<li><xsl:value-of select="firstname"/> <xsl:value-of
select="lastname"/></li>

I tried your way and it worked fine, too. I also tried putting multiple
spaces within the <xsl:text</xsl:textblock and was surprised that only
one space was displayed no matter how many I put there. If I ever need to
put multiple spaces somewhere, I may do it with character entitites; it
would be easier (or at least use less space) to do five times than to
do <xsl:text</xsl:textfive times! But there's probably a better way than
that which I just haven't learned yet ;-)

I agree that the w3schools tutorials are not the best. I've used them with
some success in the past simply to get a few basics under my belt, then
moved on to better tutorials, references, or books as I got the basics
sorted out. The w3schools tutorials are pretty superficial and really only
give you a few basic concepts and tutorials and I don't find them
particularly thorough either.

I appreciate your suggestion about a better source of information. I'll have
a look at the IBM XML site in a minute.

Thanks for your help and your time!

--

Rhino


Joseph J. Kesselman
Guest
 
Posts: n/a
#4: Jul 1 '08

re: Newbie Question


rhino wrote:
Quote:
I also tried putting multiple
spaces within the <xsl:text</xsl:textblock 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 <premarkup 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  .

rhino
Guest
 
Posts: n/a
#5: Jul 1 '08

re: Newbie Question



"Joseph J. Kesselman" <keshlam-nospam@comcast.netwrote in message
news:486a8c19$1@kcnews01...
Quote:
rhino wrote:
Quote:
>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 <premarkup 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  .
>
I wish I'd known _that_ a few hours ago!

I was using &nbsp; in my XSLT and assumed it was perfectly safe because I'd
used it hundreds of times before in regular HTML. But I kept getting error
messages to the effect that there was no document. Naturally, I assumed that
the problem was in the new stuff I was trying, even though it seemed simple
enough. But nothing I tried in the xsl statements would fix this error
except removing the entire template or having one that was so simple that
there was no &nbsp; in it. Finally, I had moderately complicated XSL without
&nbsp; in it and the code didn't break. Then I added the &nbsp; and nothing
else and the code broke again. Then I looked up XML entities and saw the
implication that ONLY the five entities listed in the tutorial were allowed
and this seemed to confirm that &nbsp; was the culprit because it wasn't in
the list.

A clearer error message that actually said something like "&nbsp; is not
permitted in XSLT documents" would have been a LOT more useful than the
message that did appear. I would have saved several hours that I could have
used more productively.....

Oh well, live and learn ;-)

Thanks again for your help with this!

--
Rhino


Joseph J. Kesselman
Guest
 
Posts: n/a
#6: Jul 1 '08

re: Newbie Question


rhino wrote:
Quote:
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.
Closed Thread