Get xml-lang value ?

Code46

Erfahrenes Mitglied
Hi leute,
ich versuche in diesem XML Baum die Werte die auf Deutsch sind also xml-kang="de" zu bekommen. Zum Beispiel habe ich versucht "Schwarz" zu lesen, jedoch ist dies gescheitert.

Code:
<ad:specifics>
  <ad:exterior-color key="BLACK">
     <resource:local-description xml-lang="de">Schwarz</resource:local-description> 
     <ad:metalic value="true"/>
     <ad:manufacturer-color-name value="Obsidianschwarzmetallic"/>
   </ad:exterior-color><ad:mileage value="94658"/>
   <ad:general-inspection value="2014-11"/>
   <ad:first-registration value="2011-11"/>
   <ad:emission-class key="EURO5">
      <resource:local-description xml-lang="de">Euro5</resource:local-description>
   </ad:emission-class>
   <ad:emission-fuel-consumption envkv-compliant="false" co2-emission="154.00" inner="7.80" outer="4.40" combined="5.80" unit="LITER_PER_100_KM"/><ad:fuel key="DIESEL">
     <resource:local-description xml-lang="de">Diesel</resource:local-description>
   </ad:fuel>
    <ad:power value="125"/>
    <ad:gearbox key="AUTOMATIC_GEAR">
        <resource:local-description xml-lang="de">Automatik</resource:local-description>
    </ad:gearbox>
    <ad:climatisation key="AUTOMATIC_CLIMATISATION">
          <resource:local-description xml-lang="de">Klimaautomatik</resource:local-description>
     </ad:climatisation>
   <ad:cubic-capacity value="2143"/><ad:condition key="USED">
      <resource:local-description xml-lang="de">Gebrauchtfahrzeug</resource:local-description>
   </ad:condition><ad:number-of-previous-owners>1</ad:number-of-previous-owners>
</ad:specifics>

Hier sind zwei verschiedene Codes die ich ausprobiert habe.

Code:
$xPath = new DOMXPath($dom);
$nodes = $xPath->query('//specifics/exterior-color/resource:local-description[@xml:lang = "de"]');	
	
  foreach ($nodes as $tag) 
  {
     echo $tag->nodeValue . '<br />';
	

  }


UND

Code:
foreach ($result as $elem) 
		{
			
		foreach ($elem->childNodes as $elems) 
		{
			
	      $exterior_color = $elems->getElementsByTagName("exterior-color")->item(0)->nodeValue;

echo $exterior_color;

}
}

Bei deim ersten code kommt nichts heraus, also eine leere Seite. Bei dem Zweiten bekomme ich "BLACK".

Wie könnte ich dieses Problem lösen. Wenn ihr mir helfen könntet würde ich mich sehr freuen.

Danke
 
Hi,

versuch mal das:

XML:
<?xml version="1.0" encoding="UTF-8"?>
<ad:specifics xmlns:ad="http://www.w3.org/2001/XMLSchema" xmlns:resource="http://www.w3.org/2001/XMLSchema">
  <ad:exterior-color key="BLACK">
     <resource:local-description xml-lang="de">Schwarz</resource:local-description> 
     <ad:metalic value="true"/>
     <ad:manufacturer-color-name value="Obsidianschwarzmetallic"/>
   </ad:exterior-color><ad:mileage value="94658"/>
   <ad:general-inspection value="2014-11"/>
   <ad:first-registration value="2011-11"/>
   <ad:emission-class key="EURO5">
      <resource:local-description xml-lang="de">Euro5</resource:local-description>
   </ad:emission-class>
   <ad:emission-fuel-consumption envkv-compliant="false" co2-emission="154.00" inner="7.80" outer="4.40" combined="5.80" unit="LITER_PER_100_KM"/><ad:fuel key="DIESEL">
     <resource:local-description xml-lang="de">Diesel</resource:local-description>
   </ad:fuel>
    <ad:power value="125"/>
    <ad:gearbox key="AUTOMATIC_GEAR">
        <resource:local-description xml-lang="de">Automatik</resource:local-description>
    </ad:gearbox>
    <ad:climatisation key="AUTOMATIC_CLIMATISATION">
          <resource:local-description xml-lang="de">Klimaautomatik</resource:local-description>
     </ad:climatisation>
   <ad:cubic-capacity value="2143"/><ad:condition key="USED">
      <resource:local-description xml-lang="de">Gebrauchtfahrzeug</resource:local-description>
   </ad:condition><ad:number-of-previous-owners>1</ad:number-of-previous-owners>
</ad:specifics>

PHP:
<?php
$dom = new DOMDocument();
$dom->load('test.xml');

$xpath = new DOMXPath($dom);

$childs = $xpath->query('/ad:specifics/ad:exterior-color/resource:local-description[@xml-lang="de"]');

foreach ($childs as $child)
{
	//var_dump($child);
	echo $child->textContent;
}
 
Die XML datein kann ich leider nicht änder, es ist eine API die ich aus einem http link lese.

Das sind die Fehler die ich bekomme

Warning: DOMXPath::query() [domxpath.query]: Undefined namespace prefix in /var/www/web63/html/test/one.php on line 11

Warning: DOMXPath::query() [domxpath.query]: Invalid expression in /var/www/web63/html/test/one.php on line 11

Warning: Invalid argument supplied for foreach() in /var/www/web63/html/test/one.php on line 13
 
Hallo,

Dein Fehler befindet sich im Prädikat des query-Arguments.
  • Falsch: resource:local-description[@xml:lang = "de"]
  • Vermutlich besser: resource:local-description[@xml-lang = "de"]
 
Dann halt mit Brutalität:

PHP:
<?php
$fileContents = file_get_contents('test.xml');
$fileContents = str_replace('ad:', '', $fileContents);
$fileContents = str_replace('resource:', '', $fileContents);

$dom = new DOMDocument();
$dom->loadXML($fileContents);

$xpath = new DOMXPath($dom);

$childs = $xpath->query('/specifics/exterior-color/local-description[@xml-lang="de"]');
foreach ($childs as $child)
{
	//var_dump($child);
	echo $child->textContent;
}
 
Bekomme dies hier:
file_get_contents(<?xml version="1.0" encoding="UTF-8" standalone="yes"?><search:result xmlns:resource="http://">
 
Noch eine Ergänzung: Wenn man schon im XML-Datenstrom rumpfriemelt, kann man auch gleich die Namespace-Declarations einbauen:

PHP:
<?php
$fileContents = file_get_contents('test.xml');
//$fileContents = str_replace('ad:', '', $fileContents);
//$fileContents = str_replace('resource:', '', $fileContents);
$fileContents = str_replace('<ad:specifics>', '<ad:specifics xmlns:ad="http://www.w3.org/XML" xmlns:resource="http://www.w3.org/XML">', $fileContents);


$dom = new DOMDocument();
$dom->xmlStandalone = TRUE;
$dom->strictErrorChecking = FALSE;
$dom->loadXML($fileContents);

$xpath = new DOMXPath($dom);

//$childs = $xpath->query('/specifics/exterior-color/local-description[@xml-lang="de"]');
$childs = $xpath->query('/ad:specifics/ad:exterior-color/resource:local-description[@xml-lang="de"]');
foreach ($childs as $child)
{
	//var_dump($child);
	echo $child->textContent;
}
 
Bekomme dies hier:
file_get_contents(<?xml version="1.0" encoding="UTF-8" standalone="yes"?><search:result xmlns:resource="http://">

Ich weiß nicht, wie du an die XML-Daten kommst, mein Beispiel bezog sich darauf, dass du das XML aus einer Datei liest. Wenn der Inhalt aus einer HTTP-Verbindung bspw. über Socket geladen wird, hast du den String im Regelfall ja schon als Variable vorliegen. Die kannst du dann nachträglich manipulieren, wie du es brauchst.
 
Man könnte sich auch minimale XML-Strings basteln, in dem die erforderlichen Namensraumdeklarationen enthalten sind.
PHP:
$root1 = '<root xmlns:ad="http://www.deinServer.de/ad" xmlns:resource="http://deinServer.de/resource">';
$root2 = '</root>';
Damit wird der eigentlichen XML-String eingehüllt. Den XML-Prolog könnte man auch noch einfügen, ist aber nicht zwingend notwendig.

PHP:
$dom = new DOMDocument();
$dom->loadXML($root1 . $testXML . $root2);

$xpath = new DOMXPath($dom);
$xpath->registerNamespace('ad', 'http://www.deinServer.de/ad');
$xpath->registerNamespace('resource', 'http://www.deinServer.de/resource');

$query = '//ad:specifics/ad:exterior-color/resource:local-description[@xml-lang = "de"]';
$childs = $xpath->query($query);
foreach ($childs as $child)
{
  echo $child->textContent;
}
 

Neue Beiträge

Zurück