xsl-list
[Top] [All Lists]

Re: Selecting only one element using templates

2003-12-01 05:59:37
Hi Michael,

Thanks for your reply. I'm sorry if my question is a bit vague--like I said,
I'm new to XSL/T, so I'm not sure how to explain some of the problems I'm
having in technical terms yet. Community patience is appreciated. :-)

Basically my XML data structure allows elements to contain data or other
elements, like so:

<fruits>
    <fruit>apple</fruit>
    <fruit>banana</fruit>
    <fruit>
        <fruit>pear</fruit>
        <color>green</fruit>
    </fruit>
    <fruit>
        <fruit>orange</fruit>
        <color>orange</color>
    </fruit>
</fruits>

Here a fruit element can contain the name of the fruit or the elements for
fruit and fruit color.

I just wanted to select the fruit name using templates, but if I did this:


<xsl:templates match="fruit">

I get all fruit nodes(term?) and also the colors as well. So my output was:

apple
banana

pear
green

orange
orange

------

I posted the solution in my previous reply to this thread topic. My main
question, repeated, is why do all the nodes get output if I haven't
specified a template? I'm not sure if that question makes sense to advanced
users. I suppose I'm used to procedural programming where nothing gets
output unless I specify what needs to be output. In XSL, it seems that
although I think I'm being specific in my template matching, it will return
the child elements as well, which, right now, is only starting to make sense
to me but I'm still a little fuzzy about it.

Regards,

/johnny :)







On 2/12/03 1:37 AM, "Michael Kay" <mhk(_at_)mhk(_dot_)me(_dot_)uk> wrote:

Sorry, don't understand the question. You say you "just want to select
the <name> node". But there are five nodes called <name> in your
example. What output are you trying to produce?

Michael Kay

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
JCS
Sent: 01 December 2003 09:00
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Selecting only one element using templates


Hello,

I'm new to XSL/T and I've been reading the list archives, the
tutorials available in and around the net, etc., but I can't
seem to find an answer to a simple question (which is usually
the case with simple "abstract"
questions) so I hope nobody minds if I ask here.

I've got an XML structure as so:

<name></name>
<name></name>
<name>
 <name></name>
  <list>
  <item></item>
  <list>
</name>
<name></name>

Now, I've been able to get at what I want using the examples
provided by this list, so I'm not having difficulty with the
more intermediate steps, however, I'm having difficulty
getting my head this:

What if I just want to select the <name> node? If I use
<xsl:apply-templates/> and select "name" I get all name
elements, plus the item element which I don't want. I just
want a list of names from the name elements.

Do I have to specify a template for each element in an XML
document? That seems rather tedious, especially if I have a
lot of elements within <name> elements used as containers.

Any help is appreciated,

TiA,

/johnny :)

-- 
"I'm not normally a praying man, but if you're up there
please save me Superman!"

Homer Simpson


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list




-- 
"A smooth sea never made a skillful sailor."


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list