如何更改svg文件的属性值

use*_*443 5 php xml-attribute

在samplexml.svg中有一个节点

<image width="744" height="1052" xlink:href="image1.png"/>
Run Code Online (Sandbox Code Playgroud)

我需要将"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;
Run Code Online (Sandbox Code Playgroud)

这是samplexml.svg:

<svg>
    <g>
        <title>Test title</title>
        <image x="0" y="0" width="744" height="1052" xlink:href="image1.png"/>
    </g>
</svg>
Run Code Online (Sandbox Code Playgroud)

如何以编程方式更改xlink:href值?

Ann*_*rom 13

使用DOMElement :: setAttributeNS():

$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();
Run Code Online (Sandbox Code Playgroud)