Re: [xsl] Selecting only one element using templates

Subject: Re: [xsl] Selecting only one element using templates
From: JCS <subscriber@xxxxxxxxxxxxx>
Date: Tue, 02 Dec 2003 03:24:28 +1300
On 2/12/03 2:35 AM, "Richard Lewis" <richard.lewis@xxxxxxxxx> wrote:

>> 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>
>> 
> This structure is slightly confusing; you have two 'fruit' nodes which mean
> different things.
> 
> Consider the following:
> 
> <fruit>
>  <name>apple</name>
> </fruit>
> <fruit>
>  <name>pear</name>
>  <color>green</color>
> </fruit>
> 
> or:
> 
> <fruit>apple</fruit>
> <fruit color="green">pear</fruit>
> 
> which are probably a bit clearer.

Hi Richard,

Although I agree that your version is "clearer", because I'm using an
extensible language I'm taking advantage of being able to duplicate element
names within containers, as sometimes the elements can stand on their own
and sometimes they may contain a random amount of elements. It's the same as
assigning attributes but maintaining flexibility IMO. The nodes themselves
don¹t actually mean different things, they mean the same thing... In my
example, the node "fruit" contains the name. Rather than invent a new
element container like "category" to contain fruits that have color data,
why not contain them in a fruit element?

<category name="fruit">
<fruit>apple</fruit>
</category>

Now I've introduced an abstract element into my data structure just to
contain an element. On top of that, it's redundant. I already know that
fruit is the category because the element name is fruit.

> If you have an <xsl:apply-templates /> element in a <xsl:template match="...">
> element then the child nodes will be processed as well.
> 

Yeah, that's the problem I was having, until I saw that I could apply only
the template I wanted using the "select" attribute in the apply-templates.

<xsl:apply-templates select="fruit">

Won't return the color child.

> If you want elements to be left out you can define empty templates for them:
> 
> <xsl:template match="color" />

I thought about this at first, but what if I have fifty elements and some of
them I'm not sure what the names would be? This was the question I was
really asking in the first post I made in this thread. Another suggestion
was to use the catch all <xsl:template match = "*"/> which seems like a good
idea. However, using the "select" attribute isolates the matches, so a
catch-all isn't necessary I've found.

Thanks again for your kind response to my question,

Regards,

/johnny :)



-- 
INTIMACY = In-to-me-I-see 


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


Current Thread