简体   繁体   中英

XSLT 1.0 Multiple template match blocks

In some xslt files i've seen multiple template match blocks. 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 ).

However, in general there are very good reasons to have multiple templates because of the way XSLT processing works. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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