Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP & XML (again)

David
Guest
 
Posts: n/a
#1: Jul 16 '05
Would I be right in saying the following files are equilavent, except
first on is HTML and second one is XML?
Code:

<?
echo "<h1>Hello world!</h1>".<br>;
echo "This is HTML";
?>


Code:

<?
echo "<page>";
echo "<hello>Hello";
echo "<world>world</world>";
echo "</hello>";
echo "<text>This is XML</text>";
echo "</page>";
?>


The above example I think illustrates my problem as as far as I can
see even to send "Hello world" to the screen I've got to write twice
as much code, and I'd still have to write an XSL file to convert it
into HTML anyway.
Perhaps this illustrates my confusion. Should I be trying to write php
files to generate XML or should I be trying to write XML files that
generate PHP. I know you can intergrate things like javescript into a
XML file so can you also do this with PHP?

David Walker
Guest
 
Posts: n/a
#2: Jul 16 '05

re: PHP & XML (again)


> Would I be right in saying the following files are equilavent, except[color=blue]
> first on is HTML and second one is XML?[/color]

Sort of - it is true that the second is formatted as XML would be, but i'm
still not sure what your use of XML has to be. For example, if you have a
fairly fixed format for every page, you could have a single standard page,
and simply use a different XML file to load the content into the page
template.
As an example, say you have a simple html page with the layout you want, and
now want to expand that so that you can have lots of similar pages but
without duplicating the content. If you have to use XML, you'd end up
putting the content in the XML file, and just keeping the layout as html.
You can do that with XSL, just loading the XML text into the right places,
or if you need more control over the display of the XML file then use PHP to
read in the XML file and do with it whatever you like.
[color=blue]
> The above example I think illustrates my problem as as far as I can
> see even to send "Hello world" to the screen I've got to write twice
> as much code, and I'd still have to write an XSL file to convert it
> into HTML anyway.[/color]

That is sort of true, but you'd never do that for a single page - its just
not worth the effort. If you have lots of pages that have a standard
format, then teh XSL holds that format, and just reads different XML files
to change the content very quickly.
[color=blue]
> Perhaps this illustrates my confusion. Should I be trying to write php
> files to generate XML or should I be trying to write XML files that
> generate PHP. I know you can intergrate things like javescript into a
> XML file so can you also do this with PHP?[/color]

That depends entirely on your application. From your last post I know
you're only using XML because you are forced to for a project, but in what
way you have to use the XML is unspecified. Now, you could use PHP to
generate XML very easily, just by having the PHP page output the appropriate
header and code instead of HTML - that's no different. XML can't generate
PHP though, at least not in any useful way, but you can have PHP to load XML
content back into webpage. The difference with that to XSL is that if you
use XSL then the client is doing the work of formatting the XSL (I think),
where as if you use PHP the server is doing the work and the client gets
what to them looks like a static html page - they have no way to know that
you even used XML unless you tell them somewhere.

So, depending the the application, you could either just write the XML for
each page yourself and upload it to the server, or if the pages change every
so often then get the PHP to rewrite the XML every time it changes. If its
constantly updating, PHP can write the XML on the fly for every request.
For reading the XML back, you can use either XSL or PHP, and which you use
is really up to you. Personally I chose to use PHP and then the client gets
just a static html page, which I think is probably better from a
compatibility point of view.

If you can provide an example of what your page does, or better link to the
page as it is now, then people may be able to give you examples of where you
could best use XML.

David


CC Zona
Guest
 
Posts: n/a
#3: Jul 16 '05

re: PHP & XML (again)


In article <3eb239cb.0307140450.623eabc9@posting.google.com >,
biffta@hotmail.com (David) wrote:
[color=blue]
> Would I be right in saying the following files are equilavent, except
> first on is HTML and second one is XML?
> Code:
>
> <?
> echo "<h1>Hello world!</h1>".<br>;
> echo "This is HTML";
> ?>
>
>
> Code:
>
> <?
> echo "<page>";
> echo "<hello>Hello";
> echo "<world>world</world>";
> echo "</hello>";
> echo "<text>This is XML</text>";
> echo "</page>";
> ?>
>
>
> The above example I think illustrates my problem as as far as I can
> see even to send "Hello world" to the screen I've got to write twice
> as much code[/color]

It's not a valid comparison. The HTML example isn't complete/valid, so of
course the XML looks more verbose. Which of these examples (leaving off
PIs and doctype declarations which look like even more "clutter" from a
reader's perspective) do you find more compact and clear?

<html>
<head>
<title>HTML</title>
</head>
<body>
<h1>Hello world!</h1>
<br>
<p>This is HTML</p>
</body>
</html>

vs.

<page title="XML">
<heading>Hello world!</heading>
<info>This is XML</info>
</page>
[color=blue]
> and I'd still have to write an XSL file to convert it into HTML anyway.[/color]

Not necessarily. Even when a transform is needed, it may not be to HTML at
all or even to HTML alone. There are plenty of circustances and
applications in which you could be feeding XML as XML, with the receiver
taking responsibilty for further processing. XML is incredibly useful for
"packaging" information descriptively, so that the information can be used
very flexibly. With the aid of a couple different XSL files and XML
parsing applications, your single file's content could be meaningfully
utilized as HTML, RSS, PDF, RTF, SYLK, and more. With a well-considered
DTD it could even be read by regular humans. (For instance, how hard is it
to figure out what the second example says?)

--
CC
David
Guest
 
Posts: n/a
#4: Jul 16 '05

re: PHP & XML (again)


I think Id have to say I like the idea of server-side processing, so I
would be creating the XML to hold the content of the page then using
PHP to parse the data and change it into something that could be
viewed in a browser.

But hang on, what if the majority of the content of a page is
dynamically generated out of a dbase, then what would I actually need
to store in the XML?

Consider the situation, two pages the first being a login page and the
second being the member services after a sucessful login.
The first page would have the general layout buttons etc, plus a form
where they would enter their details and press login. Not much scope
for XML there as there wont really be that much content on the actual
page.
Second page, showing the member services e.g. update profile, send
friend message... Possibly a bit of scope for XML there holding a few
tables of buttons and info. But the majority of the content of the
page would be coming out of a database and so I still see little use
for XML.

Dont get me wrong Im not trying to have a go at XML I just dont
beleive Im thinking about it in the right way and I used the previous
example to try and show this.
What would be a more suitable way to intergrate XML into the previous
example, because if someone could guide me there it might give me a
better idea!
thanks
David Walker
Guest
 
Posts: n/a
#5: Jul 16 '05

re: PHP & XML (again)


> Using XSL to transform XML into HTML does not have to be performed on[color=blue]
> the client. I use PHP to both generate XML and perform the XSL
> transformation on the server (see
> http://www.tonymarston.net/php-mysql/sablotron.html for details) so
> that all that gets sent to the client browser is a standard HTML file.
> There is no way of telling how this file was created, from XSL, from
> PHP, from Java or whatever.[/color]

Oh OK, I've never used XSL, just very quickly looked and since it was
described as just an extended style sheet, assumed it would be client-side
like css is. I might have a go of this server side XSL, see what happens.

Thanks

David


Tony Marston
Guest
 
Posts: n/a
#6: Jul 16 '05

re: PHP & XML (again)


biffta@hotmail.com (David) wrote in message news:<3eb239cb.0307150225.549c15da@posting.google. com>...[color=blue]
> I think Id have to say I like the idea of server-side processing, so I
> would be creating the XML to hold the content of the page then using
> PHP to parse the data and change it into something that could be
> viewed in a browser.
>
> But hang on, what if the majority of the content of a page is
> dynamically generated out of a dbase, then what would I actually need
> to store in the XML?[/color]

Your thinking is all wrong. You do not use PHP to transform an XML
file into HTML as that is what XSL is for. You use PHP to extract data
from the database, convert this data into an XML file, then perform
the XSL transformation using a separate XSL file. The output of the
transformation (an HTML file) is then sent to the browser.

Even if your DBMS is capable of generating an XML file directly you
would still need a PHP process to generate the query, receive the
pre-built XML file, then perform the XSL transformation.

The beauty of using XML/XSL files in this way is that you could change
the language in which your business layer is written, say from PHP to
Java or whatever, and you could still carry on using the same XSL
files. Now THAT is what I call separation of the presentation and
business layers.

Tony Marston
http://www.tonymarston.net
[color=blue]
> Consider the situation, two pages the first being a login page and the
> second being the member services after a sucessful login.
> The first page would have the general layout buttons etc, plus a form
> where they would enter their details and press login. Not much scope
> for XML there as there wont really be that much content on the actual
> page.
> Second page, showing the member services e.g. update profile, send
> friend message... Possibly a bit of scope for XML there holding a few
> tables of buttons and info. But the majority of the content of the
> page would be coming out of a database and so I still see little use
> for XML.
>
> Dont get me wrong Im not trying to have a go at XML I just dont
> beleive Im thinking about it in the right way and I used the previous
> example to try and show this.
> What would be a more suitable way to intergrate XML into the previous
> example, because if someone could guide me there it might give me a
> better idea!
> thanks[/color]
Closed Thread