首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XML中的根节点是什么?

XML中的根节点是什么?
EN

Stack Overflow用户
提问于 2013-02-06 10:06:03
回答 1查看 12.2K关注 0票数 6

我当时正在读一本电子书,它有以下xml代码。

代码语言:javascript
复制
<?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>节点。

但在书中我发现下面这个

与其他节点不同,根节点没有父节点。它总是至少有一个子元素,即document元素。根节点还包含文档元素之外的注释或处理指令。在我们的示例文档中,名为xml样式表和cocoon的两个处理指令都是根节点的子节点,在标记之前出现的注释和标记后面出现的注释也是如此。

根据这一点,整个文档被认为是根。如果是,那又是什么呢?显然,处理指令和注释不是节点的子节点。

我能理解吗?

UPdate

代码语言:javascript
复制
<?xml version="1.0"?> 
<Food> 
  <Cateogry> Vegetable</Cateogry>
  <Cateogry> Fruits</Cateogry>
  <Cateogry> other</Cateogry>
</Food>

这里的根元素是什么?不是吃的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-06 10:13:28

根节点没有视觉显示或文本表示。它是一个概念节点,包含文档中的其他所有内容。在您的文档示例中,如下所示:

代码语言:javascript
复制
/ (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()

如下图所示,示例中的根节点有两个处理指令、四个注释和一个子元素。

这个单个子元素(十四行诗)就是所谓的“文档元素”。它有时也被称为“根元素”,但许多人喜欢避免使用这个术语,以尽量减少与“根节点”的混淆,因为它们是完全独立的。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14726276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档