【问题标题】:What is the root node in XML?XML中的根节点是什么?
【发布时间】:2013-02-06 10:06:03
【问题描述】:

我正在阅读一本电子书,它有以下 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.

问题是当我继续阅读其余部分时,我以为我误解了根元素是什么。据我所知,根节点是&lt;sonnet&gt;节点。

但在书中我找到了下一个

与其他节点不同,根节点没有父节点。它总是在 至少一个孩子,文档元素。根节点还包含 文档之外的 cmets 或处理指令 元素。在我们的示例文档中,两个处理指令名为 xmlstylesheet 和 cocoon-process 都是根节点的子节点, 就像出现在标签之前的注释和 标记后出现的注释。

根据该整个文档被视为根。如果是的话是什么?很明显,处理指令和 cmets 不是节点的子节点。

我理解错了吗?

更新

<?xml version="1.0"?> 
<Food> 
  <Cateogry> Vegetable</Cateogry>
  <Cateogry> Fruits</Cateogry>
  <Cateogry> other</Cateogry>
</Food>

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

【问题讨论】:

  • 不要将“根节点”与“根元素”或“文档元素”混淆。

标签: xml xslt


【解决方案1】:

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

/ (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 个 cmets 和 1 个作为子节点的元素。

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

【讨论】:

  • 表示不是根节点??
  • 另一个问题我们可以使用根元素作为根节点吗?
  • 您是在问是否可以将root node 称为root element?如果是这样,不,这将是不准确的。元素是以尖括号中的标签开始和结束的节点,例如&lt;sonnet&gt;
  • 好的,谢谢。我想现在明白其中的区别了。 是根元素,但不是根节点。对吗?
  • 这个答案似乎是错误的:w3schools.com/js/js_htmldom_navigation.asp 根据 W3C 的文档,根节点只是顶部节点,例如 。
猜你喜欢
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-18
  • 2015-04-29
  • 1970-01-01
相关资源
最近更新 更多