XSL Processing Order

html, xml, xslt

Solution

Here's what's happening:

- The XSLT processor reads the root element of your XML

- Then it looks in the stylesheet to see what matches. It finds your first template

- It executes the first template.

- The first template says to output the text, and then does nothing else, so the XSLT processor moves on to the next input element.... but you've processed the entire root node, so there are no more input nodes at the same level. It's done.

What you need to do is put an `<xsl:apply-templates/>` inside the first template. When this is encountered by the processor, it starts over but this time the context is the list of second-level nodes inside the root. It will look at each XML node in turn, find the best matching template in your stylesheet, and execute it.

This is a key concept -- The template is NOT in control, and is not procedural.

Problem

I've had trouble finding an exact, and simple, answer to this question on SO or elsewhere: In XSL files, how can you tell which template will be processed first, second, etc? I read that it was ordered by how specific the XPath was. Additionally, is there a difference in XSL 1.0 vs. 2.0? Finally, here is a flawed XSL file I am toying with. Currently the output is just the title "Table of Contents". I'll attach the XML here as well. ``` <xsl:template match="/"> <h1> <xsl:text>Table of Contents</xsl:text> </h1> </xsl:template> <xsl:template match="heading1"> <h2> <xsl:value-of select="."/> </h2> </xsl:template> <p> <xsl:text>This document contains </xsl:text> <xsl:value-of select="count(/article/body/heading1)"/> <xsl:text> chapters. </xsl:text> </p> ``` and the XML: ``` <article> <title> Creating output </title> <body> <heading1>Generating text</heading1> <heading1>Numbering things</heading1> <heading1>Formatting numbers</heading1> <heading1>Copying nodes from the input document to the output</heading1> <heading1>Handling whitespace</heading1> </body> ``` Any explanation as to why all the content isn't being displayed? Thank you for your help!

Original source