2017-04-23 3 views
1

내가 처음 SimpleXMLElement 작업을 다음과 같이 내 XML의 라인을 생성해야 해요 : 내가 전에 네임 스페이스 addAttribute을 사용하지 실행할 수없는 한PHP SimpleXMLElement addAttribute는 네임 스페이스 구문

<Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

을 - 여기서 일하는 올바른 구문은 얻을 나는이 시작했습니다

$node = new SimpleXMLElement('<Product></Product >'); 
$node->addAttribute("xmlns:", "xsd:", 'http://www.w3.org/2001/XMLSchema-instance'); 

하지만 원하는 출력을 생성 할 수있는 적절한 구문이 문제를 해결하는 방법을 작동하지 않을 수 있습니까?

답변

0

솔루션 1 : 접두사에 접두사를 추가

<?php 
$node = new SimpleXMLElement('<Product/>'); 
$node->addAttribute("xmlns:xmlns:xsi", 'http://www.w3.org/2001/XMLSchema-instance'); 
$node->addAttribute("xmlns:xmlns:xsd", 'http://www.w3.org/2001/XMLSchema'); 
echo $node->asXML(); 

출력 :

<?xml version="1.0"?> 
<Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/> 

참고 :이, 속성에 대한 네임 스페이스를 설정하지 않습니다 실제로 해결하고 있지만, 당신이 에코/결과를 파일로 저장하려고한다면 충분합니다.

해결책 2 : 직접 네임 스페이스를 지정하십시오. SimpleXMLElement 생성자

<?php 
$node = new SimpleXMLElement('<Product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>'); 
echo $node->asXML(); 

출력 LY 용액 3 1

용액 동일하다 (추가 속성을 추가)

<?php 
$node = new SimpleXMLElement('<Product/>'); 
$node->addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance", "xmlns"); 
$node->addAttribute("xmlns:xsd", 'http://www.w3.org/2001/XMLSchema', "xmlns"); 
echo $node->asXML(); 

출력 부가 추가 xmlns:xmlns="xmlns"

<?xml version="1.0"?> 
<Product xmlns:xmlns="xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>