XML & RDBMS By Molly Gumpert for: LIS 469: XML Fall 2014 Professor Gerry Benoit.

9
XML & RDBMS By Molly Gumpert for: LIS 469: XML Fall 2014 Professor Gerry Benoit

Transcript of XML & RDBMS By Molly Gumpert for: LIS 469: XML Fall 2014 Professor Gerry Benoit.

XML & RDBMSBy Molly Gumpert for:

LIS 469: XML

Fall 2014

Professor Gerry Benoit

Uses

Relational databases allow for easy data storage, manipulation, and visualization and, when make correctly, help avoid errors.

xml files are a good way to make your DB data more interoperable with other systems

Though it can take a be time consuming to produce, once you have a working php script you can turn create xml files of your database data almost instantly.

These xml files are easily updatable when data is added, removed or updated in the database

In turn, websites created with these xml files are immediately updated with changes to the database without having to edit your other scripts

Client/Server Architecture

Database

User

Server

User interacts through an html form

Form runs php script that connects to and

queries database

Result set outputted as a new

xml file

Xml file meets with existing xsl, css, and image files

Formatted and styled xml file sent back to

user’s browser

Xml file

Example

I began with a relational mySQL database designed and built during LIS458.The database serves a fictional art gallery and includes tables for artist information, art pieces, sales, deliveries and more.

html and php

The “View Works” buttons under each artist on the Artist webpage link to three different php files.

The php files are identical in every way except the WHERE statement in the sql query. Each artist is identified by a different “art_id” field$q = "SELECT w.work_id, w.title, w.series, w.medium, w.available, w.price, w.height, w.width, w.image_link FROM works AS w INNER JOIN collections AS c on w.work_id=c.work_id INNER JOIN artists on artists.art_id=c.art_id WHERE artists.art_id='1'";

php script<?phprequire ('mysqli_connect.php');

$q = "SELECT w.work_id, w.title, w.series, w.medium, w.available, w.price, w.height, w.width, w.image_link FROM works AS w INNER JOIN collections AS c on w.work_id=c.work_id INNER JOIN artists on artists.art_id=c.art_id WHERE artists.art_id='1'";

$r = @mysqli_query ($dbc, $q);

if ($r) {

$body = "<?xml version='1.0' encoding='utf-8'?><?xml-stylesheet type='text/xsl' href='catalog.xsl'?><works>";

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

$body .=”<work><work_id>" . $row['work_id'] . "</work_id><title>" . $row['title'] . "</title><series>" . $row['series'] . "</series><medium>" . $row['medium'] . "</medium><available>" . $row['available'] . "</available><price>" . $row['price'] . "</price><dimensions><height>" . $row['height'] . "</height><width>" . $row['width'] . "</width></dimensions><image_link>" . $row['image_link'] . "</image_link></work>";

} } else {print '<p>Error retrieving results</p>';

$body .="</works>";

$resultfile = fopen('resultfile.xml', 'w+');fwrite ($resultfile, $body);header( 'Location:resultfile.xml' );mysqli_close($dbc);?>

• Opens and creates new xml file with the contents specified by $body variable

• Redirects browser to open new xml file

• Closes database connection

• Check for errors (truncated here for space)

• Creating the $body variable by writing tags, calling the sql results that will populate those tags

• Connect to DB (external script is reusable)

Sql query joins three tables to find data on all the artworks by a specified artist

• Variable $r consists of database connection and sql query

• If both are correct, the if statement will run• Writing the xml declaration and root tag

and connecting the existing xsl file

• While statement runs through all the databse records called by the sql query and creates an associative array

resultfile.xml

Without a connected stylesheet the php will output and redirect to a plain xml file.

The same xml file “resultfile.xml” is rewritten everytime the script is run. This means any changes to the database will be reflected in the xml without any other changes elsewhere

$resultfile = fopen('resultfile.xml', ’w');fwrite ($resultfile, $body);header( 'Location:resultfile.xml' );

catalog.xsl*<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><table>

<xsl:for-each select="works/work"><tr><td width="60%">

<ul><li><b><xsl:value-of select="title"/></b></li><li><xsl:if test="series !=' '"><i><xsl:value-of select="series"/> Series</i></xsl:if></li>

<li><xsl:value-of select="medium"/></li> <li><xsl:value-of select="dimensions/height"/>cm h x <xsl:value-of select="dimensions/width"/>cm w</li> <li><xsl:if test="available = '1'">$<xsl:value-of select="price"/></xsl:if></li> <li><xsl:if test="available = '0'"> <p>No longer available for purchase</p> </xsl:if></li>

</ul></td>

<td class="image"><xsl:element name="img">

<xsl:attribute name="src"><xsl:value-of select="image_link"/>

</xsl:attribute></xsl:element>

</td></tr></xsl:for-each>

</table></xsl:template></xsl:stylesheet>

The xsl file follows the Xpath structure to find certain tags and displays the data inside as directed.

<xsl:if> Tags allow for conditionals. Here works that are still available for purchase get special treatment.

Creating an “img” element with a “src” attribute creates an html string to link to images:

<img src=“image_link”>*Some elements have been removed for space. View the entire page here.

Adding CSS

After being transformed with the xsl file, the resulting xml file is still very elementary. More formatting can be done in the xsl, but I prefer to attach a css file to create the final polished look.

View the complete css file here