简体   繁体   English

XSLT 1.0 多个模板匹配块

[英]XSLT 1.0 Multiple template match blocks

In some xslt files i've seen multiple template match blocks.在一些 xslt 文件中,我看到了多个模板匹配块。 Is there a reason for this?是否有一个原因?

<xsl:template match="/">
    <xsl:apply-templates select="/ns0:MyRoot" />
</xsl:template>

<xsl:template match="/ns0:MyRoot">
.. // do stuff
</xsl:template>

Update: Removed 2nd question更新:删除了第二个问题

In your example, there is no good reason to have the 1st template, because it doesn't do anything that wouldn't be done anyway by the built-in template :在您的示例中,没有充分的理由拥有第一个模板,因为它不会做任何内置模板无论如何都不会做的事情:

<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

(unless the purpose is to prevent the processing of XML documents whose root element is not ns0:MyRoot ). (除非目的是防止处理根元素不是ns0:MyRoot的 XML 文档)。

However, in general there are very good reasons to have multiple templates because of the way XSLT processing works.但是,由于XSLT 处理的工作方式,通常有很好的理由拥有多个模板。 In many cases it is convenient to apply templates to multiple nodes, and let the processor find the best-matching template for each node.在许多情况下,将模板应用于多个节点很方便,让处理器为每个节点找到最佳匹配的模板。 This allows you to encapsulate the code for processing each type of node and avoid complex conditional statements.这允许您封装用于处理每种类型节点的代码并避免复杂的条件语句。

OTOH, it needs to be said that too often multiple templates are overused for no good reason, leading to GOTO syndrome . OTOH,需要说的是,经常无缘无故地过度使用多个模板,导致GOTO 综合症

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM