This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: : RE: suppressing default template rules
- From: Aseef Jamaluddin <j_aseef at yahoo dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Thu, 28 Mar 2002 21:17:28 -0800 (PST)
- Subject: Re: : RE: [xsl] suppressing default template rules
- Reply-to: xsl-list at lists dot mulberrytech dot com
That was very good explanation. I am still working on
the fundementals. Just to check the oder in which the
nodes are processed i made the following changes to my
xml,xsl documents.
<?xml version="1.0" ?>
<custdet>custdettag
<employee>emptag</employee>
<firstname>first</firstname>
<lastname>last</lastname>
</custdet>
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">root
<xsl:apply-templates/></xsl:template>
<xsl:template match="*">
<br/>
------
<xsl:value-of select="generate-id()" />
<xsl:value-of select="name()" />
------
<br/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()">
<br/>
---text---
<xsl:value-of select="generate-id()" />
<xsl:value-of select="name()" />
------
<xsl:value-of select="."/>
---/text---
<br/>
</xsl:template>
</xsl:stylesheet>
The expected output (as per the following node
structure)_
/ (root node)
+- custdet
| +- 'custdettag'
+- employee
| +- 'emptag'
+- firstname
| +- 'first'
+- lastname
+- 'last'
was,
root
------ N400001custdet ------
---text--- N400002 ------ custdettag ---/text---
------ N400003employee ------
---text--- N400004 ------ emptag ---/text---
------ N400005firstname ------
---text--- N400006 ------ first ---/text---
------ N400007lastname ------
---text--- N400008 ------ last ---/text---
But what i am getting now is
root
------ N400001custdet ------
---text--- N400002 ------ custdettag ---/text---
------ N400003employee ------
---text--- N400004 ------ emptag ---/text---
---text--- N400005 ------ ---/text---
------ N400006firstname ------
---text--- N400007 ------ first ---/text---
---text--- N400008 ------ ---/text---
------ N400009lastname ------
---text--- N40000A ------ last ---/text---
---text--- N40000B ------ ---/text---
if i apply <xsl:strip-space elements="*"/> i am
getting the expected output.
from which node does the ---text--- N400005 ------
---/text--- ,---text--- N400008 ------ ---/text---
,etc.. getting printed.
Thanks in advance
Aseef.J
--- Jeni Tennison <jeni@jenitennison.com> wrote:
> Hi Aseef,
>
> > This could very much be elementary (sorry), but i
> really didnt
> > follow in which order the nodes got processed and
> why i got the
> > contents of firstname and lastname elements
> printed twice before
> > adding the template rule for custdet element. Can
> anyone give some
> > tip as to what really happened with the two cases.
>
> Your XML structure was:
>
> <custdet>
> <employee />
> <firstname>first</firstname>
> <lastname>last</lastname>
> </custdet>
>
> Which translates to a tree that looks like:
>
> / (root node)
> +- custdet
> +- employee
> +- firstname
> | +- 'first'
> +- lastname
> +- 'last'
>
> When the processor starts processing, it starts at
> the root node and
> tries to find a template that matches it. If it
> doesn't find one, it
> uses the built-in template:
>
> <xsl:template match="/">
> <xsl:apply-templates />
> </xsl:template>
>
> which applies templates to the root node's child
> nodes -- in this case
> the custdet element. Again, if there's no template
> for an element (as
> there isn't for the custdet element), the processor
> uses the built-in
> template:
>
> <xsl:template match="*">
> <xsl:apply-templates />
> </xsl:template>
>
> This applies templates to all the children of the
> element: the
> employee element, the firstname element and the
> lastname element. So
> you get templates applied once to the firstname and
> lastname elements
> because of the built-in template for the custdet
> element.
>
> When the processor applies templates to the employee
> element, it finds
> the following template:
>
> <xsl:template match="employee">
> <xsl:apply-templates select="//firstname"/>
> <xsl:apply-templates select="//lastname"/>
> </xsl:template>
>
> Inside the template, you have two
> xsl:apply-templates instructions,
> which apply templates to all the firstname and
> lastname elements in
> the document. So templates get applied to the
> firstname and lastname
> elements for a second time, from within this
> template.
>
> If you want to only process the firstname and
> lastname elements once,
> then you can either add a template for the custdet
> element that only
> processes the employee element:
>
> <xsl:template match="custdet">
> <xsl:apply-templates select="employee" />
> </xsl:template>
>
> Or you can change the template for the employee
> element so that it
> doesn't apply templates for the firstname or
> lastname elements (or
> remove that template entirely).
>
> But as Jarno said, if the firstname and lastname are
> information about
> the employee, it probably makes more sense to alter
> your XML
> structure, so that the firstname and lastname
> elements are nested
> inside the employee:
>
> <custdet>
> <employee>
> <firstname>first</firstname>
> <lastname>last</lastname>
> </employee>
> </custdet>
>
> You should then change your template for the
> employee element so that
> the paths are relative to the employee element
> you're on rather than
> getting all the firstname and lastname elements in
> the document:
>
> <xsl:template match="employee">
> <xsl:apply-templates select="firstname" />
> <xsl:apply-templates select="lastname" />
> </xsl:template>
>
> Cheers,
>
> Jeni
>
> ---
> Jeni Tennison
> http://www.jenitennison.com/
>
>
> XSL-List info and archive:
> http://www.mulberrytech.com/xsl/xsl-list
>
__________________________________________________
Do You Yahoo!?
Yahoo! Greetings - send holiday greetings for Easter, Passover
http://greetings.yahoo.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list