1

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

Baked Inhalf
  • 3,375
  • 1
  • 31
  • 45
  • 1
    I suggest you study the "default template" and remember that the stylesheet is NOT driving the process -- it is not procedural. The input document drives the process and any input node not otherwise matched will be processed by the default template. – Jim Garrison May 05 '20 at 22:19
  • 1
    Too many questions at once. You need to understand the [XSLT processing model](https://www.w3.org/TR/1999/REC-xslt-19991116/#section-Processing-Model). And yes, it is very bad practice to ignore the source namespace/s. See here for the correct handling: https://stackoverflow.com/a/34762628/3016153 And also using `//` is not the most efficient way to select nodes. – michael.hor257k May 05 '20 at 22:20
  • @michael.hor257k Updated the question – Baked Inhalf May 06 '20 at 07:05
  • @JimGarrison So the first "block" is the default route? – Baked Inhalf May 06 '20 at 07:08
  • 2
    You should be reading some books on the language, all of which devote considerable space to descriptions of the rule-based processing model. And there are lots of tutorials as well, for example Evan Lenz: https://www.lenzconsulting.com/how-xslt-works/. Voting to close. – Michael Kay May 06 '20 at 07:59
  • Note that your example code assumes that `ns0:MyRoot` is the root element. That wasn't true in the example XML you had in the previous version of your question. In such case, the processing would stop after executing the 1st template, with an empty result. – michael.hor257k May 06 '20 at 07:59
  • 1
    I’m voting to close this question because you're basically asking for a tutorial introduction to the language. – Michael Kay May 06 '20 at 08:00
  • @MichaelKay Reading some books on the subject is usually the best bet, but considering time as a factor I found your link to be helpful (more than the other n guides i've read lately). – Baked Inhalf May 06 '20 at 08:29
  • 1
    I second the suggestion to read Evan Lenz' tutorial. Back in 2005 he wrote the "XSLT 1.0 Pocket Reference" (O'Reilly Books). The link Michael Kay gave you is the online version of the same chapter in that book. In a few short pages that explanation triggered the "Aha!" moment for me. Once you assimilate that, how you view XSLT will change for the better. – Jim Garrison May 06 '20 at 17:41
  • @JimGarrison Yes I read the whole thing, very spot on. Might as well look into that book, since it's nice to have a go-to source when you get stuck on a problem. – Baked Inhalf May 06 '20 at 20:58

1 Answers1

2

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.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51