使用PHP循环SVG元素

使用PHP循环SVG元素,php,xml,xpath,svg,Php,Xml,Xpath,Svg,如何使用PHP循环SVG元素 <?php $svgString = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="overflow: hidden; position: relative;" width="9140" version="1.1" height="3050"> <rect x="0" y="0" width="9140"

如何使用PHP循环SVG元素

<?php

$svgString = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="overflow: hidden; position: relative;" width="9140" version="1.1" height="3050">
<rect x="0" y="0" width="9140" height="3050" r="0" rx="0" ry="0" fill="#FFFF00" stroke="#000"/>
<image x="-101.5" y="-113.5" width="203" height="227" xlink:href="1.jpg" stroke-width="1"></image>
<image x="-201.5" y="-213.5" width="103" height="127" xlink:href="2.jpg" stroke-width="1"></image>
</svg>';

$svg = new SimpleXMLElement( $svgString );
$result = $svg->xpath('//image');
echo count( $result ); 
for ($i = 0; $i < count($result); $i++) 
{
    var_dump( $result[$i] );
}
xpath('//image');
回声计数(结果);
对于($i=0;$i
count($result)
返回0,因此省略循环


我做错了什么?

svg文档使用的是默认名称空间:

<svg xmlns="http://www.w3.org/2000/svg" ...
哦,joy,名称空间:

$svg = new SimpleXMLElement( $svg );
$namespaces = $svg->getDocNamespaces();
$svg->registerXPathNamespace('__nons', $namespaces['']);

$result = $svg->xpath('//__nons:image');

非常感谢。我对xlink:href有类似的问题。我尝试了$svg->registerXPathNamespace('xlink',');但事实并非如此;好像不行。请你帮忙好吗?最新答案。现在它也使用xlink命名空间来获取href属性。我得到了:“警告:SimpleXMLElement::_construct():命名空间错误:未定义使用时href的命名空间前缀xlink…”当我尝试执行以下操作时:新建SimpleXMLElement(“”);为什么这样做:
newsimplexmlement(“”)?!“”只有一块短的贴在这里。我需要更换
$svg = new SimpleXMLElement( $svg );
$namespaces = $svg->getDocNamespaces();
$svg->registerXPathNamespace('__nons', $namespaces['']);

$result = $svg->xpath('//__nons:image');