Forum Moderators: open

Message Too Old, No Replies

XML declaration doesn't work in php files?

Writing XHTML...

         

Crescendo

10:54 am on Aug 5, 2002 (gmt 0)



In the revised (Second Edition) W3C XHTML Recommendation [w3.org] (1 August 2002), in Section 3.1.1 part 5, it states "An XML declaration is not required in all XML documents; however XHTML document authors are strongly encouraged to use XML declarations in all their documents."

Eg:

<?xml version="1.0" encoding="UTF-8"?>

But if I do this in a php file, I get a parse error on that line. Do I need the line? Or is using php not proper XHTML anyway?

Rhys

11:52 am on Aug 5, 2002 (gmt 0)

10+ Year Member



That declaration is XHTML rather than PHP, so I think it would not work with the ??'s in the code

rewboss

11:57 am on Aug 5, 2002 (gmt 0)

10+ Year Member



The problem, of course, is that <? is also an opening PHP tag. The PHP parser gets to the <? before the browser does, but then can't make sense of the rest of the line. You can change the php.ini file to disallow short tags, preventing the parser from interpreting <? as the start of a block of PHP, but that means you have to change all your short tags from <? to <?php, and <?= to <?php echo.

Another cure is to use PHP to output the <?xml declaration in a print or echo statement.

There's no problem (other than that) with using PHP in conjunction with XHTML. You are using PHP to create an XHTML file: the browser doesn't care how the XHTML file was generated, as long as it's valid. If you View Source and see a valid XHTML file, it's a valid XHTML file whether you typed it in yourself or used PHP, ASP, Perl or even ROCProL (rewboss' Own Cool Programming Language) to output it.

moonbiter

1:39 pm on Aug 5, 2002 (gmt 0)

10+ Year Member



ROCProL? I like that. Sounds like REBOL [rebol.com]. :)

Crescendo

2:42 pm on Aug 5, 2002 (gmt 0)



Great! But what happens if the document is read by a device which doesn't have PHP? It won't recognize it as XML. So the document becomes platform specific, surely not the idea of XML documents?

moonbiter

2:50 pm on Aug 5, 2002 (gmt 0)

10+ Year Member



Great! But what happens if the document is read by a device which doesn't have PHP? It won't recognize it as XML. So the document becomes platform specific, surely not the idea of XML documents?

This is true, a PHP document is not an XML document. The output of the PHP document will be an XML document though, and will be portable.

Knowles

3:10 pm on Aug 5, 2002 (gmt 0)

10+ Year Member



It will work, I ran into this problem before Here [webmasterworld.com]. You have to echo or print it out <? echo "<?XML \"Dec\"?>"; ?>

Crescendo

3:14 pm on Aug 5, 2002 (gmt 0)



OK, so a device such as a spider finds the document on the server, and comes across this line:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

It fails to consider it as XML, and moves on.

Solution?

moonbiter

4:12 pm on Aug 5, 2002 (gmt 0)

10+ Year Member



OK, so a device such as a spider finds the document on the server, and comes across this line:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>

It fails to consider it as XML, and moves on.

Solution?

Okay, maybe I'm ignorant here, but I don't see the problem.

Spider requests page from server. Server gets request, parses php file, and serves spider XHTML. Spider reads XHTML, and moves on.

No php source will be sent, since PHP server-side scripting.

Now, if you don't have php enabled on the server, that would be a problem. But then why are these php pages then?

ergophobe

9:10 pm on Aug 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Moonbiter and Knowles are right: just echo the xml declaration - it's parsed before the spider sees it.

If you are using templates, even a primitive one like mine, no URL ever points to a file that outputs anything, let alone valid XHTML.

If you're worried about what would happen if some malicious sysadmin suddenly removed PHP from your system and you started sending out php source, I would say

- for most files, who cares, unless you have some absolutely ingenious script that you don't want anyone to see? Considering the number of freely available PHP scripts out there, that's pretty unlikely.

- for files holding passwords (such as to open a connection to a database), put them in a directory that is outside the directory tree and is not publicly readable. If your setup allows, it's best to put that stuff outside of your web tree altogether, like in /usr/local/etc or something like that. No file that ever has a URL pointing to it should include that stuff IMHO

Tom

rewboss

7:02 am on Aug 6, 2002 (gmt 0)

10+ Year Member



Crescendo, I think you're getting slightly confused as to how PHP operates.

When PHP is installed on the server, any file with a .php extension will be parsed when it is requested but before it is sent to the device -- whether said device is a browser or a spider.

Imagine you have a PHP document that looks like this:


<html>
<head><title>Test</title></head>
<body>
<?php
echo "<h1>Hello, world</h1>";
?>
</body>
</html>

A spider requests the document. PHP sees the .php extension on the file name, and knows that it has to parse the file. It does so, executing any PHP statements it finds before letting the spider see the document. The spider then receives the following file:


<html>
<head><title>Test</title></head>
<body>
<h1>Hello, world</h1>
</body>
</html>

It doesn't see any PHP at all. Spiders request files in the same way that browsers do.

Crescendo

8:02 am on Aug 6, 2002 (gmt 0)



Then there's no problem! I'll add PHP to all my files now.

Crescendo

8:38 am on Aug 7, 2002 (gmt 0)



OK, added the code, but when I came to validate it, I get the following message:

"Warning: DOCTYPE Override in effect! Any DOCTYPE Declaration in the document has been supressed and the DOCTYPE for «XHTML 1.0 Transitional» inserted instead. The document will not be Valid until you alter the source file to reflect this new DOCTYPE."

This is nonsense, as I'm using the DTD supplied by the W3C! It replaces the one I had with exactly the same thing! All I can think is that the XML declaration above it is at fault, but again, I am copying code from the W3C. Or should it be on the second line after the DTD?

Also, I've noticed one page I have now has an extra line space in it, and tighter line-height in one cell. Huh!?!? Does the XML declaration do something to the browser? (IE6, W98)

ergophobe

5:01 pm on Aug 7, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you're using the W3C validator you have two choices

- let it find the DOCTYPE

- force it to validate to a specific DOCTYPE.

If you choose the latter, you are overriding your document's DOCTYPE, even if you are overriding it with the exact same DOCTYPE. What it's telling you is that your DOCTYPE is being ignored, not that it's incorrect.

If you choose auto-detect, it should use your DOCTYPE

In other words, you are submitting the form incorrectly, but there is nothing wrong with the W3C validator and there may be nothing wrong with your document.

Tom