RJP43 / CitySlaveGirls

The Restoration of Nell Nelson
http://nelson.newtfire.org
5 stars 4 forks source link

Using PHP to transform XML to HTML with XSLT #59

Open adammparker opened 8 years ago

adammparker commented 8 years ago

PHP: Manual

The process for transformation looks like this:

  1. Create a PHP file that accepts 2 parameters from the URL a) XML document location b) XSL document location
  2. Use the DOMDocument object for both files using the load function
    $xmlDocument = new DOMDocument;
    $xmlDocument->load($xmlParameter);
  3. Then use a XSLTProcessor object to generate the HTML output
    $processor = new XSLTProcessor;
    $processor->importStylesheet($xslDocument);
    $processor->transformToURI($xmlDocument, 'file:///output.html');

The above example assumes the XML, XSL, and PHP file are all in the same directory on the web server. You also need to ensure that the XSL module is installed and enabled in PHP on the web server as well.(not enabled by default).

Note: You could then use AJAX to return the PHP directly to the current web page without having to send the end-user to another web page after they click the link.

Full PHP code example below:

<?php 

$xmlParam = $_GET["xml"];
$xslParam = $_GET["xsl"];

$xml = new DOMDocument;
$xml->load($xmlParam);

$xsl = new DOMDocument;
$xsl->load($xslParam);

$proc = new XSLTProcessor;
$proc->importStylesheet($xsl);

$proc->transformToURI($xml, 'file:///var/www/html/out.html');

echo file_get_contents('out.html');

?>

Call the php file with a URL that looks like: /transform.php?xml=xmlFile.xml&xsl=xslFile.xsl

adammparker commented 8 years ago

Also if you want to see a fully functioning version of this (except for the PHP code obviously which is on the server) you can visit:

presidential.obdurodon.org

RJP43 commented 8 years ago

@ebeshero this is the basics of what my brother taught me ... I hope to experiment with this further in preparation for our Oakland Presentations.

ebeshero commented 8 years ago

@RJP43 @Ampzilla Thanks for the PHP help! I am just learning this myself. :+1:

RJP43 commented 8 years ago

@ebeshero's idea for PHP integrated into SVG from Cytoscape

grabbing the elements controlling particular nodes or edges with JavaScript, and firing a script that that would grab and post the associated file information, and pull up some relevant content from that file formatted in HTML in the same window

Moved this from other issue to keep all PHP stuff together!

RJP43 commented 8 years ago

@spadafour and I were discussing creating several xslts (@Ampzilla 's suggestion) that all have a different focus (ie. versioning, gender, grammatical) for the markup so that users could choose what kind of marked up text they want to see

more to come on this soon!

ebeshero commented 8 years ago

@RJP43 @spadafour @Ampzilla For implementing PHP on the CitySlaveGirls site at the PSC, here's what here's what I've learned, and what I recommend

Does that make sense as a battle plan? I'm pinging David Birnbaum, who's done things like this before. @djbpitt

djbpitt commented 8 years ago

This is the PHP I use when users click on a tale title at http://aal.obdurodon.org (remove the ".txt" extension; GitHub made me add that). Let me know if anything is unclear. The basic logic is that when a user asks for a tale (to be served in HTML), after a lot of security checking, it checks whether the HTML exists. If not, it generates it, saves it, and serves it. If it does exist, it compares the dates of the HTML and the corresponding XML. If the HTML is newer, it serves it. If not, that means that the XML has been updated since the last access, so it regenerates the HTML, caches it, and serves it. The point of all of this is that I just upload revised XML whenever I have it, and I don't have to remember to create or recreate the HTML manually each time I do that.

readTale.php.txt

djbpitt commented 8 years ago

The security risk with the approach I describe above is that PHP exec() can execute malicious code if a user knows how to fool it. For that reason, some systems that run PHP disable the exec() function. If you need it, you don't have to be afraid of it, but you should sanitize any user input carefully before passing it to exec(). If you're already running eXist for other purposes, the idea of going through that, as Elisa describes, makes good sense.

RJP43 commented 8 years ago

@djbpitt My brother ( @Ampzilla ) and I attempted to use your PHP code discussed above on the Nelson site sitting on Obdurodon; however, seems that since the folder owner is not apache my exec function in PHP can't write the html output file. The error message we are getting is: ( [0] => Error on line 8 of readingView.xsl: [1] => Failed to create output file [2] => file:/var/www/html/nell/articles/html/1888-07-30-ChTimes.html: Permission denied [3] => Failed to create output file file:/var/www/html/nell/articles/html/1888-07-30-ChTimes.html )

I attempted to change ownership of the necessary directories, but cannot do that through FTP (at least as rjp43).

You can view our edited version of your PHP code and all of the site code here and see the error live on the site (as of 8/13 4:15PM) here.

So what I'm asking is for you to change ownership of the articles folder in the Nelson site to be changed to ownership and for the properties to have group write power so I can still access/change. Or perhaps this is a larger ((private?) conversation we need to have about permission structure for the Nelson site sitting on Obdurodon. Open for any and all suggestions on what to do here and your time is always much appreciated!

We were hoping to first get the site up and running on Obdurodon and then find out how to approach PSC to get it working there (have the Saxon9HE.jar installed there and obtain permissions).