Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using PHP to transform XML to HTML with XSLT #59

Open
adammparker opened this issue Apr 9, 2016 · 9 comments
Open

Using PHP to transform XML to HTML with XSLT #59

adammparker opened this issue Apr 9, 2016 · 9 comments

Comments

@adammparker
Copy link

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
Copy link
Author

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
Copy link
Owner

RJP43 commented Apr 11, 2016

@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
Copy link
Collaborator

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

@RJP43
Copy link
Owner

RJP43 commented Apr 14, 2016

@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
Copy link
Owner

RJP43 commented Apr 14, 2016

@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
Copy link
Collaborator

ebeshero commented Apr 29, 2016

@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

  • In order to make XSLT execute from within the Apache web server, we'd need to install a special program there, something so that Java can execute the saxon XSLT transformation. It's probably a security risk for the PSC, and it's something we'd need to inquire about
  • Here's what we can do now, and maybe it's better: You can save your XSLT transformations in eXist directories, so that you can call them in eXist (the same we we get a REST address for your XQuery output). eXist-db can run XSLT as well as XQuery, so you could write PHP to call the XSLT file where it's saved in eXist, and make it run a transformation, and access the output from an output directory you set up in eXist. Here's how that could work:
  • Write XQuery that uses the transform(transform) function (we can read up on this)--because that's how you make XQuery execute XSLT, and set up a place in eXist to save your XSLT and your output.
  • Write your PHP to call the XQuery (that calls the XSLT and makes the output!
  • One more thing you can do (once this is working) is do a check in your output directory to see if an HTML file was already generated and that its date is the same as your most recent XML input. If its date is older, you can run the XSLT again and make new output. If it's the same date, just pull in the existing HTML so you don't have to run the transformation (which should speed up site access for your users).

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

@djbpitt
Copy link

djbpitt commented Apr 29, 2016

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
Copy link

djbpitt commented Apr 29, 2016

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
Copy link
Owner

RJP43 commented Aug 13, 2016

@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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants