[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Convert XML to Excel using XSLT Question II.
Dear Karen,
At 02:06 PM 6/21/2006, you wrote: In your following example: Yes, exactly right. Then according to your following rule #3, when you have a template that matches a node (say node A), no matter whether there is apply-template, this node A would be traversed or applied this template anyway, but not its decendant, right? Node A will be matched only if it is selected for processing. This typically happens because its parent has been matched by a template that contains an apply-templates instruction, and it has been selected there. Remember that this will happen to A's parent even if there is no template for it, since in that case it will be matched by the built-in rule, <xsl:template match="*"> <xsl:apply-templates/> </xsl:template> Sorry that my questions may be too naive, but all your replies did help me a lot, I feel I'm closer and closer to the goal..... :-) Actually it's not that the questions are naive. Everyone starts at the beginning; smart learners often stay there awhile, getting the foundations right. It's just frustrating because while apparently you really need a working stylesheet pronto, and so we'd like to help with that, we can also tell that without a firm grasp of *why* things are happening, you'll continue to have the same questions of how to fix them. (We see this all the time on the list, which makes it worse.) In other words: you can't afford to start from the beginning, but are rushing straight into the middle. Experience shows that such a course takes longer than starting from the start. 1. Applying templates does *not* happen just because a template is present 2. Nevertheless, since the built-in default templates (for elements and the root) contain apply-templates instructions, the tree will be traversed completely by default in any case. 3. Yet, this default behavior is easy to override, at any level of the tree, simply by writing a template that does not contain an apply-templates instruction. This is an important, and very useful, feature of the language. In the general case, we simply allow the processor to match elements one level at a time, by selecting children and matching them with templates that apply templates to their own children, etc. Occasionally it's useful to jump from a node down deeper than its children, but again since the default rule says to apply templates to children, any grandchildren or deeper will be processed eventually even without jumping to them explicitly. If you have a template that needs to match more than one kind of node, that's easy; we just write a match pattern accordingly: <xsl:template match="a | b | c">...</xsl:template> (This will match any node that could be found in a set of nodes "a | b | c", that is any a, b, or c.) Note that a, b and c do *not* have to be siblings. Try this: input: <doc> <title>My test document</title> <p>My paragraph.</p> <sec> <title>My subsection</title> <p>Hierarchies can be very useful.</p> </sec> </doc> stylesheet (no other templates): <xsl:template match="doc | title | p | sec"> <div class="{name()}"> <xsl:apply-templates/> </div> </xsl:template> and see what comes out. The ease of doing exactly this sort of thing is why XSLT is so powerful. As for your suggestion, it's true that a template can have both a name and a match, and this is sometimes useful; but it's also somewhat rare to actually need such a construction and I'd be surprised if you did. Cheers, Wendell
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|