How to insert RDF content into a Web site in PHP (v)

Source: Internet
Author: User
Tags functions html page html tags include insert net php class php script
web| Insert | Site free lunch (A lunch)

The class I wrote above is also very basic, is to illustrate the problem, perhaps also can be used for low traffic site. If you're looking for something more professional, go online, there are many open source RDF parsers with a variety of additional features (including caching). So let's look at some examples of how to use these analyzers.

The first one to talk about is the RDF parser class developed by Stefan Saasen for the FASE4 Web site, which can be downloaded free of charge from http://www.fase4.com/rdf/. This is a full-featured RDF parser that supports caching and proxy authentication. Here is an example of how to use it:

<style type= "Text/css" >
body {font-family:verdana; font-size:11px;}
. FASE4_RDF {font-size:13px Font-family:verdana}. Fase4_rdf_title
{font-size:13px; font-weight:bolder;}
</style>
<body>
?
Include class
Include ("rdf.class.php");

Instantiate object
$RDF = new Fase4_rdf;

Set number of items to display
$RDF->set_max_item (5);

Set RDF engine options
$RDF->use_dynamic_display (TRUE);
$RDF->set_options (Array ("image" => "hidden", "TextInput" => "hidden"));

Parse and display data
$RDF-&GT;PARSE_RDF ("HTTP://WWW.FRESHMEAT.NET/BACKEND/FM-RELEASES.RDF");
$RDF->finish ();
?>
</body>


Another introduction is the PHP RDF parser developed by Jason Williams, which can be downloaded in http://www.nerdzine.net/php_rdf/. This is a PHP class without any rendering, and implements some basic methods. But it contains a lot of attributes that allow you to schedule processed data until you are satisfied.

--------------------------------------------------------------------------------
<basefont face= "Verdana" >
<body link= "Red" vlink= "red" alink= "Red" >
?
Include ("rdf_class.php");

This is needs to is a local file
$f = new Rdffile ("./fm-releases.rdf");
$f->parse (True);
$f->returntable (True, "Black", "white", "100%");
?>
</body>



Documents about these classes are presented on their respective websites.

Add a little style (adding a Little style)

In case you hate iterating through those php arrays and tagging them in HTML, you can also choose to format and display the data by using an XSLT style sheet. PHP4 1 can support the Sablotron XSLT processor with the new XSLT APIs, which can be used to merge an XSLT style sheet and an XML document (here, the RDF file), making it very easy to translate XML tags into browser-readable HTML tags.

I don't want to be too thin on this, you can take a look at the PHP manual, or focus on the links later in this article for more detailed information. However, I will give a simple example to illustrate this problem. First, give the style sheet file:

<?xml version= "1.0"?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform"
xmlns:rdf= "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
Xmlns:rss= "http://purl.org/rss/1.0/"
Xmlns:dc= "HTTP://PURL.ORG/DC/ELEMENTS/1.1/" version= "1.0" >

<!--Main Page-->
<xsl:template match= "/RDF:RDF" >
<basefont face= "Arial" size= "2"/>
<body>
<xsl:apply-templates select= "Rss:channel"/>
<ul>
<xsl:apply-templates select= "Rss:item"/>
</ul>
</body>
</xsl:template>

<!--Channel-->
<xsl:template match= "Rss:channel" >
<b>
<a>
<xsl:attribute name= "href" ><xsl:value-of select= "Rss:link"
/></xsl:attribute>
<xsl:value-of select= "Rss:title"/>
</a>
</b>
</xsl:template>

<!--item-->
<xsl:template match= "Rss:item" >
<li/>
<a>
<xsl:attribute name= "href" ><xsl:value-of select= "Rss:link"
/></xsl:attribute>
<xsl:value-of select= "Rss:title"/>
</a>
<br/>
<xsl:value-of select= "Rss:description"/>
</xsl:template>
</xsl:stylesheet>
Here is a PHP script to combine the above style sheet with the "Fresh Meat" RDF document to generate an HTML page:

<?php
XML file
This is needs to is a local file
$xml = "FM-RELEASES.RDF";

XSLT file
$XSLT = "fm.xsl";

Create a new XSLT processor
$XP = Xslt_create ();

Transform the XML file as per the XSLT stylesheet
Return to $result
$result = Xslt_process ($xp, $xml, $XSLT);
if ($result)
{
Print it
echo $result;
}

Clean up
Xslt_free ($XP);
?>

I think it is quite simple and needless to say. Two of documents are merged together to produce the following "synthetics":

This is another way to convert RDF data into browser-readable HTML, which may be simpler (though not ideal). Note, however, that you need to run an external program to periodically update your local copy of your RDF file because the PHP XSLT processor may have difficulty accessing the remote file.

Homework (homework)

If you are interested in learning more about the techniques mentioned in these texts and not talked about, you may consider accessing the following links:

RSS 1.0 Spec: http://www.purl.org/rss/1.0/
RSS History: http://backend.userland.com/stories/rss091

Web site about RDF content: http://www.w3.org/RDF/

Discussion of sax and DOM programming in PHP: Http://www.devshed.com/Server_Side/XML/XMLwithPHP

Discussion of implementing XSLT transformations in PHP: Http://www.devshed.com/Server_Side/XML/XSLTrans

Discussion of PHP classes: Http://www.devshed.com/Server_Side/PHP/BackToClass

Discussion of XML Basics: Http://www.devshed.com/Server_Side/XML/XMLBasic

Discussion of the XSLT basics: http://www.devshed.com/Server_Side/XML/XSLBasics

What's in the PHP manual about sax functions: http://www.php.net/manual/en/ref.xml.php

What's in the PHP manual about XSLT functions: http://www.php.net/manual/en/ref.xslt.php

See you next time ... Take care of yourself!

Note: All of the examples in this article are tested in linux/i386,apache1.3.12,,php4.1.1 environments.

These examples are used only to illustrate the problem, not for a particular product. The Melonfire website does not provide any authorization or support for the source code in the text. (This article copyright for Melonfire website All, original source: http://www.devshed.com/Server_Side/PHP/PHPRDF/page1.html)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.