differences between for-each and templates in xsl?

前端 未结 7 649
你的背包
你的背包 2020-12-03 10:48

Both xsl:for-each and xsl:template are used to retrieve nodes from xml in an xsl stylesheet. But what is the fundamental difference between them? P

相关标签:
7条回答
  • 2020-12-03 11:28

    It doesn't really matter, but you may want to think about it for the following thumb of rules that I found:

    • If the code depends on the context position (position()), put it in a <xsl:for-each>.
    • If the code depends on the context node (. or any location path), put it in a matching template.
    • Otherwise, use a named template.

    Reference and read more at: http://www.jenitennison.com/blog/node/9

    0 讨论(0)
  • 2020-12-03 11:31

    Both the 'for-each' and 'template' are used to retrieve the nodes from xml in the xsl. But what is the difference between them in basically

    Here are some of the most important differences:

    1. xsl:apply-templates is much richer and deeper than xsl:for-each, even simply because we don't know what code will be applied on the nodes of the selection -- in the general case this code will be different for different nodes of the node-list.

    2. The code that will be applied can be written way after the xsl:apply templates was written and by people that do not know the original author.

    The FXSL library's implementation of higher-order functions (HOF) in XSLT wouldn't be possible if XSLT didn't have the <xsl:apply-templates> instruction.

    Summary: Templates and the <xsl:apply-templates> instruction is how XSLT implements and deals with polymorphism.

    Reference: See this whole thread: http://www.stylusstudio.com/xsllist/200411/post60540.html

    0 讨论(0)
  • 2020-12-03 11:32

    for-each can only be used inside one place in your template. Templates can be re-used with different apply-templates calls. I mostly use templates instead of for-each because of the added flexibility.

    0 讨论(0)
  • 2020-12-03 11:32

    One use of for-each I haven't seen mentioned: you can use it to switch the context node to another document. I've used it to transform data XML to a HTML input form. The template that matched a data field contained a for-each that selected a single node: the xs:element in the XSD that described the data field to transform.

    Using the description in the XSD one data field could be transformed to a radio button group, a drop down box, or a plain and simple text input. Without for-each I couldn't walk through the two documents at the same time.

    In general, I prefer matching templates. I find that it corresponds to the notion of a single transform applied at once better than for-each-ing this node, then the next, then the one after that, etc.. But that's a personal preference, of course.

    0 讨论(0)
  • 2020-12-03 11:36

    These are to complete different XSLT instructions.

    More than push vs. pull style, this is more like iteration vs. recursion.

    xsl:for-each is an iterator instruction with all the benefits and constrains of iteration in a stateless declarative paradigm: a good processor should not polute the call stack.

    xsl:apply-templates is a general recursion instruction. General in the sense that it's more powerful than xsl:call-template: you "throw" the selected nodes to the pattern matching mechanism, a truly "dynamic function invocation".

    0 讨论(0)
  • 2020-12-03 11:38

    I think this has some what to do with understanding push vs. pull style processing than just comparing xsl:for-each or xsl:template match="...". You often see programmers from another discipline using a lot of xsl:if, xsl:choose and for-loops when the problem could have been solved in a more elegant XSLTish way.

    But to the question: In my opinion, if you consider using xsl:for-each instead of processing the data with xsl:apply-templates you need to rethink. There are cases where a for-loop is suitable in XSLT, but whenever a matching template would do the same, templates are the way to go. In my experience, you can usually do most xsl:for-each with an xsl:apply-templates instead.

    Some benefits as I see it of using matching templates over a for-loop are:

    • The stylesheets are easier to maintain and extend especially if source data changes.
    • As @chiborg mentions, templates can be reused since they are not built into a specific template. Together with xsl:next-match in XSLT 2.0 you can chain templates together in powerful ways.
    • You don't have to mimic behavior already built in to all XSLT processors, that is; use xsl:apply-templates and let the processor work for you.
    • Also, I find it easier to understand and debug a push style stylesheet. If you divide your stylesheet info small templates which do one or a few things and write specific matching patterns it's easy to see which template is doing what and trace the source of the problem.
    0 讨论(0)
提交回复
热议问题