5

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;

ここに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値を変更するにはどうすればよいですか?

4

1 に答える 1

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();
于 2010-05-18T12:29:03.023 に答える