什么是XML中的根节点?
问题描述:
我在读电子书,它有以下的XML代码。什么是XML中的根节点?
<?xml version="1.0"?>
<?xml-stylesheet href="sonnet.xsl" type="text/xsl"?>
<?cocoon-process type="xslt"?>
<!DOCTYPE sonnet [
<!ELEMENT sonnet (auth:author, title, lines)>
<!ATTLIST sonnet public-domain CDATA "yes"
type (Shakespearean | Petrarchan) "Shakespearean">
<!ELEMENT auth:author (last-name,first-name,nationality,
year-of-birth?,year-of-death?)>
<!ELEMENT last-name (#PCDATA)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT nationality (#PCDATA)>
<!ELEMENT year-of-birth (#PCDATA)>
<!ELEMENT year-of-death (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT lines (line,line,line,line,
line,line,line,line,line,line,line,
line,line,line)>
<!ELEMENT line (#PCDATA)>
]>
<!-- Default sonnet type is Shakespearean, the other allowable -->
<!-- type is "Petrarchan." -->
<sonnet type="Shakespearean">
<auth:author xmlns:auth="http://www.authors.com/">
<last-name>Shakespeare</last-name>
<first-name>William</first-name>
<nationality>British</nationality>
<year-of-birth>1564</year-of-birth>
<year-of-death>1616</year-of-death>
</auth:author>
<!-- Is there an official title for this sonnet? They're
sometimes named after the first line. -->
<title>Sonnet 130</title>
<lines>
<line>My mistress' eyes are nothing like the sun,</line>
<line>Coral is far more red than her lips red.</line>
<line>If snow be white, why then her breasts are dun,</line>
<line>If hairs be wires, black wires grow on her head.</line>
<line>I have seen roses damasked, red and white,</line>
<line>But no such roses see I in her cheeks.</line>
<line>And in some perfumes is there more delight</line>
<line>Than in the breath that from my mistress reeks.</line>
<line>I love to hear her speak, yet well I know</line>
<line>That music hath a far more pleasing sound.</line>
<line>I grant I never saw a goddess go,</line>
<line>My mistress when she walks, treads on the ground.</line>
<line>And yet, by Heaven, I think my love as rare</line>
<line>As any she belied with false compare.</line>
</lines>
</sonnet>
<!-- The title of Sting's 1987 album "Nothing like the sun" is -->
<!-- from line 1 of this sonnet.
问题是当我继续阅读的其余部分时,我想我误解了什么是根元素。据我所知,根节点为<sonnet>
节点。
但在本书中,我发现旁边这个
不像其他的节点,根节点没有父。它始终在 至少有一个孩子,文档元素。根节点还包含 注释或处理指令,它们位于文档 元素之外。在我们的样本文档中,这两个处理指令命名 xmlstylesheet和茧过程是根节点的两个孩子, 的是,标签和标签后出现的 评论之前出现的注释。
accordning到整个文件被认为是根。如果是这样的话?很明显,处理指令和注释不是节点的子节点。
我minunderstand?
更新
<?xml version="1.0"?>
<Food>
<Cateogry> Vegetable</Cateogry>
<Cateogry> Fruits</Cateogry>
<Cateogry> other</Cateogry>
</Food>
什么是这里的根元素?它不是食物吗?
答
根节点没有视觉表现或文本表示。它是一个包含文档中其他所有内容的概念节点。在文档的示例中,这将是这样的:
/ (root node)
|-- processing-instruction() xml-stylesheet
|-- processing-instruction() cocoon-process
|-- comment()
|-- comment()
|-- sonnet (document element)
|-- auth:author
|-- last-name
|-- ....
|-- comment()
|-- title
|-- lines
|-- line
|-- line
|-- ....
|-- comment()
|-- comment()
如这里所示,在你的例子根节点2个处理指令,4个注释和1种元素作为孩子。
这种单一的子元素(十四行诗)是所谓的“文档元素”。它有时也被称为“根元素”,但很多人喜欢避免这个词,以最大限度地减少与“根节点”的混淆,因为它们是两个完全分离的东西。
不要混淆“根节点”与“根元素”或“文档元素”。 –