Re: [xsl] Selecting only one element using templates

Subject: Re: [xsl] Selecting only one element using templates
From: constantin.vasiliu@xxxxxxxxxxxxxx
Date: Mon, 1 Dec 2003 16:58:49 -0800
Hi, /johnny CS,

I'm a newbie too, that's probably helping me understand what you're trying
to do. Looks like you did your homework before asking (I did the same
thing:). I got a kick start from this list that helped me "click". Hope
you'll get the same benefit:).

Here's my two cents, if something is wrong no flaming necessary, I was just
trying to help.

> 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.

The way I handled this very same problem in my code was by using
<xsl:apply-templates "name/name"/>. That way you'll select only the nodes
named "name" that are children of nodes named "name".


> I was able to find the solution to my problem:
> 
> <xsl:template match="class[class]">
>         <xsl:apply-templates select="class"/>
>     </xsl:template>
> 
>     <xsl:template match="class">
>         <xsl:element name="option">
>             <xsl:apply-templates/>
>         </xsl:element>
>     </xsl:template>

I guess that might work if the default axis for the predicate is the child
axis (which it should). Don't know enough about XSLT to tell you "yay" or
"nay".


> I did not know you could use a select attribute when applying 
> templates. But
> this leaves me with a question: If I left "select" out of the
> apply-templates xsl element, I would have thought it would 
> only apply the
> templates that I defined. Does the processor output all nodes 
> anyway? In
> other words, if I put in an "apply-templates" element, and I have no
> templates defined for a particular node (or set?), why do I 
> get output?
> 
> I think this is where my confusion lies, and isn't really explained in
> simple terms anywhere I have looked.

The "select" attribute is optional. When it is absent, all children of the
current node are selected. Or so my book says. The matching process
continued to the children where it might have matched some of your
templates, so it displayed them.


> 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.

If your XML document is pretty simple, be careful to draw conclusions and
generalize. You might match everything in your input tree just because it
goes only a couple of branches down from the root.


> > There is a built-in template rule for nodes that are not 
> matched by an
> > explicit template rule. This rule does a recursive 
> apply-templates for
> > element nodes, and for text nodes it outputs the content. 
> If you want to
> > override this, write a rule such as:
> > 
> > <xsl:template match="*"/>
> > 
> > Michael Kay

If using the <xsl:apply-templates/> element without the "select" attribute
all children of the current node are selected. Once one of the child nodes
is the current node, same thing happens. So that makes it recursive as
Michael says (or so it seems to me).

Regards,

Constantin Vasiliu

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


Current Thread