tags:

views:

48

answers:

3

I've been trying to create an xslt template, but it keeps silently failing like as if an exception is happening but is not being caught. The closing bracket is not being written out, invalidating the output;

the XML file

<?xml version="1.0"?>
<gallery>
    <item>
        <file>IMAGEHEADER1.jpg</file>
        <thelink>michaeljackson123.htm</thelink>
    </item>
    <item>
        <file>IMAGEHEADER2.jpg</file>
        <thelink>barrywhite456.htm</thelink>
    </item>
</gallery>

XSLT File

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <body>
                <table>
                    <tr>
                        <xsl:apply-templates />
                    </tr>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="gallery">
        <xsl:for-each select="item">
            <xsl:value-of select="position()"/>
            <xsl:choose>
                <xsl:when test="position() = 1">
                    <td rowspan="2" height="122" width="510">
                        <xsl:apply-templates select="." />
                    </td>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="item">
        <a style="display:block;width:520px;height:330px" id="categorylink">
            <xsl:attribute name="href">
                <xsl:value-of select="thelink"/>
            </xsl:attribute>
            <xsl:apply-templates select="file" />
        </a>
    </xsl:template>
    <xsl:template match="file">
        <img alt="">
            <xsl:attribute name="src">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </img>
    </xsl:template>
</xsl:stylesheet>

Invalid output, missing the closing tab.

<html>
<body>
<tr>1<td rowspan="2" height="122" width="510"><a style="display:block;width:520px;height:330px" id="categorylink" href="michaeljackson123.htm"><img alt="" src="IMAGEHEADER1.jpg"></a></td>2</tr>
</body>
</html>

My expected output is;

<html>
    <body>
        <table>
            <tr>1
                <td rowspan="2" height="122" width="510">
                    <a style="display:block;width:520px;height:330px" id="categorylink" href="michaeljackson123.htm">
                        <img alt="" src="IMAGEHEADER1.jpg"></img>
                    </a>
                </td>2
            </tr>
        </table>
    </body>
</html>

Please help, can't see why it's failing.

+1  A: 

How are you viewing the output? The closing tag on an img element is not required in HTML so if you are looking at it in a web browser, a lot of times the browser will display something slightly different than its literal input. I've at least noticed this with firebug/chrome debugger.

flatline
I'm viewing it using the Marrorsoft Xselerator application, there is just a window outputting the raw text.
wonea
+2  A: 

If I had to hazard a guess, I'd say try adding an:

<xsl:output method="xml"/>

I think what is happening is that your serialization method is in auto mode, when it sees the html element in the default (not xhtml) namespace it is defaulting to html serialization, in which it is not required to close empty tags.

Nick Jones
Thanks for the info. As a side note it's strange how a strict HTML form is not observed when it could be easily produced.
wonea
+1  A: 

This is because the differences between HTML and XML serialization:

Your stylesheet defaults to HTML serialization because your root element is html. In this case, all DTD declared empty elements are output the way it's suppose:

<img alt="" src="IMAGEHEADER1.jpg">

If you want an XML serialization, you should declare:

<xsl:output method="xml"/>

Then your output will be:

<html>
    <body>
        <table>
            <tr>1
                <td rowspan="2" height="122" width="510">
                    <a style="display:block;width:520px;height:330px" id="categorylink" href="michaeljackson123.htm">
                        <img alt="" src="IMAGEHEADER1.jpg" />
                    </a>
                </td>2
            </tr>
        </table>
    </body>
</html>
Alejandro
Yes. I believe this is a case where "failing" meant that the user's expectations were just not aligned with specified behavior. That's why it's so important for askers to state clearly what they expected.
LarsH
Alejandro, that's great thanks. Much help.
wonea
@wonea: You are wellcome.
Alejandro