2014-01-23 23 views
2

이것은 속임수 질문이 아닙니다. 다른 것들은 print_r에 속성이 없습니다. 그러나 xlink : href 속성에 액세스 할 수 없습니다.PHP SimpleXML에 속성이 없습니다.

여기에 내가 무엇을 시도했다입니다 :

 $xml = simplexml_load_string($imageSVG);   
     $image = $xml->g->image; // works 
     $style = $xml->g->image->style; // works 
     $style = $xml->g->image['style']; // works 
     $remoteHref = $xml->g->image['xlink:href']; // doesn't work 
     $remoteHref = $xml->g->image['href']; // doesn't work 
     $remoteHref = $xml->g->image->href; // doesn't work 
     $array= $xml->g->image->attributes('xlink'); // 0 elements in the array 

가 여기에 입력 된 XML의 :

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="351" height="351" xml:space="preserve"><desc></desc><defs></defs><g transform="translate(145 175) scale(0.4 0.4)"><image xlink:href="http://cdn.katori.com/BfFEEXuBTrii818CNQvN_71344PL.jpg" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(0,0,0); opacity: 1;" transform="translate(-300 -300)" width="600" height="600" preserveAspectRatio="none"></image></g></svg> 

감사합니다! 의 - 태그

당신이 구문 분석 할 XML의 형식을 알고 있다면 당신은 항상 당신이 할 수있는 빠른 더러운 해킹에 대한 필요 : - 매트

+0

이것은 속는 사람이 아닙니다. [그 (것)들의 살벌한 수백이있다] (http://stackoverflow.com/search?q=%5Bphp%5D+simplexml+namespace) : P (그러나 당신이 당신의 수색에 '네임 스페이스'를 추가하는 것을 모른다면 조금 힘들 수 있습니다). – Wrikken

+0

가능한 [Simplexml 네임 스페이스가있는 특성 얻기] (http://stackoverflow.com/questions/13511217/simplexml-get-attributes-with-a-namespace) – Wrikken

답변

4

attributes 방법은 전체 공간을 필요로하기위한 작동 희망하기 전에, 그냥 접두사 :

$s = '...'; 

$xml = new SimpleXMLElement($s); 

$attributes = $xml->g->image->attributes('http://www.w3.org/1999/xlink'); 
echo $attributes['href']; 

또는 (attributes의 두 번째 매개 변수를 사용하여 is_prefix - boolean) 접두어 만 지정하십시오.

$attributes = $xml->g->image->attributes('xlink', true); 

Here it is in action.

+0

예 !! 고마워. – mdundas

0

simplexml_load_string이 현물과 매우 잘 작동하지 않습니다 예

$imageSVG=str_replace(array('xlink:'),array('xlink-'),$imageSVG)

또는

$imageSVG=str_replace(array('xlink:'),array(''),$imageSVG);

,691 같은 것을 할

당신

simplexml_load_string($imageSVG);

당신이

관련 문제