PHP SimpleXML缺少属性

PHP SimpleXML缺少属性,php,xml,Php,Xml,这不是一个骗人的问题。其他人缺少打印中的属性。 但我根本无法访问属性xlink:href 以下是我尝试过的: $xml = simplexml_load_string($imageSVG); $image = $xml->g->image; // works $style = $xml->g->image->style; // works $style = $xml->g-&

这不是一个骗人的问题。其他人缺少打印中的属性。 但我根本无法访问属性xlink:href

以下是我尝试过的:

        $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>

谢谢!
-matt

simplexml\u load\u字符串不能很好地处理这种标记

如果您知道要解析的xml格式,并且需要进行快速的恶意攻击,那么您可以执行以下操作

$imageSVG=str_替换(数组('xlink:')、数组('xlink-')、$imageSVG)

$imageSVG=str_替换(数组('xlink:')、数组('')、$imageSVG)

在你面前

simplexml\u load\u字符串($imagessvg)


希望它适合您

属性
方法需要完整的名称空间,而不仅仅是前缀:

$s = '...';

$xml = new SimpleXMLElement($s);

$attributes = $xml->g->image->attributes('http://www.w3.org/1999/xlink');
echo $attributes['href'];
或者使用
属性
是前缀
-)的第二个参数,并仅指定前缀:

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

这不是骗局吗P(但是,如果你不知道在搜索中添加“名称空间”,可能会有点困难)。可能是重复的!!谢谢,就这样。