[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Dear Karen,
At 02:06 PM 6/21/2006, you wrote:
Yes, exactly 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,
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.
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:
stylesheet (no other templates):
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.
Re: [xsl] Convert XML to Excel using XSLT Question II.
Subject: Re: [xsl] Convert XML to Excel using XSLT Question II. From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> Date: Wed, 21 Jun 2006 14:30:34 -0400 |
Dear Karen,
At 02:06 PM 6/21/2006, you wrote:
In your following example:
<xsl:template match="foo"> <br/> </xsl:template>
A <br> will show up every time there is a "foo" tag, but the decendants of "foo" won't be traversed. Is this correct?
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.
Having the instruction be explicit also allows us to:
1. Do it more than once 2. Apply templates to any nodes we like (not just the children)For your point #2, if I'd like to apply this template to its children and other nodes, how do I do this? I guess I need to give it a name and let other nodes call this template, right? Then use apply-template to its children, right? If I'm right, then can <xsl:template name = "foo" match = "foo1"> be right?
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Convert XML to Excel usin, Karen Yang | Thread | Re: [xsl] Convert XML to Excel usin, Karen Yang |
Re: [xsl] XSLT convert xml file to , Karen Yang | Date | Re: [xsl] XML data appearing in XSL, Wendy Bossons |
Month |
Keywords