1

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.

  • 1
    Would it be possible for you to show your input XML document, as well as a complete XSLT stylesheet that replicates the problem? Thanks! – Tim C Jun 26 '14 at 21:37
  • 1
    Your `` is not actually applying. The table-row match is applied later in a different context. Perhaps `theItem` is not where you think it is? We need to see some of the xml to know for sure. – Francis Avila Jun 26 '14 at 21:40
  • Your template "theItem" should not `match` - it should have a `name` to be called from inside your table generating template. – Filburt Jun 26 '14 at 21:41
  • Unfortunately, due to the length and nature of the document, I cannot post it in full. Fortunately, trying to produce a suitable sample for you showed me where the error came from! I guess this a good lesson in debugging. – Russell_Rollins Jun 26 '14 at 21:50
  • So, this question is obsolete? Maybe you should answer it yourself or delete it. – Marcus Rickert Jun 27 '14 at 22:48

1 Answers1

1

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.