Using XML to split content [warning newbie lurks here]

B

build

G'day All,
I hope you folk are tolerant of newbies who don't express themselves
well, cause I'm one of those. If not please excuse this and move on.

I'm designing a small web site and while I thought I'd done a good job
of planning the project and learning the required skills I've now
discovered a hole in my plan.

I'm using a template like php script to the build pages.
I'm also using php sessions to authenticate access to members and
guests.
The content component is in xhtml.

Now I'm at the testing stage, I've started to prepare a few of the
xhtml files and realised a lot of the content is an amalgum of 'guest'
and 'member' stuff. I don't really want to have a separate page for
each class of user, with largly identical content.

I've read all the W3C tutorials on XML, XSLT, etc. I'm quite excited
about the possible uses of XML applied to my current problem and a
myriad of other applications (such a simple idea, but so bloody
useful). However my mind is currently a potpourri of half baked ideas.
I'm failing to get a proper grasp on exactly how to utilise xml.

I'm wondering if I could modify the xhtml files to xml and use tags to
decide what to display?

i.e. the stuff enclosed by the <member> tag should only be written to a
member session.

<h1>My Page</h1>
<p><em>16/03/2005</em><br />
Sorry, we were offline for a few hours this morning, someone tripped
over the extension lead.
</p>
<member>
<p>
<strong><em>03/03/2005</em>
<br />
Barton Mawer WINS at the Australian Grand Prix (<a
href="lindsay/bartonmawer.htm" target="main">more</a>).</strong>
</p>
</member>

My questions are:
1) Am I on the right track? Is XML the answer to my problem?
2) Is there an easy way of separating the content without having to
parse all the xhtml tags as well as the xml tags???
3) If so, I need to get a better understanding of XML, what is your
suggested next step after reading the W3C tutorial?

If this is an inappropriate place for this post could some kind soul
please point me in the right direction?

apologies in advance as I realise I have not phrased the question well
(cringe), your tolerance would be greatly appreciated.

Thanking you in anticipation,
build
 
S

Soren Kuula

Hi,

G'day All,
I hope you folk are tolerant of newbies who don't express themselves
well, cause I'm one of those. If not please excuse this and move on.

Grrrrrrrrrrrrrrrrrrrrrrrrrrrr.. BARK BARK! No, welcome, really :)
I'm designing a small web site and while I thought I'd done a good job
of planning the project and learning the required skills I've now
discovered a hole in my plan.

I'm using a template like php script to the build pages.
I'm also using php sessions to authenticate access to members and
guests.
The content component is in xhtml.

Good idea -- it's better to work on with power tools than html.
Now I'm at the testing stage, I've started to prepare a few of the
xhtml files and realised a lot of the content is an amalgum of 'guest'
and 'member' stuff. I don't really want to have a separate page for
each class of user, with largly identical content.
I've read all the W3C tutorials on XML, XSLT, etc. I'm quite excited
about the possible uses of XML applied to my current problem and a
myriad of other applications (such a simple idea, but so bloody
useful). However my mind is currently a potpourri of half baked ideas.
I'm failing to get a proper grasp on exactly how to utilise xml.

XML is just one way to describe (tree) structured data. If you are
working with that (you are), it's nice to have a well known and standard
description. XSML it that :)
I'm wondering if I could modify the xhtml files to xml and use tags to
decide what to display?

Well .. files and tags can't decide anything. What you want to do is to
decorate your contets with some more information, which is useful for
the program that will decide what to include. Your member elements are that.

The decision-implementing program could be this stylesheet:

blog.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:eek:utput method="xml"/>

<xsl:param name="loggedin" select="'false'"/>

<!-- rule that renames the blog element to html -->
<xsl:template match="blog">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>

<!-- default rule that copies everything, even attributes -->
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:copy>
</xsl:template>

<!-- rule that overrides the default rule for member elements-->
<xsl:template match="member">
<!-- if logged in, forget about the member element but recurse to
copy all its children. If not logged in, do not recurse the copying. -->
<xsl:if test="$loggedin='true'">
<xsl:apply-templates/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>


(I have altered your input a little, just adding a blog element around
all of it .. if not everything is contained in a single root element,
it's not valid XML):

blog.xml:
<?xml version="1.0"?>
<blog>
<h1>My Page</h1>
<p><em>16/03/2005</em><br />
Sorry, we were offline for a few hours this morning, someone tripped
over the extension lead.
</p>
<member>
<p>
<strong><em>03/03/2005</em>
<br />
Barton Mawer WINS at the Australian Grand Prix (<a
href="lindsay/bartonmawer.htm" target="main">more</a>).</strong>
</p>
</member>
</blog>


The stylesheet, when run on the input, takes a parameter loggedin. I
have made a couple of test runs, feeding a value for the parameter to
the stylesheet processor:


[dongfang@granada membersonly]$ xsltproc --stringparam loggedin false
blog.xsl blog.xml
<?xml version="1.0"?>
<html>
<h1>My Page</h1>
<p><em>16/03/2005</em><br/>
Sorry, we were offline for a few hours this morning, someone tripped
over the extension lead.
</p>

</html>


and

[dongfang@granada membersonly]$ xsltproc --stringparam loggedin true
blog.xsl blog.xml
<?xml version="1.0"?>
<html>
<h1>My Page</h1>
<p><em>16/03/2005</em><br/>
Sorry, we were offline for a few hours this morning, someone tripped
over the extension lead.
</p>

<p>
<strong><em>03/03/2005</em>
<br/>
Barton Mawer WINS at the Australian Grand Prix (<a
href="lindsay/bartonmawer.htm" target="main">more</a>).</strong>
</p>

1) Am I on the right track? Is XML the answer to my problem?
It's one answer, and seemingly an easy one. Of course you will have to
figure how to make your server fire up a stylesheet processor, send a
parameter to it, and send the output to the client -- but that's a
standard technique. You may even leave the transformation to be done by
the client -- but of course nonmembers will still have the member stuff
served (just not displayed). They could see it if they view the page
source code.
2) Is there an easy way of separating the content without having to
parse all the xhtml tags as well as the xml tags???
Not really. Copying a chunk of data involves finding out where the end
of it is. That involves parsing, if it's a section of text in the middle
of a file. But it doesn't hurt all that much.
3) If so, I need to get a better understanding of XML, what is your
suggested next step after reading the W3C tutorial?
an XLST tutorial?

Good luck
Soren
 
P

Peter Flynn

G'day All,
I hope you folk are tolerant of newbies who don't express themselves
well, cause I'm one of those. If not please excuse this and move on.

No problem. We all have to start somewhere.
I'm designing a small web site and while I thought I'd done a good job
of planning the project and learning the required skills I've now
discovered a hole in my plan.

I'm using a template like php script to the build pages.
I'm also using php sessions to authenticate access to members and
guests.
The content component is in xhtml.

Now I'm at the testing stage, I've started to prepare a few of the
xhtml files and realised a lot of the content is an amalgum of 'guest'
and 'member' stuff. I don't really want to have a separate page for
each class of user, with largly identical content.

If you've spotted that unaided then you are already way ahead :)
I'm wondering if I could modify the xhtml files to xml and use tags to
decide what to display?

Certainly. (Just a small terminological point: they're elements, not tags,
see http://www.ucc.ie/xml/#makeup)
i.e. the stuff enclosed by the <member> tag should only be written to a
member session.

<h1>My Page</h1>
<p><em>16/03/2005</em><br />
Sorry, we were offline for a few hours this morning, someone tripped
over the extension lead.
</p>
<member>
<p>
<strong><em>03/03/2005</em>
<br />
Barton Mawer WINS at the Australian Grand Prix (<a
href="lindsay/bartonmawer.htm" target="main">more</a>).</strong>
</p>
</member>

My questions are:
1) Am I on the right track? Is XML the answer to my problem?

Yes, absolutely. I don't know how to do it in PHP, though.
2) Is there an easy way of separating the content without having to
parse all the xhtml tags as well as the xml tags???

No. A parser has to read and digest the whole document from start to finish
(actually that's a lie, there are fragment parsers, but leave them alone
for the moment).

If you were using XSLT, for example, your stylesheet might contain something
like this:

<xsl:choose>
<xsl:when test="$sessiontype='member'">
<xsl:apply-templates select="member"/>
<xsl:when>
<xsl:when test="$sessiontype='guest'">
<xsl:apply-templates select="guest"/>
<xsl:when>
</xsl:choose>

Actually, have a look at attributes. It might be better to say

<h1>My Page</h1>
<p><em>16/03/2005</em><br />
Sorry, we were offline for a few hours this morning, someone tripped
over the extension lead.
</p>
<div class="<p>
<strong><em>03/03/2005</em>
<br />
Barton Mawer WINS at the Australian Grand Prix (<a
href="lindsay/bartonmawer.htm" target="main">more</a>).</strong>
</p>
</div>

and teach your PHP/xml to distinguish between and
3) If so, I need to get a better understanding of XML, what is your
suggested next step after reading the W3C tutorial?

Try the FAQ if you haven't already (www.ucc.ie/xml/) and a good book on
XML for online applications. I can't recommend a specific one, there are
too many, but an hour or so spent browsing them at a large tech bookstore
would be useful.

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top