Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your 1st example Template #1 runs because when you start processing the input xml it begins at the root and that is the only template in your stylesheet that matches the root element. Even if it was 2nd in the stylesheet it would still run 1st.</p> <p>In this example template 2 will not run as you have already processed the root element using template 1 and there are no more elements to process after the root. If you did want to process other elements using additional templates you should change it to.</p> <pre><code>&lt;xsl:template match="/"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; </code></pre> <p>This then allows you to define a template for each element you are interested in and process the xml in a more logical way, rather than doing it procedurally.</p> <p>Also note that this example will not output anything as at the current context (the root) there is no firstName element, only a person element so it should be:</p> <pre><code>&lt;xsl:template match="/"&gt; &lt;xsl:value-of select="person/firstName"/&gt; &lt;xsl:value-of select="person/lastName"/&gt; &lt;/xsl:template&gt; </code></pre> <p>I find it easier to think that you are stepping through the xml, starting at the root and looking for the template that matches that element then following those instructions to generate teh output. The XSLT transforms the input document to the output so the output doucument is empty at the start of the transformation. The output is not used as part of the transformation it is just the output from it.</p> <p>In your 2nd example Template #2 will not execute because the template is run against the input xml not the output.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload