Php 如何更改svg文件的属性值

Php 如何更改svg文件的属性值,php,xml-attribute,Php,Xml Attribute,在samplexml.svg中有一个节点 <image width="744" height="1052" xlink:href="image1.png"/> 我需要将“image1.png”替换为另一个值,如“image2.png”。请用示例代码指导我如何做到这一点 我可以得到属性值“image1.png”。代码如下: $xdoc = new DomDocument; $xdoc->Load('samplexml.svg'); $tagName = $xdoc->

在samplexml.svg中有一个节点

<image width="744" height="1052" xlink:href="image1.png"/>

我需要将“image1.png”替换为另一个值,如“image2.png”。请用示例代码指导我如何做到这一点

我可以得到属性值“image1.png”。代码如下:

$xdoc = new DomDocument;
$xdoc->Load('samplexml.svg');
$tagName = $xdoc->getElementsByTagName('image')->item(0);
$attribNode = $tagName->getAttributeNode('xlink:href');

echo "Attribute Name  : " . $attribNode->name . "<br/>";
echo "Attribute Value : " . $attribNode->value;
$xdoc=新文档;
$xdoc->Load('samplexml.svg');
$tagName=$xdoc->getElementsByTagName('image')->项(0);
$attribNode=$tagName->getAttributeNode('xlink:href');
echo“属性名:”$属性节点->名称。“
”; 回显“属性值:”$属性节点->值;
以下是samplexml.svg:

<svg>
    <g>
        <title>Test title</title>
        <image x="0" y="0" width="744" height="1052" xlink:href="image1.png"/>
    </g>
</svg>

考试题目
如何以编程方式更改xlink:href值?

使用:

$xdoc=新文档;
$xdoc->Load('svg.xml');
$tagName=$xdoc->getElementsByTagName('image')->项(0);
$attribNode=$tagName->getAttributeNode('xlink:href');
echo“属性名:”$属性节点->名称。“
”; 回显“属性值:”$属性节点->值; $tagName->setAttributeNS($tagName)http://www.w3.org/1999/xlink'、'xlink:href'、'image2.png'); echo$xdoc->saveXML();
$xdoc = new DomDocument;
$xdoc->Load('svg.xml');
$tagName = $xdoc->getElementsByTagName('image')->item(0);
$attribNode = $tagName->getAttributeNode('xlink:href');

echo "Attribute Name  : " . $attribNode->name . "<br/>";
echo "Attribute Value : " . $attribNode->value;

$tagName->setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'image2.png');

echo $xdoc->saveXML();