[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] super basic xsl question

Subject: RE: [xsl] super basic xsl question
From: "Mark Lundquist" <ml@xxxxxxxxxxxxxx>
Date: Thu, 13 Jan 2005 11:20:56 -0800

> From: Jeb Boniakowski [mailto:jeb@xxxxxxxxxxx]
> In general though, on the topic of apply-templates, there is a larger
> issue that trips me up.  Oftentimes, it seems that I mess up my set of
> templates in such a way that things get matched and copied to the
> output tree automatically, even though they are matched.  To deal with
> this, I've been sticking a template at the top of my sheets that is:
> <xsl:template match="text()"/>
> Is this bad style?  Is it a crappy hack to deal with messed up
> templates?

Yes :-).

> I have situations where I have things along the lines of:
> <node>
>    <description>Foo</description>
>    <datum>1</datum>
>    <datum>2</datum>
> </node>
> When I do something like:
> <xsl:template match="node">
>    <h1><xsl:value-of select="description"/></h1> <!-- XXX --->
>    <ul><xsl:apply-templates/><ul> <!-- YYY --->
> </xsl:template>
> <xsl:template match="node/datum">
>   <li><xsl:value-of select="."/></li>
> </xsl:template>
> I end up with:
> <h1>Foo</h1>
> Foo  <!-- Extraneous foo that I don't want -->
> <ul>
>   <li>1</>
>   <li>2</2>
> </ul>

OK... see the lines I marked with the comments 'XXX' and 'YYY' above.

You can either:

(1) Change the line marked 'YYY' to this:

	<ul><xsl:apply-templates select="datum" /></ul>


(2) Move the line 'XXX' out into its own template that matches
"node/description".  This will leave you with a template matching "node"
that does nothing but "<xsl:apply-templates />.  That template is
unnecessary and can be deleted... the default rules will do what you want
here (since <node> has no (non-whitespace) text-only children).

The choice of (1) or (2) is a matter of taste, or how you happen to be
feeling at the moment :-)... it really doesn't matter.  (1) is more of a
"push" (template-driven) approach, and (2) is more of a "pull"
(content-driven) approach.

> Does the <apply-templates/> in
> node automatically copy the text values of any child nodes that are not
> explicitly matched?


>  If so, why?

See http://www.dpawson.co.uk/xsl/sect2/defaultrule.html.


Current Thread