Luke:
At 05:56 PM 9/7/2006, you wrote:
> (But are you sure you want the value of the first name element child
> of the name element? That seems rather odd.)
I'm not sure I understand what your mean by that. The confusion may be
in that I used "fake" code rather then the real one.
That might account for it, but then again maybe not, unless your
latest examples are also fake.
> ...we have <xsl:text>...</xsl:text>:
>
> <xsl:template match="name">
> <xsl:value-of select="name" />
> <xsl:text> : </xsl:text>
> <xsl:apply-templates />
> </xsl:template>
>
I had at one time had <xsl:text>:</xsl:text>, however it was still getting
moved to the front.
That might have been happening (indeed I don't doubt it was
happening) for an entirely different reason.
It wasn't until I did
<xsl:template match="name">
<xsl:apply-templates>
<xsl:value-of select="name/>
<xsl:text> : </xsl:text>
</xsl:templeate>
I don't understand why switching the apply-templates above the rest of the
code worked. By my logical thinking, that doesn't make sense. :-/
Actually that was the only thing that did anything.
My guess is that you actually have no "name" element child of a
"name" element. In other words, you have nowhere in your XML
something like this:
<name>
<name>Bob</name>
</name>
You probably have, more simply, <name>Bob</name> or possibly
something like <name><first>Bob</first></name>.
Your value-of instruction selects the nodes returned by the XPath
location path "name", which is short for "child::name". (I am
deliberately using technical jargon here both to expose you to it,
and to suggest to you that there is actually a logic here, which XSLT
programmers learn to use if they are not always going to be
programming in the dark.)
"child::name" returns the "name" element children of the current
node. Since your template has matched "name" elements, your current
node is perforce a name; this value-of evidently expects it will have
element children (contained elements) named "name". This is what I'm
doubting. And if you don't, your instruction says to write their
value (more precisely: write the value of the first one, since
value-of in XSLT 1.0 can handle only one node at a time), but they
aren't there, so nothing gets written.
In contrast, the instruction <xsl:apply-templates/> is short for
<xsl:apply-templates select="child::node()"/>, which is to say "apply
templates to all child nodes of the current node".
<xsl:apply-templates/> is one of the most common instructions in XSLT
and in fact is what we like to see beginners using, since applying
templates (as I suggested to you a day or two ago :-) is how the
language works. Usually (as in this case) it's just what you want,
since it selects whatever child elements (and other nodes, including
"text nodes", which are where the document's text content is mainly
stored), and finds templates to process them.
When no such templates are given in the stylesheet, an XSLT engine
provides built-in templates to make sure that no text content is
actually dropped. This is why your stylesheet "starts working" when
you use the instruction. Your text has <name>Bob</name>, you match on
"name", xsl:apply-templates selects the text node containing the
string "Bob", which is then matched by the built-in template for text
nodes, which copies "Bob" to the result, so that's what you see.
To summarize (try it and see):
INPUT
<name>Bob</name>
XSLT A:
<xsl:template match="name">
<xsl:text>name: </xsl:text>
<xsl:apply-templates/>
</xsl:template>
RESULT A:
name: Bob
XSLT B:
<xsl:template match="name">
<xsl:text>name: </xsl:text>
<xsl:value-of select="name"/>
</xsl:template>
RESULT B:
name:
See the thing is, people who know XSLT are able to say not only that
this will happen, but *why* it happens.
(Mark Twain on asking if he believed in Infant Baptism: "Not only do
I believe it: I've seen it!")
At the moment everything seems to be working great. Thanks again.
You're welcome. I recommend studying up on the XSLT processing model
in order to understand all this. In the meantime you can expect us to
be tossing around terms like "text node" and "match pattern" and
"context", and not know what we mean or even that we mean something
very particular with these terms. :-)
Cheers,
Wendell