[英]PHP SimpleXML is missing attributes
This isn't a dupe question. 这不是一个假问题。 Others are missing the attributes in print_r.
其他人缺少print_r中的属性。 But I can't access the attribute xlink:href at all.
但是我根本无法访问属性xlink:href。
Here's what i've tried: 这是我尝试过的:
$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
Here's the inputted XML: 这是输入的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>
Thanks! 谢谢! -matt
-matt
The attributes
method requires the full namespace, not just the prefix: attributes
方法需要完整的名称空间,而不仅仅是前缀:
$s = '...';
$xml = new SimpleXMLElement($s);
$attributes = $xml->g->image->attributes('http://www.w3.org/1999/xlink');
echo $attributes['href'];
Or use the second parameter of attributes
( is_prefix
- boolean ) and specify the prefix only: 或使用
attributes
的第二个参数( is_prefix
- boolean )并仅指定前缀:
$attributes = $xml->g->image->attributes('xlink', true);
simplexml_load_string doesn't work very well with this-kind:of-tags simplexml_load_string不能与this:of-tags一起很好地工作
If you know the format of the xml that you want to parse and you need a quick dirty hack you can always for example do something like 如果您知道要解析的xml的格式,并且需要快速进行处理,则可以例如执行以下操作
$imageSVG=str_replace(array('xlink:'),array('xlink-'),$imageSVG)
or 要么
$imageSVG=str_replace(array('xlink:'),array(''),$imageSVG);
before you 在你之前
simplexml_load_string($imageSVG);
Hope it works for you 希望这对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.