라이브러리
[PHP] DOMElement::setAttributeNS - 새로운 속성을 추가합니다.
DOMElement::setAttributeNS
`DOMElement::setAttributeNS`는 DOM 요소에 이름 공간이 있는 속성을 추가하는 메서드입니다. 이름 공간이 있는 속성을 추가하려면 `setAttributeNS`를 사용해야 합니다.
# 이름 공간이 있는 속성
이름 공간이 있는 속성은 다음 형식으로 구성됩니다.
`{namespace-uri}local-name`
예를 들어, `http://www.w3.org/1999/xhtml` 이름 공간이 있는 `title` 속성은 `{http://www.w3.org/1999/xhtml}title`로 표현됩니다.
# 사용법
`DOMElement::setAttributeNS` 메서드는 다음 형식으로 사용됩니다.
#hostingforum.kr
php
void DOMElement::setAttributeNS ( string $namespaceURI , string $qualifiedName , string $value )
* `$namespaceURI`: 이름 공간의 URI
* `$qualifiedName`: 이름 공간이 있는 속성 이름
* `$value`: 속성 값
# 예제
#hostingforum.kr
php
// DOMDocument 객체를 생성합니다.
$doc = new DOMDocument();
// 루트 요소를 생성합니다.
$root = $doc->createElement('html');
$doc->appendChild($root);
// 이름 공간이 있는 속성을 추가합니다.
$root->setAttributeNS('http://www.w3.org/1999/xhtml', 'xmlns', 'http://www.w3.org/1999/xhtml');
$root->setAttributeNS('http://www.w3.org/1999/xhtml', 'lang', 'ko');
// 이름 공간이 없는 속성을 추가합니다.
$root->setAttribute('title', 'PHP DOM');
// XML을 문자열로 변환합니다.
$xml = $doc->saveXML();
// 결과를 출력합니다.
echo $xml;
이 예제에서는 `http://www.w3.org/1999/xhtml` 이름 공간이 있는 `xmlns`와 `lang` 속성을 추가합니다. 또한 이름 공간이 없는 `title` 속성을 추가합니다.
# 결과
#hostingforum.kr
xml
<?xml version="1.0"?>
PHP DOM
이 예제는 이름 공간이 있는 속성을 추가하는 방법을 보여줍니다. 이름 공간이 있는 속성을 추가하려면 `setAttributeNS` 메서드를 사용해야 합니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.