2012-01-31 4 views
0

XML、名前空間、PHP DOMに問題があります。PHP DOMでxml名前空間を生成

これは私が取得する必要私の出力である。これを解決する方法任意のアイデア

<cd:Document xmlns="http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2" xmlns:cd="http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd"> 
<cd:Data> 
    <cd:DataFormat> 
     <cd:MimeType>text/xml</cd:MimeType> 
    </cd:DataFormat> 
    <cd:Content> 
     <cd:EmbeddedData> 

、これが私のPHPコード

$root = $doc->appendChild($doc->createElementNS('http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2', 'cd:Document')); 
$root->setAttributeNS('http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2', 'cd', 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0'); 
$root->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation', 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd'); 

のですか?

答えて

3
<?php 
$doc = new DOMDocument(); 
$doc->formatOutput = true; 
//set root element to correct cd prefix _and_ namespace: 
$root = $doc->appendChild(
     $doc->createElementNS(
     $cd = 'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0', 
     'cd:Document')); 
//this is the bit of obscure magic: it will set the default namespace 
$doc->createAttributeNS(
     'http://www.zbs-giz.si/Schemas/2006/ZBSxml/2.2', 
     'xmlns'); 
//now continue as normal 
$root->setAttributeNS(
     'http://www.w3.org/2001/XMLSchema-instance', 
     'xsi:schemaLocation', 
     'http://www.crea.si/Schemas/2004/Document/ZBSxml/2.0/ZbsCreaDoc.xsd'); 
$data = $root->appendChild($doc->createElementNS($cd,'cd:Data')); 
$dataformat = $data->appendChild($doc->createElementNS($cd,'cd:DataFormat')); 
$dataformat->appendChild($doc->createElementNS($cd,'cd:MimeType','text/xml')); 
$content = $data->appendChild($doc->createElementNS($cd,'cd:Content'));