This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Understanding Templates


Hewko, Doug wrote:
> Hi!
> 
> I have a couple of questions about Templates
> 
> Say that I have the following XML file:
> <root>
> 	<foo>Shoo
> 		<bar>Beer</bar>
> 	</foo>
> 	<eggs>Ham
> 		<bar>Soap</bar>
> 	</eggs>
> </root>
> 
> Using "match='/'" will give me the entire XML document

Not really. Processing begins at the root node (one level above the
element you named 'root' in your doc). The XSLT processor looks at
one node at a time and finds the template, perhaps using a built-in
one, that best matches that node. Processing ends when it is done
processing the template that matches the root node.

The template that matches the root node typically contains an
instruction telling the XSLT processor to go process another set
of nodes -- xsl:apply-templates, usually.

Within the template that matches the root node, you "have" just
the root node as the current node. You don't have the whole doc,
though you can access it pretty conveniently relative to the
current node.

The built-in templates for the root and element nodes contain
<xsl:apply-templates/>, which causes all child nodes to be
processed. (children being basically everything except attributes)

The built-in template for a text node copies the text node to
the result tree.

> If I have "match='//bar'",

Then you have a template that is a good match if the XSLT processor
happens to need to process an element named 'bar' that is a 
descendant of the root node.

> and a corresponding "value-of", would I get both
> "Beer" and "Soap" because "//" looks for any instance in the node of the
> named element?

No. xsl:value-of is an instruction to create a text node in the
result tree. If you give it a node-set in the select attribute,
it will use the string-value (as defined by XPath) of the *first*
node in the set, no more.

> If my only templates are "match='/'" and "match='bar'", what would I get?

Try it and see!

> Aside of using "match='foo/bar'" and "match='eggs/bar'", how could I make
> sure I get the <bar> under <foo> and not the one under <eggs>?

You need to concentrate more on what you do with apply-templates
than what you do with match.

I think once you understand the processing model, it will become more clear
how to solve your problem, whatever it is. (you didn't say)

   - Mike
____________________________________________________________________________
  mike j. brown, fourthought.com  |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  personal: http://hyperreal.org/~mike/

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]