XML初探(一)
趁工作的空闲时间,随手拿了一本Erik T. Ray写的 《Learning XML》 翻了一下。一直想了解一下XML,确苦于没有时间。还有就是技术这个东西,你学了不用,过了一段时间就会全部忘光光。可惜我的项目用不到XML,不过反正没事,就看了一个下午,觉得容易忘记的一些东西记了一点下来。
Elements, attributes, namespaces, and entities are the most important markup objects in XML.
保留属性
XML可以定义自己的元素(element)包括这个元素的属性,但下没保留的元素属性不能重新定义:
xml:lang
Classifies an element by the language of its content. For example, xml:lang="en" describes an element
as having English content. This is useful for creating conditional text, which is content selected by an
XML processor based on criteria such as what language the user wants to view a document in. We'll
return to this topic in Chapter 7.
xml:space
Specifies whether whitespace should be preserved in an element's content. If set to preserve, any XML
processor displaying the document should honor all newlines, spaces, and tabs in the element's
content. If it is set to default, then the processor can do whatever it wants with whitespace (i.e., it
sets its own default). If the xml:space attribute is omitted, the processor preserves whitespace by
default. Thus, if you want to compress whitespace in an element, set the attribute xml:space="default"
and make sure you are using an XML processor whose default is to remove extra whitespace.
xml:link
告诉 XLink processor 这是一个xlink element。相当于HTML里面的超链接
xml:attribute
In addition to xml:link, XLink relies on a number of attribute names. But to prevent conflict with other
potential uses of those attributes, XLink defines the xml:attribute attribute, which allows you to
"remap" those special attributes. That is, you can say, "When XLink is looking for an attribute called
title, I want you to use the attribute called linkname instead." This attribute is also discussed in more
detail in Chapter 3.
名字空间 (namespace)
名字空间是属性和元素的集合,为了解决元素和属性的名字冲突的问题,XML也有命名空间。处于不用的名字空间的元素和属性的名字可以不同。名字空间声明在一个某一个element中,所有被这个element包含的element都将属于这个名字空间
例如下面的例子声明了一个名字空间bob,并且告诉XML parser 这里有一个名字空间的属性
<part-catalog xmlns:bob="http://www.bobco.com/">
实体 (entity)
实体是XML内容中的占位符(placeholder),一旦你定义一个实体后,接下来就可以用这个实体标识符在XML文档里来表示相同的内容。例如下面的文档定义了一个client 实体。在文档中,&client; 引用了该实体。
<?xml version="1.0"?> <!DOCTYPE message SYSTEM "/xmlstuff/dtds/message.dtd" [ <!ENTITY client "Mr. Rufus Xavier Sasperilla"> ]> <message> <opening>Dear &client;</opening> <body>We have an exciting opportunity for you! </body> </message>
在XML中有多种实体。