wonea
wonea

Reputation: 4969

XSLT failing for unknown reason on xsl:value-of

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.

Upvotes: 0

Views: 308

Answers (3)

user357812
user357812

Reputation:

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>

Upvotes: 1

Nick Jones
Nick Jones

Reputation: 6493

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.

Upvotes: 2

flatline
flatline

Reputation: 42613

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.

Upvotes: 1

Related Questions