Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The root is not the root element? #40

Open
therealprof opened this issue Feb 23, 2017 · 4 comments
Open

The root is not the root element? #40

therealprof opened this issue Feb 23, 2017 · 4 comments
Labels

Comments

@therealprof
Copy link

While trying to work with a parsed document I found something strange and rather counter-intuitive: The root does not seem to be the root element of the parsed document so when I iterate over the children of the root I will actually see all top level elements of the document including the root element itself.

let package = parser::load_xml(file);
let doc = package.as_document();
println!("{:?}", doc.root ()); // Yields "Root"

for elem in doc.root().children() {
    println!("Got {:?}", elem);  // Prints 2 elements: A comment and the root element
}
@shepmaster
Copy link
Owner

Hmm, yes, it appears I've accidentally colluded some bits of terminology. You are correct that the result of Document::root is a node that corresponds to section 4.8 of the XML spec:

The document entity serves as the root of the entity tree

(emphasis mine). This is unfortunate as I've also already used "document" to mean something else; the entrypoint to the allocation functions and owner of the DOM...

I wonder if there's anything preventing combining Document and Root...

@therealprof
Copy link
Author

Actually a document is the whole shebang including all metadata like the XML version and encoding and may also contain comments while the root is supposed to be the only regular element directly in the document.

I also don't quite get why there's a distinction between Root and Element in the code as they're exactly the same with the only exception being that a document must have exactly one Root. ChildOfRoot kind of seems superfluous as well (unless I'm missing something) because ChildOfRoot should not be any different than ChildOfElement.

I thought the easiest change would be to change:

    pub fn root(self) -> Root<'d> {
        self.wrap_root(self.connections.root())
    }

to return an Element instead but I failed miserably trying to implement that because a lot of code depends on the "root" being a Root.

@shepmaster
Copy link
Owner

ChildOfRoot should not be any different than ChildOfElement.

You'll note that ChildOfElement allows a Text element, which ChildOfRoot does not. That is, this text is not valid XML:

<?xml version="1.0">
hello
<thing />
world

@therealprof
Copy link
Author

True, but Root at the moment is not actually the root element but what the Document should be so I was trying to change the root fn to actually return the real root element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants