Re: [xsl] "Instantiating a template" ?

Subject: Re: [xsl] "Instantiating a template" ?
From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
Date: Sun, 30 Nov 2003 22:38:11 +0100
Sony Antony wrote:
1. I saw a mention about "data driven" vs "template driven" paradigms
for XSLT transformations. But I could not see any detailed description
of these 2 fundamentally different strategies. I thought XSLT is
essentially data driven with the input XML solely deciding which
templates to pick for execution.

You can cheat. Once a tempalte is matched, the template has sort of control. For example: <xsl:template match="/"> <!-- pull values from source --> <xsl:value-of select="/foo/bar"/> <xsl:for-each select="/foo/stuff"> <!-- do something --> </xsl:for-each> </xsl:template> Because there is alwasy a root node, the template matches, and you have full control how elements are pulled from the source.

The "pull" style is often used if the source is well known and
usually "data" like XML under full control of the parties working
on it (standardized or agreed upon before and machine generated).
The  "push" style, which relies on matching templates, is better
suited for "document" style XML, especially if there is mixed
content.

2. I also saw a lot of mention about "Instantiating" a template. What
exactly is "Instantiating". I thought its just "executing" the template
like a subroutine.
"Instantiating" means nodes are put into the result tree. You
can think of it as "executing" if you like.

3. Is there a technique/function that will show me the type of a node (
Element vs Attribute vs text ) at run time.

Use templates for matching: <xsl:template match="*" mode="node-type"> <xsl:text>Element node</xsl:text> </xsl:template> <xsl:template match="@*" mode="node-type"> <xsl:text>Attribute node</xsl:text> </xsl:template> <xsl:template match="text()" mode="node-type"> <xsl:text>Text node</xsl:text> </xsl:template>

 <xsl:template match="node()">
   <xsl:apply-templates select="." mode="node-type"/>
   <xsl:apply-templates/>
 </xsl:template>


4. What s the difference between the following 3 template defs
<xsl:template match="*">
<xsl:template match="node()">
<xsl:template match="//*">

The first will match any element node. The second matches any node except namespace nodes, but including element, attribute, text, comments, PI and the root node (not sure about the last, check with the spec). The third is basically the same as the first, but it has a slightly different inherent priority, which matters if you include or import style sheets. In general, a "//" in front of a match pattern is unnecesary.

5. If I have the following 2 templates
<xsl:template match="*"> stuff1 </xsl:template>
<xsl:template match="a/b/c"> stuff2 </xsl:template>

which one will get executed if I have a nested  element c inside element
b which is nested inside a ( a is not the document element )

It should be the second, because it has a higher priority than the mode generic "*" pattern.

J.Pietschmann


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



Current Thread