当前位置: 首页 > 网络学院 > XML相关教程 > XML > XML 元素

XML
XML DHTML行为
XML 相关技术
XML 编辑器
XML 摘要
XML 实例
XML字符编码
xml 文档树
IE和火狐读取XML方法比较

XML 元素


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 873 ::
收藏到网摘: n/a

XML Elements are extensible and they have relationships.
XML元素是可扩展的,并且它们是互相关联的。

XML Elements have simple naming rules.
XML元素的命名规则非常简单。


 

XML Elements are Extensible
XML元素是可扩展的

XML documents can be extended to carry more information.
XML文档可以通过扩展来承载更多信息。

Look at the following XML NOTE example:
请看下面的XML Note 的案例:

<note>
<to>Tove</to>
<from>Jani</from>

<body>Don't forget me this weekend!</body>
</note>

Let's imagine that we created an application that extracted the <to>, <from>, and <body> elements from the XML document to produce this output:
假设我们已经创建了一个应用软件,它可以从XML文档中获取 <to>、<from> 和 <body> 元素,输出下面的结果:

MESSAGE

To: Tove
From: Jani

Don't forget me this weekend!

Imagine that the author of the XML document added some extra information to it:
假设XML文档的作者又添加了一些附加信息:

<note>

<date>2002-08-01</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>

</note>

Should the application break or crash?
应用程序是被终止还是被破坏?

No. The application should still be able to find the <to>, <from>, and <body> elements in the XML document and produce the same output.
不!应用程序仍会获取XML文档中的<to>, <from>,和 <body>元素,输出同样的结果。

XML documents are Extensible.
XML文档是可扩展的。


XML Elements have Relationships
XML元素是互相关联的

Elements are related as parents and children.
元素之间的关系类似于父母与子女这样的关系。

To understand XML terminology, you have to know how relationships between XML elements are named, and how element content is described.
想要了解XML术语,你必须了解如何定义XML元素之间的关系,以及如何描述元素所包含的内容。

Imagine that this is a description of a book:
假设下面的内容是对一本书的具体描述:

My First XML

Introduction to XML

  • What is HTML
  • What is XML

XML Syntax

  • Elements must have a closing tag
  • Elements must be properly nested

Imagine that this XML document describes the book:
假设下面的XML文档对这本书作出了具体描述:

<book>
<title>My First XML</title>

<prod id="33-657" media="paper"></prod>
<chapter>Introduction to XML
<para>What is HTML</para>
<para>What is XML</para>

</chapter>

<chapter>XML Syntax
<para>Elements must have a closing tag</para>
<para>Elements must be properly nested</para>
</chapter>

</book>

Book is the root element. Title, prod, and chapter are child elements of book. Book is the parent element of title, prod, and chapter. Title, prod, and chapter are siblings (or sister elements) because they have the same parent.
Book 是根元素。Title、prod 和 chapter 是书的子元素。Book 是 title、prod 和 chapter 的父元素。Title、prod 和 chapter 是“同属关系”(或姐妹元素),因为它们拥有相同的父类元素。


Elements have Content
元素中包含内容

Elements can have different content types.
元素可以包含不同的内容类型。

An XML element is everything from (including) the element's start tag to (including) the element's end tag.
一个XML元素的所有内容是元素的起始标签和终止标签之间的一切内容(起始、终止标签也包含其中)。

An element can have element content, mixed content, simple content, or empty content. An element can also have attributes.
一个元素可以包含元素内容、混合内容、简单内容、甚至内容。一个元素也可以包含属性。

In the example above, book has element content, because it contains other elements. Chapter has mixed content because it contains both text and other elements. Para has simple content (or text content) because it contains only text. Prod has empty content, because it carries no information.
上述例子中,book 包含元素内容,因为它包含着其它元素;Chapter 包含混合内容,因为它既包含文本又包含其它元素;Para包含简易内容(或者文本内容),因为它只包含文本。Prod 包含空内容,因为它没有携带任何信息。

In the example above only the prod element has attributes. The attribute named id has the value "33-657". The attribute named media has the value "paper". 
上述例子中只有prod元素有属性。ID属性值为"33-657"。Media属性值为"paper"。


Element Naming
元素命名规则

XML elements must follow these naming rules:
XML 元素必须遵循下面这些命名规则:

  • Names can contain letters, numbers, and other characters
    名称可以包含字母、数字和其它字符。
  • Names must not start with a number or punctuation character
    名称不能以数字或标点字符起始
  • Names must not start with the letters xml (or XML, or Xml, etc)
    名称不能以xml(或XML,或Xml)等字母开头
  • Names cannot contain spaces
    名称不能包含空格

Take care when you "invent" element names and follow these simple rules:
为元素命名时必须要遵循以下这些简单的规则:

Any name can be used, no words are reserved, but the idea is to make names descriptive. Names with an underscore separator are nice.
几乎所有名称都可被使用,但命名的方式必须让名称具有自我描述的性质。在名称之间用下划线加以分割的方法值得推荐,具体如下:

Examples: <first_name>, <last_name>.
例子:<first_name>、 <last_name>。

Avoid "-" and "." in names. For example, if you name something "first-name," it could be a mess if your software tries to subtract name from first. Or if you name something "first.name," your software may think that "name" is a property of the object "first."
避免在名称中出现"-" 和"."。举个例子,如果你将名称定为"first-name" 软件会误以为这是从“first”中减去name,这结果会造成运行混乱。如果你取名称为“first.name”,软件可能会认为"name"是first"对象的属性。

Element names can be as long as you like, but don't exaggerate. Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>. 
元素名称想取多长都可以,但不要太夸张了。名称应该尽量简短,像这样:<book_title>而不是这样:<the_title_of_the_book>

XML documents often have a corresponding database, in which fields exist corresponding to elements in the XML document. A good practice is to use the naming rules of your database for the elements in the XML documents.
XML文档通常包含相应的数据库,数据库应该与XML文档的元素相符。给XML文档元素取名时遵循数据库里的命名规则,这是个很好的习惯。

Non-English letters like éòá are perfectly legal in XML element names, but watch out for problems if your software vendor doesn't support them.
非英文字母如“éòá”在XML元素名称中是完全合法的。但是,如果你使用的软件不支持它们,必须注意这可能会出问题哦。

The ":" should not be used in element names because it is reserved to be used for something called namespaces (more later).
元素名称中不应该使用":"。因为它已经为命名空间专门预留出来了(具体内容将在后面做具体阐释)。

评论 (1) 1 All

登陆 | 还没注册?