simpleXml et xpath: evaluation failed

Petit nouveau ! | 2 Messages

23 août 2011, 10:18

Bonjour,
j'essai de parser un fichier xml (issu d'un docx) avec simpleXml. Mon fichier contient pleins de namespaces, et j'utilise xpath pour gérer celà.

voilà ce que je fais:
	if (!file_exists($xml)) 
	{
		echo "Le document XML n'existe pas";
	}
	else
	{
		//chargement du fichier XML
		$fichierXml = simplexml_load_file($xml);

		foreach($fichierXml->xpath('//w:body/w:p') as $r)
		{
			$corps=$r->children("http://schemas.openxmlformats.org/wordprocessingml/2006/main");
			//error_log("corps: $corps");

				foreach($corps->xpath('//w:r') as $rw)
				{
					$wr = $rw->children("http://schemas.openxmlformats.org/wordprocessingml/2006/main");
					
					//error_log("wr: $wr");
					echo "$wr<br>";
				}	
				
		}
	
	}
j'arrive à afficher tout le contenu de mon fichier (j'aurai d'ailleurs une autre question après ce problème par rapport à ça), mais il me fait un warning, et plante au bout de quelques rafraîchissements. Voilà mon error_log:
[23-Aug-2011 09:51:46] PHP Stack trace:

[23-Aug-2011 09:51:46] PHP 1. {main}() C:\wampServer2\www\importation\index.php:0

[23-Aug-2011 09:51:46] PHP 2. SimpleXMLElement->xpath() C:\wampServer2\www\importation\index.php:28

[23-Aug-2011 09:51:46] PHP Warning: SimpleXMLElement::xpath() [<a href='simplexmlelement.xpath'>simplexmlelement.xpath</a>]: xmlXPathEval: evaluation failed in C:\wampServer2\www\importation\index.php on line 28

[23-Aug-2011 09:51:46] PHP Stack trace:

[23-Aug-2011 09:51:46] PHP 1. {main}() C:\wampServer2\www\importation\index.php:0

[23-Aug-2011 09:51:46] PHP 2. SimpleXMLElement->xpath() C:\wampServer2\www\importation\index.php:28

[23-Aug-2011 09:51:46] PHP Warning: Invalid argument supplied for foreach() in C:\wampServer2\www\importation\index.php on line 28

[23-Aug-2011 09:51:46] PHP Stack trace:

[23-Aug-2011 09:51:46] PHP 1. {main}() C:\wampServer2\www\importation\index.php:0

[23-Aug-2011 09:51:46] PHP Warning: SimpleXMLElement::xpath() [<a href='simplexmlelement.xpath'>simplexmlelement.xpath</a>]: Undefined namespace prefix in C:\wampServer2\www\importation\index.php on line 28

[23-Aug-2011 09:51:46] PHP Stack trace:

[23-Aug-2011 09:51:46] PHP 1. {main}() C:\wampServer2\www\importation\index.php:0

[23-Aug-2011 09:51:46] PHP 2. SimpleXMLElement->xpath() C:\wampServer2\www\importation\index.php:28

[23-Aug-2011 09:51:46] PHP Warning: SimpleXMLElement::xpath() [<a href='simplexmlelement.xpath'>simplexmlelement.xpath</a>]: xmlXPathEval: evaluation failed in C:\wampServer2\www\importation\index.php on line 28

[23-Aug-2011 09:51:46] PHP Stack trace:

[23-Aug-2011 09:51:46] PHP 1. {main}() C:\wampServer2\www\importation\index.php:0

[23-Aug-2011 09:51:46] PHP 2. SimpleXMLElement->xpath() C:\wampServer2\www\importation\index.php:28

[23-Aug-2011 09:51:46] PHP Warning: Invalid argument supplied for foreach() in C:\wampServer2\www\importation\index.php on line 28

[23-Aug-2011 09:51:46] PHP Stack trace:

[23-Aug-2011 09:51:46] PHP 1. {main}() C:\wampServer2\www\importation\index.php:0
Et pourtant dans mon document.xml, j'ai bien
 xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" 
Merci de l'aide que vous m'apporterez!!

Petit nouveau ! | 2 Messages

23 août 2011, 10:38

Bonjour!

ça ne me fait plus d'erreur sans que j'aie changé grande chose:
if (!file_exists($xml)) 
	{
		echo "Le document XML n'existe pas";
	}
	else
	{
		//chargement du fichier XML
		$fichierXml = simplexml_load_file($xml);

		foreach($fichierXml->xpath('//w:body/w:p/w:r') as $r)
		{
			$corps=$r->children("http://schemas.openxmlformats.org/wordprocessingml/2006/main");
			//error_log("corps: $corps");

			echo utf8_decode($corps);
			
			echo "<br>";
	
		}
	
	}
J'en profite pour poser une question sur l'accès aux données.
Est-ce que l'accès aux éléments d'un tableau se fait pareil que n'importe quelle balise ou il y a une fonction spéciale qui permet de gérer les tableaux avec simplexml??

Invité
Invité n'ayant pas de compte PHPfrance

24 août 2011, 10:07

Bonjour,
Pas de réponse? Je cherche vraiment à sélectionner le contenu d'une balise précise qui n'a pas d'identifiant:

Code : Tout sélectionner

<w:r><w:rPr><w:b/><w:noProof/></w:rPr> <w:instrText>réconisations de développement générales</w:instrText> </w:r><w:r><w:rPr><w:color w:val="FFFFFF"/><w:sz w:val="4"/></w:rPr><w:fldChar w:fldCharType="end"/></w:r><w:r><w:rPr><w:color w:val="FFFFFF"/><w:sz w:val="4"/></w:rPr> <w:instrText xml:space="preserve"> \* MERGEFORMAT </w:instrText></w:r><w:r><w:rPr><w:color w:val="FFFFFF"/><w:sz w:val="4"/></w:rPr><w:fldChar w:fldCharType="separate"/></w:r> <w:r><w:rPr><w:color w:val="FFFFFF"/><w:sz w:val="4"/></w:rPr> <w:t>Normes de gestion des équipes</w:t> </w:r><w:r><w:rPr><w:color w:val="FFFFFF"/><w:sz w:val="4"/></w:rPr><w:fldChar w:fldCharType="end"/></w:r> </w:p> </w:tc>
j'aimerai récupérer le texte
Normes de gestion des équipes
.

j'ai essayé:
foreach ($corps->xpath('//w:tbl/w:tr/w:tc/w:p/w:r') as $tab)
			{ 
				$wp=$tab->children("http://schemas.openxmlformats.org/wordprocessingml/2006/main");
				 				 
				if ($wp->getName() != "instrText")
				{
					$tabIdentification = $wp[1];
					if ($tabIdentification == "Identification")
					{	
						echo utf8_decode($tabIdentification);
					
						foreach ($corps->xpath('//w:tbl/w:tr/w:tc/w:p/w:r') as $tab)
						{
							$texte = $tab[9];  //le texte se trouve dans le 9ième <w:r>
							//echo $neuf = $tab->children("http://schemas.openxmlformats.org/wordprocessingml/2006/main");
							echo $texte."<br>";
						}
					}
				}
				 
				// echo $baliseTab = $wp->getName();
						
				echo "<br>";
			}
mais ça ne marche pas.

Et comment on fait pour ne pas que le texte compris dans
<w:instrText xml:space="preserve"> texte à ne pas afficher</w:instrText>  
ne s'affiche pas lors de la visualisation?

:(