
DOMElement::removeAttributeNS 함수를 사용하여 namespace를 가진 attribute를 제거할 때, namespace URL을 지정하는 방법은 다음과 같습니다.
- 첫 번째 인자는 namespace URL을, 두 번째 인자는 attribute 이름을 지정합니다.
- namespace URL은 attribute의 namespace URL과 일치해야 합니다.
- attribute 이름은 attribute의 이름을 지정합니다.
위의 예제에서, `$element->removeAttributeNS('http://example.com/ns', 'ns:attr3');` 라인에서 에러가 발생하는 이유는 attribute 'ns:attr3'이 존재하지 않기 때문입니다.
attribute 'ns:attr3'을 생성한 후에 제거하려고 하는 경우, 다음과 같이 코드를 작성할 수 있습니다.
#hostingforum.kr
php
$doc = new DOMDocument();
$root = $doc->createElement('root');
$root->setAttributeNS('http://example.com/ns', 'ns:attr', 'value');
$root->setAttributeNS('http://example.com/ns', 'ns:attr2', 'value2');
$root->setAttributeNS('http://example.com/ns', 'ns:attr3', 'value3');
$element = $root->getElementsByTagName('element')->item(0);
$element->removeAttributeNS('http://example.com/ns', 'ns:attr');
$element->removeAttributeNS('http://example.com/ns', 'ns:attr2');
$element->removeAttributeNS('http://example.com/ns', 'ns:attr3');
위의 코드를 실행하면 attribute 'ns:attr3'이 제거됩니다.
2025-05-26 22:17