---
title: "XML基础知识及Namespaces理解"
author: "ChuanSu"
date: "2012-03-08T10:52:31Z"
tags: ["Atttibutes", "Local name", "Qualified name", "XML", "namespaces"]
categories: ["Web前端"]
slug: "xml-namespaces"
draft: false
lastmod: "2012-03-13T10:52:59Z"
id: 1445628
url: "https://chuansu.iteye.com/blog/1445628"
---

\[size=medium]XML 是"Extensible Markup Language" 的简称，是一种text-based，tag-based(基于文本，基于标签)的标记性语言。 XML 可以分为两种不同的mode(模式) ： \[color=red]Well-formed XML Valid XML\[/color] Well-formed XML相对来说比较自由,没有预先设定好的schema(架构)来约束，可以在不违反 nesting rule for tag 的情况下,创建自己的tag，标签。 刚开始学XML，必须得清楚几个重要的概念 ： 1，\[color=blue]Tags\[/color] 2，\[color=blue]Element，Root Element，Empty Element\[/color] 3，\[color=blue]Attribute\[/color] 4，\[color=blue]Namespace\[/color] 5，\[color=blue]Namespace Name\[/color] 6，\[color=blue]Qualified Name\[/color] 7，\[color=blue]Local Name\[/color] Tag\[color=red]s\[/color] -标签，在上面之所以称标签为Tags而不是Tag，是因为在XML中，标签总是成对出现的，当然除了 Empty Tag。举个例子 \[color=blue]\[/color]Chuan Su\[color=blue]\[/color] 当一个 (opening tag)出现时，后面必定会跟上一个matched (closing tag) 所以 \[color=red]Tags in XML are text surrounded by triangular brackets,&lt;...&gt; \[/color] 两个tag，opening tag与closing tag，并且包括在两标签内的everything，就称为XML的一个 \[color=red]Element。\[/color] 说了这么多先来看个简单的例子吧。\[/size] \[code="xml"] Carrie Fisher

123 Maple St. Hollywood

5 Locust Ln. Malibu

Mark Hamill 456 0ak Rd. Brentwood Star Wars 1977 \[/code] 第一行称为XML文件的prolog(开端),最小的XML prolog为 \[color=violet]\[/color]仅表明这个文件是XML 文件 对于attribute \[color=red] standalone = "yes"\[/color] 这个的理解，官方网站上这样讲的\[quote]standalone: Tells whether or not this document references an external entity or an external data type specification. If there are no external references, then "yes" is appropriate.\[/quote] 也可以简单的理解为这个XML文件不涉及到 DTD，当看到 standalone = "yes"就可以直接说 这个xml 文件 是一个\[color=red]well-formed XML\[/color]。 上面讲到了 element， 在这个例子中，可以看到有很多个elements，但是在一个xml文件中\[color=red]root element\[/color]只有一个，位于 \[code="xml"] .......... \[/code] \[quote]在 XML 中，an element can have\[color=red]attributes(name-value pairs) within its opening tag.\[/color] \[/quote] 每一个xml element 可以有多个attributes，attributes是以 “name = value"这样的形式出现在 这个element的opening tag当中的。 比如上面例子中的 "... "element,如果要对Movie添加属性的话， 写为 \[code="xml"] America 120 minutes \[/code] 此时的 title,year不再是element而是 element Movie的 attributes了。 在XML 中也可以有 \[color=red]Empy element\[/color],在上面的例子当中 可以出现 \[color=red]\[/color] 这样一个单纯的Empty element，即只有一个tag，但是Empty element可以有自己的attributes，例如 \[code="xml"]\[/code] \[size=large]\[color=red]Namespaces\[/color]\[/size] 由于关于Namespace 写的态度，姑且放到了 我的博客分类\[color=red] 《详解XML namespace》\[/color]一文中了，请见谅。