0

これは私が欲しい結果です:

<?xml version="1.0" encoding="utf-8"?>
<types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
</types>

しかし、簡単な解決策を使用します:

$document = new DOMDocument('1.0', 'utf-8');

$schema = $document->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:schema');

$types = $document->createElement('types');
$types->appendChild($schema);

$document->appendChild($types);

echo $document->saveXML();

私はこれだけを手に入れます:

<?xml version="1.0" encoding="utf-8"?>
<types xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
</types>

私は何が欠けていますか?

4

1 に答える 1

4

問題は、子を追加する順序です。これを試して:

$document = new DOMDocument('1.0', 'utf-8');

$types = $document->createElement('types');
$schema = $document->createElementNS('http://www.w3.org/2001/XMLSchema', 'xs:schema');

$document->appendChild($types);
$types->appendChild($schema);

echo $document->saveXML();
于 2012-10-13T03:04:27.943 に答える