Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Link to image in CDATA

Status
Not open for further replies.

pknudsen

Programmer
Sep 20, 2010
5
0
0
DK
In my XML-code I want to make a link to popup an image.

I have this code:
the data comes from this array:
$files[] = array(
"filename"=>$file,
"intLatitude"=>$intLatitude,
"intLongitude"=>$intLongitude);
"directory"=>$$dh;
------------------
-and the XML-file:

$picfil = $dir."/".$name;
$file = fopen($dir."/".$name,"w");

$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= '<gpx>';
$xml .= '<metadata>';
$xml .= '<extensions>';
$xml .= '</extensions>';
$xml .= '</metadata>';
for($i = 0; $i < sizeof($files); ++$i) {
$xml .= '<wpt lat="' . $files[$i]['intLatitude'] .'" lon="' . $files[$i]['intLongitude'] .'">';
$xml .= '<extensions>';
$xml .= '<html>';
$xml .= '<![CDATA[<a href="#"><img src="'.parseToXML($files[$i]['filename']) .'"/></a>]]>';
$xml .= '</html>';
$xml .= '</extensions>';
$xml .= '</wpt>';
}
$xml .= '</gpx>';
fwrite($file,$xml);
fclose($file);

-and the function parseToXML

function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}

I want to change the CDATA-code so I can open the image in an popup-window. The images is in different directories, so the must be a variable.

 
[0] Let me set-aside any doubt in regard to the construction of $xml. I take it as correct to your satisfaction (including typos that may not exist in the script, $file at multiple places, or what to result of a pass-through of the function parseToXML().)

[1] >I want to change the CDATA-code so I can open the image in an popup-window.
As to this functionality, it belongs to html arena. It won't happen in the xml document as reflect in $xml.

[1.1] Having said that, you can extract the cdatasection as child of every possible <html> element. Put them all inside a html wrapper element, and then save it to a html file. In that case you can do the hyperlinking. (But then again just a remark: the href attribute is set to #. Hence, I don't see, a priori, the effect of clicking on the image bringing you to some other url. In any case, it doesn't matter and I leave it to you for further setup.)

[1.2] This is how it can be done as a quick sketch.
[tt]
[green]//etc etc in your script before the end
fclose($file);[/green]

//now your desired output html file
[green]$htmlfile="xyz.htm";[/green]

$xdoc=DOMDocument::loadXML($xml);
$xpdoc=new DOMXpath($xdoc);
$anchors=$xpdoc->query('//html');

$s='';
foreach ($anchors as $a) {
$s.=$a->nodeValue."\n"; //no need to pass through any function like parseToXML
}
$s="<html>\n<body>\n".$s."</body>\n</html>";

$f=fopen($htmlfile,"w");
fwrite($f,"\n\n");
fwrite($f,$s);
fclose($f);
[/tt]
[1.3] Now you have a html file which has the rudimentary layout that gives you the desired hyperlinkage.
 
I suggest i should replace my CDATA with something. Can you explain me?

If I replace my code in CDATA with text:

$xml .= '<![CDATA[<a target=_blank href="mymap/picture.jpg"><img src="mymap/picture.jpg"/></a>]]>';
-then it works.
 
I should say: what I need is a output like this:

example:

<?xml version="1.0" encoding="UTF-8" ?>
- <gpx>
- <metadata>
- <extensions>
</extensions>
</metadata>
- <wpt lat="55.68316" lon="11.82938">
- <extensions>
- <html>
- <![CDATA[ <a target=_blank href="mymap/picture.jpg"><img src="mymap/picture.jpg/></a>
]]>
</html>
</extensions>
</wpt>
</gpx>
 
If you can replace like that, then you can replace until any thing works for you in any manner you like. That's not my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top