Page 1 sur 2

parser xml xsl

Posté : 10 janv. 2011, 17:45
par hypo
bonjour et bonne année à tout le monde :)

j'ai un souci avec un script, il ne fonctionne que si les fichiers xml et xsl sont hébergés sur mon seveur.

Je souhaiterais que cela fonctionne avec une adresse Url exterieur ou sont stockés les fichiers xml.

voici le bout de code qu'il faudrait modifié :

Code : Tout sélectionner

if ($xml !== false) { $path = ABSPATH; $xml = $path . $xml; $xslt = $path . $xslt; if (file_exists($xml) && file_exists($xslt)) { $xslDoc = new DOMDocument(); $xslDoc->load($xslt); $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); $proc = new XSLTProcessor();
Voici une réponse que l'on ma donné, mais je ne sais pas comment le codé en dur dans le script :
Yes it is possible if you modify the code of the processor. At the moment the path is hardcoded on line 126, it can be made so that if the http:// string is present in the xml / xsl file path then said path is not modified.
It would go something like this:
If http:// is present in $xml then do nothing, otherwise, $xml = $path.$xml
if http:// is present in $xsl then do nothing, otherwise, $xsl = $path.$xsl

The modification necessary should be around line 126/127
Merci par avance de votre aide.

Cordialement.

Re: parser xml xsl

Posté : 10 janv. 2011, 17:52
par stealth35
lorsque que tu changes des fichiers externe a quoi correspond ?
$path = ABSPATH;
$xml  = $path . $xml;
$xslt = $path . $xslt;

Re: parser xml xsl

Posté : 10 janv. 2011, 18:00
par hypo
lorsque que tu changes des fichiers externe a quoi correspond ?
$path = ABSPATH;
$xml  = $path . $xml;
$xslt = $path . $xslt;
si je mets l'url distante j'ai cette erreur :

Code : Tout sélectionner

Error processing XML and XSLT. Configuration was: Array ( [xml] => http://lecomarquage.service-public.fr/xml2v2/N358.xml [xslt] => servicepublic/Stylesheets/spNoeud.xsl [params] => XMLURL=http://lecomarquage.service-public.fr/xml2v2/|REFERER=?|PIVOTS=|CATEGORIE=particuliers|IMAGES=http://p650.phpnet.org/servicepublic/images/ )

Re: parser xml xsl

Posté : 10 janv. 2011, 18:13
par stealth35
mais ton code il sort d'où ?

Re: parser xml xsl

Posté : 10 janv. 2011, 18:19
par hypo
mais ton code il sort d'où ?
<?php

if(!isset($_GET[xml])){
$xml="http://lecomarquage.service-public.fr/xml2v2/N358.xml";
}
else{
$xml="http://lecomarquage.service-public.fr/xml2v2/".$_GET[xml];
}
if(!isset($_GET[xsl])){
$xsl="servicepublic/Stylesheets/spNoeud.xsl";
}
else{
$xsl="servicepublic/Stylesheets/".$_GET[xsl];
}
 
$REFERER="?"; 
$IMAGES=get_bloginfo('url')."/servicepublic/images/";
$XMLURL="http://lecomarquage.service-public.fr/xml2v2/";
$CATEGORIE="particuliers"; 
$PIVOTS=""; 

echo (do_shortcode('[XmlProcessor xml="'.$xml.'" xslt="'.$xsl.'" params="XMLURL='.$XMLURL.'|REFERER='.$REFERER.'|PIVOTS='.$PIVOTS.'|CATEGORIE='.$CATEGORIE.'|IMAGES='.$IMAGES.'"]'));


?> 
et voici le script XMLProcessor :
<?php
/*
Plugin Name: Xslt Processor
Plugin URI: no uri at the moment
Description: The plugin aims to allow XML document transformed through XSLT to be outputted in a WordPress Page. It also now allows to pass parameters to the XSL doc
Version: 0.5
Author: Chryzo, Hakre
Author URI: http://www.chryzo.net

Copyright 2009  Chryzo  (email : [email protected])

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/*
usage:

	[XmlProcessor xml="xml_filepath" xslt="xslt_filepath"]
	
		xml_filepath and xslt_filepath is relative to wordpress root directory

	[XmlProcessor filepath]
	
		filepath stands for both filenames (.xml and .xsl) without the
		file extension and relative to wordpress root directory

	[XmlProcessor filepath get="key|ns=key" post="key|ns=key"]

		get / post stands for the parameters that need to be passed to the xsl document but are taken from the HTML methods

examples without parameters:

	[XmlProcessor wp-content/plugins/CPT_XslProcessor/example]
	
	[XmlProcessor xml="wp-content/plugins/CPT_XslProcessor/example.xml" xslt="wp-content/plugins/CPT_XslProcessor/example.xsl"]
	
		Both Examples work the same, they will process example.xml and
		example.xsl located in the wordpress root directory.

examples with parameters:

	[XmlProcessor wp-content/plugins/CPT_XslProcessor/example]
	
	[XmlProcessor xml="wp-content/plugins/CPT_XslProcessor/example.xml" xslt="wp-content/plugins/CPT_XslProcessor/example.xsl"]
	
		Both Examples work the same, they will process example.xml and
		example.xsl located in the wordpress root directory.	
		

*/

add_option("CPT_Path", "");
add_option("CPT_Separator", ",");

/**
 * XmlProcessor Option retrieval
 *
 *	XML - XSLT Processor
 *
 *
 * @return array list of path to look inside to retrieve files
 */
function cpt_retrieve_option() {
	$paths = get_option("CPT_Path");
	$separator = get_option("CPT_Separator");
}
 

/**
 * XmlProcessor Shortcode Hook
 * 
 * XML - XSLT Processor
 * 
 * @param  array $atts shortcode attributes
 * @return string output of shortcode(processed xml/xslt or error message on failure)
 */
function cpt_process_xml($atts) {
	
	$options = null;
	$get =null;
	$post = null;
	$namespace = null;
	$xml = false;
	$xslt = false;
	$paramSep = "|";
	$keyvalSep = "=";
	
	if (isset($atts[0])) {						// one parameter for naming both files
		$xml  = sprintf('%s.xml', $atts[0]);
		$xslt = sprintf('%s.xsl', $atts[0]);
	} elseif (isset($atts["xml"]) && isset($atts["xslt"])) {	// name xml and xslt files with an attribute on it's own.
		$xml = $atts["xml"];
		$xslt = $atts["xslt"];
	}
	
	// This is a separate section because we can't use the short format of the filepath in this case since the parameters may have
	// some blanks in them which would screw around with the parsing
	if (isset($atts["params"])){				// parameter for the parameters of the xsl file
		$options = $atts["params"];
	}
	if (isset($atts["get"])){
		$get = $atts["get"];
	}
	
	if (isset($atts["post"])){
		$get = $atts["post"];
	}
		
	if ($xml !== false)
	{	
		// this will include relative to this file
		//$path = dirname(__FILE__);
		// this will include relative to this wordpress root folder
		$path = ABSPATH;
		
		$xml  = $path . $xml;
		$xslt = $path . $xslt;
			
		if (file_exists($xml) && file_exists($xslt))
		{
			$xslDoc = new DOMDocument();
			$xslDoc->load($xslt);
			
			$xmlDoc = new DOMDocument();
			$xmlDoc->load($xml);
	
			$proc = new XSLTProcessor();
			// setting parameters section
			// /*
			if ($options != null) {
				$options = explode($paramSep,$options);
				//print_r(count($options));
				for ($i = 0; $i< count($options); $i++){
					$pair = explode($keyvalSep, $options[$i]);
					if ( count($pair) == 1 ) {
						$proc->setParameter(null, $pair[0], "");			// just a key, then we blank the value of the parameter
					} elseif ( count($pair) == 2 ) {
						//print_r($pair);
						//$proc->setParameter(null, 'default_param', 'test');		// just a key/value pair, we replace
						$proc->setParameter(null, $pair[0], $pair[1]);		// just a key/value pair, we replace
					} elseif (count($pair) == 3 ) {
						$proc->setParameter($pair[0], $pair[1], $pair[2]);	// a namespace/key/value set, we replace
					}
				}
			}
			if ($get != null) {
				$get = explode($paramSep,$get);
				for ($i = 0; $i< count($get); $i++){
					$pair = explode($keyvalSep, $get[$i]);
					if ( count($pair) == 1 ) {
						$proc->setParameter(null, $pair[0], $_GET[$pair[0]]);	// just a key, then we blank the value of the parameter
					} elseif ( count($pair) == 2 ) {
						$proc->setParameter(null, $pair[0], $_GET[$pair[1]]);	// just a key/value pair, we replace
					} elseif (count($pair) == 3 ) {
						$proc->setParameter($pair[0], $pair[1], $_GET[$pair[2]]);// a namespace/key/value set, we replace
					}
				}
			}
			if ($post != null) {
				$post = explode($paramSep,$post);
				for ($i = 0; $i< count($post); $i++){
					$pair = explode($keyvalSep, $post[$i]);
					if ( count($pair) == 1 ) {
						$proc->setParameter(null, $pair[0], $_POST[$pair[0]]);	// just a key, then we blank the value of the parameter
					} elseif ( count($pair) == 2 ) {
						$proc->setParameter(null, $pair[0], $_POST[$pair[1]]);	// just a key/value pair, we replace
					} elseif (count($pair) == 3 ) {
						$proc->setParameter($pair[0], $pair[1], $_POST[$pair[2]]);// a namespace/key/value set, we replace
					}
				}
			}
			// */
			// end of parameter section
			$proc->importStylesheet($xslDoc);			
			return $proc->transformToXML($xmlDoc);		
		}
	}
	
	return sprintf('<div class="error"><strong>Error processing XML and XSLT. Configuration was: %s</strong></div>', htmlspecialchars(print_r($atts, true)));	
}

if (function_exists("add_shortcode")){
	add_shortcode("XmlProcessor", "cpt_process_xml");
}

?>
c'est ce 2ème qui faudrait modifier

Re: parser xml xsl

Posté : 10 janv. 2011, 18:26
par stealth35
fait :
$path = ABSPATH;
               
$xml  = $path . $xml;
$xslt = $path . $xslt;

var_dump($path, $xml, $xslt);
exit;

Re: parser xml xsl

Posté : 10 janv. 2011, 18:30
par hypo
j'ai cette erreur qui s'affiche suite à la modification :

Code : Tout sélectionner

string(10) "/home/www/" string(63) "/home/www/http://lecomarquage.service-public.fr/xml2v2/N358.xml" string(47) "/home/www/servicepublic/Stylesheets/spNoeud.xsl"

Re: parser xml xsl

Posté : 10 janv. 2011, 18:44
par stealth35
essaye comme ca
$path = ABSPATH;
                
if(!preg_match('/^https?:\/\//', $xml))
{
    $xml  = $path . $xml;
}
                             
if(!preg_match('/^https?:\/\//', $xslt))
{
    $xslt = $path . $xslt;
}

Re: parser xml xsl

Posté : 10 janv. 2011, 18:51
par hypo
voici l'erreur affiché désormais...

Code : Tout sélectionner

Error processing XML and XSLT. Configuration was: Array ( [xml] => http://lecomarquage.service-public.fr/xml2v2/N358.xml [xslt] => servicepublic/Stylesheets/spNoeud.xsl [params] => XMLURL=http://lecomarquage.service-public.fr/xml2v2/|REFERER=?|PIVOTS=|CATEGORIE=particuliers|IMAGES=http://p650.phpnet.org/servicepublic/images/ )

Re: parser xml xsl

Posté : 10 janv. 2011, 18:53
par stealth35
montre avec le var_dump() plutôt

EDIT : le problème doit venir du file_exists ca marche pas avec les URL
if(!preg_match('/^https?:\/\//', $xml))
                {
                	$xml  = $path . $xml;
                	$xml_exist = file_exists($xml);
                }
                else
                {
                	$xml_exist = true;
                }
                             
                if(!preg_match('/^https?:\/\//', $xslt))
                {
                	$xslt = $path . $xslt;
                	$xml_exist = file_exists($xslt);
                }
                else
                {
                	$xslt_exist = true;
                }
                
                       
                if ($xml_exist && $xslt_exist)

Re: parser xml xsl

Posté : 10 janv. 2011, 18:56
par hypo
string(10) "/home/www/" string(53) "http://lecomarquage.service-public.fr/xml2v2/N358.xml" string(47) "/home/www/servicepublic/Stylesheets/spNoeud.xsl"

Re: parser xml xsl

Posté : 10 janv. 2011, 19:01
par stealth35
string(10) "/home/www/" string(53) "http://lecomarquage.service-public.fr/xml2v2/N358.xml" string(47) "/home/www/servicepublic/Stylesheets/spNoeud.xsl"
j'ai éditer mon message au dessus :wink:

visiblement les paths on l'air corrects non ?

Re: parser xml xsl

Posté : 10 janv. 2011, 19:24
par hypo
voici suite au changement :

Code : Tout sélectionner

string(10) "/home/www/" string(53) "http://lecomarquage.service-public.fr/xml2v2/N358.xml" string(47) "/home/www/servicepublic/Stylesheets/spNoeud.xsl"

Re: parser xml xsl

Posté : 10 janv. 2011, 19:25
par stealth35
et toujours la même erreurs ?

Re: parser xml xsl

Posté : 10 janv. 2011, 19:27
par hypo
oui malheureusemennt :(