0

I have question on how to access SourceUrl for image with width=400

images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198

By default it is showing me image with width=100 and somehow my xpath syntax is not picking up 400

  <?php
$string = <<<XML
<imageList>
<image available="true" height="100" width="100">
        <sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-100x100-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL>
</image>
<image available="true" height="200" width="200"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-200x200-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="300" width="300"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-300x300-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="400" width="400"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="569" width="500"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-500x569-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image></imageList>
XML;


$xml = simplexml_load_string($string); 

$result = $xml->xpath("//image[@height='400']/sourceURL");



?> 

1 Answers1

0

Your xpath is all right, but the XML is not valid, see http://www.xmlvalidation.com
The & within your links will be parsed as start of a character entity --> error.

solution: exclude the text within <sourceURL> from parsing with <![CDATA[...]]>:

$x = <<<XML
<imageList>
<image available="true" height="100" width="100">
    <sourceURL>
        <![CDATA[images/di/47/6b/77/454430384d6d324b413332544a695675313851-100x100-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198]]>
    </sourceURL>
</image>
...
</imageList>
XML;

$xml = simplexml_load_string($x);
$result = $xml->xpath("//image[@height='400']/sourceURL")[0];
echo $result;

output:

images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198

see it working: http://codepad.viper-7.com/31PLr3

more on CDATA: What does <![CDATA[]]> in XML mean?

Community
  • 1
  • 1
michi
  • 6,486
  • 4
  • 31
  • 52