Search code examples
xmlxsltmsxsl

How can I force XSLT transform to follow order of document?


So I am writing an XSLT transform to parse an XML file into HTML. For a while, everything was going well. However, I've run into a problem. Basically, doesn't seem to follow the order I would expect.

In the transform there is some code that looks like this.

<xsl:template match="/"
<html>
    <head></head>
    <body>
        <h1>Summary</h1>
        <table>
            <xsl:apply-templates select="theItem"/>
        </table>
    </body>
</html>
</xsl:template>
<xsl:template match="theItem"/>
    <tr><td>Column1</td><td>Column2</td></tr>
</xsl:template>

What I would expect that to do is build a table, and fill in a row for each match to the template.

What it does instead is open and close the table, then put all the rows after. Something like this.

<table></table>
<tr>
    <td>
        Column1
    </td>
    <td>
        Column2
    </td>
</tr>
<tr>
    <td>
        Column1
    </td>
    <td>
        Column2
    </td>
</tr>

So what the heck is happening? I'm not sure if it matters, but I used msxsl to do the transform. I also tried embedding the transform in the data and opening it in IE. Creates the same issue. I can't see any way this would be the intended behavior, but maybe there's someting I'm missing.

EDIT

Note that I can wrap the template application in any number of tags of any type, and they all open and close before any of the template information is shown.


Solution

  • While for the most part, this question is obsolete, I'd like to clarify the issue if anyone ever finds a similar bug.

    What was happening was that

    <xsl:apply-templates select="theItem"/>
    

    Did not match anything when it was called in the sample code. So it didn't produce anything (as you would expect). However, later in the code something did match to

    <xsl:template match="theItem"/>
        <tr><td>Column1</td><td>Column2</td></tr>
    <xsl:template>
    

    And was inserting all the rows, just like as it should. This produced the undesired effect.

    If anyone ever sees this behavior, that is the first place I would look. Ensure that your template is actually being called when you expected it to, and not some other time. That could cause what seems like an out of order application of the templates. Thank you to everyone who helped me come to this solution.