XML DOM and top-level document constructs
| From: | Paul DuBois | Date: | Sun, 27 Feb 2000 03:58:47 +0000 |
| Subject: | XML DOM and top-level document constructs | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-11065@lists.php.net to get a copy of this message | ||
php 4.0b4-dev
I'm trying to understand how top-level constructs are handled by the DOM
support for XML documents. I've modified a copy of the tests/testdom script
to perform tests 1 and 2 (which generate xmldoc and xmltree structures and
print them). I'm using a very simple document as a test.
First document, which has a single top-level construct, the <root> element:
$xmlstr = "<?xml version='1.0'?>
<root>
<!-- comment -->
<element>Text</element>
</root>
";
xmldoc() returns a structure that prints like this:
<root>
<!-- comment -->
<element>
Text </element></root> If I move the comment before the root element, like this, the document now has two top-level constructs (the comment and the <root> element: $xmlstr = "<?xml version='1.0'?> <!-- comment --> <root> <element>Text</element> </root> "; In this case, xmldoc() returns a structure that prints like this: <!-- comment --> Hmm! Looks like the root element got lost. Okay, next I try xmltree(). With the first document (where the comment occurs after the <root> element, and the <root> element is the only top-level construct), the output from var_dump() looks like this (note, the output is somewhat long, but the important thing is that the comment node is a child of the root node, as it should be): object(Dom document)(3) { ["doc"]=> resource(6) of type 10 ["version"]=> string(3) "1.0" ["root"]=> object(Dom node)(4) {
["node"]=>
resource(7) of type 11
["type"]=>
int(1)
["name"]=>
string(4) "root"
["children"]=>
array(2) {
[0]=>
object(Dom node)(4) {
["name"]=>
string(4) "text"
["content"]=>
string(9) " comment "
["node"]=>
resource(8) of type 11
["type"]=>
int(8)
}
[1]=>
object(Dom node)(4) {
["name"]=>
string(7) "element"
["node"]=>
resource(9) of type 11
["type"]=>
int(1)
["children"]=>
array(1) {
[0]=>
object(Dom node)(4) {
["name"]=>
string(4) "text"
["content"]=>
string(4) "Text"
["node"]=>
resource(10) of type 11
["type"]=>
int(3)
}
}
}
}
}
}
However, with the comment before the <root> tag, so that there are
two top-level constructs, the output looks like this:
object(Dom document)(3) {
["doc"]=>
resource(3) of type 10
["version"]=>
string(3) "1.0"
["root"]=>
object(Dom node)(4) {
["node"]=>
resource(4) of type 11
["type"]=>
int(8)
["name"]=>
string(4) "text"
["content"]=>
string(9) " comment "
}
}
Looks like some of the document has gotten lost, and curiously, the
comment has become a child of the <root> node, even though it isn't.
It seems to me that top-level constructs that appear in the XML document
are handled oddly if they are not element nodes. This differs from Perl
XML handlers -- my experience with the Perl XML handlers is that the
root element, comments, and processing instructions that appear at the
top level of the document are all instantiated as children of the
document node. (This is so with XML::DOM and XML::Grove, for example).
What is the document object for xmldoc() or xmltree() supposed to contain?
It doesn't seem to have a children() array, for instance, even though a
document could be constructed such that there are several constructs
at the top level. Besides the "version" property, it seems to have
only a "root" property, and no place for other children of the document
node.
--
Paul DuBois, paul@snake.net