开发者

Why don't all XSLT templates get executed at once?

In the开发者_如何学JAVA this posting, I don't understand why the two template blocks don't get executed upon in the incoming XML at the same time. As far I can can see, the XSL risks the second template executing for every Document element whether it is called with apply-templates or not.

Could someone explain this?


Mads is right that one template matches just the root xml element, and one matches a specific element (Problems/Problem/Description). So, the two are operating on different elements. However, is your question more general? Is it why aren't two xsl:templates that could both match a particular XML element executed against it? In other words, why wouldn't both the template that matches "node()" and the the one that matches "Problems/Problem/Description" run against the same Problems/Problem/Description XML element?

If so, then there's a key XSLT concept you're missing, priority. It's similar to order of operations that you learned in algebra (division, multiplication, subtraction, addition). In XSLT it works like this. For any given XML element, the default behavior is to process it once and only once. It's the job of the XSLT engine to determine all the templates that could act against a particilar XML element and find the most specific one and only apply that template.

How specificity is determined is a bit complicated, but in this example, it' simple. One template matches any nodes (node()), and the other matches a specific, named, node. So, the specifically named node wins.

For the full rules on priority, see section 5.5 of the XSLT spec at W3C. http://www.w3.org/TR/xslt


The template matching the root node <xsl:template match="/"> is the starting point for the transform and controls what happens from that point forward.

If the template for the root node did not call an apply-templates, then you would only get the output described within that template for the root node.

However, because it does call apply-templates, it tells the processor to execute whatever template rules that match the content it encounters. The xpath expression given in the @select happens to be Problems/Problem/Description, which limits the content in which the processor will apply template rules for to the Description elements.

So, since the XSLT has a template defined for Description elements (<xsl:template match="Description"> ), that template is triggered for each Description element that it encounters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜