How to convert XML using XSLT using PHP in Wordpress

I am currently converting an XML document with an XSLT stylesheet using Javascript (on the Wordpress website). This works fine in Firefox and Chrome, but not in IE. Also, if Javascript is not enabled, nothing will appear.

So my goal is to convert XML / XSLT to XHTML on the server, not the client, preferably using PHP.

I have tried many different PHP scripts written by other people (I am new), but I cannot get them to work. I included the simplest PHP script I found below. I know that a dynamic file path can be a problem, but I donโ€™t know how best to find the XML and XSLT files.

When I use the script below, I get the error: Syntax error: syntax error, unexpected T_STRING in / home / alan / public _html / wp-content / themes / Stacked / page-renting.php on line 42

Alternative solutions are also welcome.

<?php $xml = new DOMDocument(); $xml->load('<?php bloginfo('template_directory'); ?>/rentals/works.xml'); $xsl = new DOMDocument; $xsl->load('<?php bloginfo('template_directory'); ?>/rentals/works.xsl'); $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); ?> 
+4
source share
4 answers

You just need to replace this PHP bit in the correct context, like this:

 $xml = new DOMDocument; $xml->load(get_bloginfo('template_directory') . '/rentals/works.xml'); $xsl = new DOMDocument; $xsl->load(get_bloginfo('template_directory') . '/rentals/works.xsl'); $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); 
+3
source

I decided.

I tried the above suggestions of Josh and Rubens, but the xml and xsl documents are still not found. But from Joshโ€™s idea of โ€‹โ€‹another way to access the template directory, I searched Google a bit and found this solution:

Here's the final PHP script I used to convert XML from XSLT to the server using PHP. Thanks to everyone who helped.

 <?php $xml = new DOMDocument; $xml->load('./wp-content/themes/Stacked/rentals/WORKS.xml'); $xsl = new DOMDocument; $xsl->load('./wp-content/themes/Stacked/rentals/WORKS.xsl'); $proc = new XSLTProcessor; $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); ?> 

Two key things that make it work:

  • Using the period and file paths as an alternative to the usual Wordpress methods that I used before.

  • Case sensitive. My file names were uppercase (not reasonable, I know). Since the file paths are usually not case sensitive, I did not think about it, but it turns out that in this case (when inside the PHP script?), Using the correct case for BOTH, the topic name (Stacked) and the file name (WORKS.xml, WORKS.xsl) is necessary to find the file correctly.

+2
source

Another way would be to not use XSLT at all, but instead a plugin that converts XML using a simple premium. See this plugin .

+1
source

You must delete this information on the blog; this download method gets your XML / XSLT file names.

 $xml->load('/rentals/works.xml'); $xsl->load('/rentals/works.xsl'); 

Of course, they should indicate the correct path to your XML / XSLT files.

0
source

Source: https://habr.com/ru/post/1299265/


All Articles