1

I was outputting my XSLT as HTML, but I have read that outputting as XML is better for validation reasons. So when I change my output from HTML to XML it ruins the page, so does this mean I need to remove all HTML elements from the XSLT file now? I am also styling via an external CSS file, when outputting as XML, does the styling need to be within the XSLT file?

Here is my code for the page:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output 
method="xml" 
doctype-system="http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd"  
doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.2//EN" />

<xsl:template match="/">


        <xsl:element name="head">
            <xsl:element name="title">Selected Flight Route</xsl:element>
            <link rel="stylesheet" type="text/css" href="mystyles.css" title="Style"/>
        </xsl:element>

        <xsl:element name="body"> 

            <xsl:element name="div"><!-- This holds the navigation bar-->
                <xsl:attribute name="id">
                    <xsl:text>navdiv</xsl:text>
                </xsl:attribute>


                <xsl:element name="div"><!-- This holds the image links-->
                    <xsl:attribute name="id">
                        <xsl:text>navlinks</xsl:text>
                    </xsl:attribute>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinksone</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">flights.php</xsl:attribute>
                    </xsl:element>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinkstwo</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">planes.php</xsl:attribute>
                    </xsl:element>

                    <xsl:element name="a">
                        <xsl:attribute name="id">
                            <xsl:text>navlinksthree</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="href">weatherfeed.php</xsl:attribute>
                    </xsl:element>

                </xsl:element>
            </xsl:element><!-- End the navigation bar-->






<xsl:element name="div"><xsl:attribute name="id">
                            <xsl:text>maindiv</xsl:text>
                        </xsl:attribute>



<h1 id="heading">Flights and planes</h1>

<xsl:element name="div"><xsl:attribute name="id">
                            <xsl:text>divone</xsl:text>
                        </xsl:attribute><img width="30" height="30" src="globe.png"/><p class="centerp">Selecting the Globe will display all flight routes.</p></xsl:element>

<xsl:element name="div"><xsl:attribute name="id">
                            <xsl:text>divtwo</xsl:text>
                        </xsl:attribute><img width="75" height="30" src="planepic.png"/><p class="centerp">Selecting the Plane will display all Aircraft info.</p></xsl:element>

<xsl:element name="div"><xsl:attribute name="id">
                            <xsl:text>divthree</xsl:text>
                        </xsl:attribute><img width="32" height="30" src="weather.png"/><p class="centerp">The Weather icon displays the weather in all citys.</p></xsl:element>
</xsl:element>







        </xsl:element>
        <!-- End the Body element -->



</xsl:template>
<!-- End the Main Template element -->

</xsl:stylesheet>

Here is a link to the page with the output as XML, and here is the same page with output as HTML, can anyone tell me why this is messing up my page when outputting as XML? While researching to see what the issue was I noticed that example files outputting as XML did not seem to have any HTML elements in there.

Here is the original file I use to transform the XML:

<?php
$xml = new DOMDocument();   
$xml->load('flights.xml');

$xsl = new DOMDocument;
$xsl->load('index.xsl');

$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);
?>

1 Answer 1

1

Short Answer

You cannot output XML to the browser and expect him to render HTML, they are different languages.

Long Answer

If you output XML instead of HTML, the browser is not going to render the page as you expect because is creating XML nodes instead of HTML nodes. The difference between these two is that HTML nodes have more properties than XML nodes. E.g. some of these properties are the ones which allows us to style an HTML element.

The properties of an XML node allows us to manipulate and navigate the DOM, while HTML properties are a superset of all XML properties (I think).

For a list of properties which are available for each kind of node (from the perspective of Firefox, which follows pretty well the standards): https://developer.mozilla.org/en-US/docs/DOM/element

XHTML

Because you are referring to validation, an alternative to HTML 4.01 is XHTML 1.0 which is a different language than HTML or XML and is more strict in terms of validation (for example, it does not allow unmatched tags).

To indicate the browser that you are using XHTML 1.0 instead of HTML 4.01, you have to include one of these DOCTYPE elements at the top of your document.

 <!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

CSS Differences

There are also some (minor) differences on how the CSS is processed for an XHTML document. I leave you an article listing these differences:

http://reference.sitepoint.com/css/htmlxhtml

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you I have been working on this and I just figured out it is the Doctype that is causing the issue as I am developing a mobile site and when I use a mobile Doctype it screws the page up. Do you know how I can use a mobile XHTML Doctype in my XSLT? Thank you! I am trying to use one of these on this page mobiforge.com/testing/story/fixit-xhtml-mobile-profile
Well I have never used XHTML mobile and I do not know much about it, but you could read the second section of these article/fragment of book : xml.com/pub/a/2004/04/14/mobile.html In there they just use external DTDs. Hope it helps
Thanks for all your help, I have tried every type of Mobile DTD and nothing works. When I use the XHTML strict DTD my page loads fine and everything is valid, but when I use a Mobile DTD it messes it all up and I have no idea why. There does not seem to be much information on Mobile DTD's and XSLT! I shall keep reading up!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.