dポイントプレゼントキャンペーン実施中!

PHP5のdomを使ってRSS1.0用のxmlファイルを自動生成しようと
試みているのですが、RSS1.0用にネームスペースを記述しようとしても
なかなか理想通りに行きません。

理想としては下記のように表示したいのですが
<rdf:RDF
xmlns="http://purl.org/rss/1.0/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xml:lang="ja">

現時点では『xmlns="http://purl.org/rss/1.0/"』が表示出来ません。
<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF
 (ここが抜けている)
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:lang="ja">

書いたソースコードを記載します。
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$root = $doc->createElementNS('http://purl.org/rss/1.0/', 'rdf:RDF');
$doc->appendChild($root);
$root->setAttributeNS($NAMESPACE ,'xmlns:rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
$root->setAttributeNS($NAMESPACE ,'xmlns:dc', 'http://purl.org/dc/elements/1.1/');
$root->setAttributeNS($NAMESPACE ,'xmlns:lang', 'ja');

setAttributeNSで色々と試してみたのですが、『xmlns=』と言う形に出来ませんでした。

初歩的な質問で大変申し訳ありませんが
皆様のお力添えを頂ければと思います。

宜しくお願い致します。

A 回答 (5件)

<?php //とりあえず出ればいいんかな..? v5.2.8



$doc = new DOMDocument('1.0', 'utf-8');
$root = $doc->createElement('rdf:RDF');
$doc->appendChild($root);
$doc->createAttributeNS('http://purl.org/rss/1.0/', 'xmlns');
$root->setAttribute('xmlns:rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
$root->setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
$root->setAttribute('xml:lang', 'ja');

print '<xmp>' . $doc->saveXML($root);
    • good
    • 0
この回答へのお礼

createElementで『rdf:RDF』を作ってしまえばいいんですね。
盲点でした。

本当に、ありがとうございます!

お礼日時:2009/09/05 12:41

しつこくてすまんが,一応バグ報告出してみた。

(俺がおかしければ,指摘が入るだろう)

http://bugs.php.net/bug.php?id=49463
    • good
    • 0
この回答へのお礼

ご協力有難う御座いました。
PHPのバグの可能性もあるという事なのですね…

ちょっと別のやり方も試して見ます。
本当に感謝致します。 _(._.) _

お礼日時:2009/09/04 11:13

多分PHPのバグです。

名前空間URIとして空文字列を指定した場合,xmlns属性が付与できました。

同じことをJavaでやると,DOMの仕様どおり NAMESPACE_ERRが投げられます。
    • good
    • 0

#適当に決め打って,#1の回答したけど,それが原因ではないようだ。



ちょっと再調査中。

この回答への補足

回答ありがとう御座います。
私の方でも現在調査中です。

補足日時:2009/09/04 10:09
    • good
    • 0

メモ:



http://www.w3.org/TR/xml-names/#ns-decl
Namespace constraint: Reserved Prefixes and Namespace Names

The prefix xml is by definition bound to the namespace name http://www.w3.org/XML/1998/namespace. It MAY, but need not, be declared, and MUST NOT be bound to any other namespace name. Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace.

The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/. It MUST NOT be declared . Other prefixes MUST NOT be bound to this namespace name, and it MUST NOT be declared as the default namespace. Element names MUST NOT have the prefix xmlns.

All other prefixes beginning with the three-letter sequence x, m, l, in any case combination, are reserved. This means that:

*users SHOULD NOT use them except as defined by later specifications
*processors MUST NOT treat them as fatal errors.
===========================
http://www.w3.org/TR/DOM-Level-2-Core/core.html# …

NAMESPACE_ERR: Raised if the qualifiedName is malformed, if the qualifiedName has a prefix and the namespaceURI is null, if the qualifiedName has a prefix that is "xml" and the namespaceURI is different from "http://www.w3.org/XML/1998/namespace", or 【if the qualifiedName is "xmlns" and the namespaceURI is different from "http://www.w3.org/2000/xmlns/"】.
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!