XML transcript

I have often encountered XML recently, so I learned it today and shared my notes here

 

(1) Introduction and source:

      XML, short for Extensible Markup Language, is designed to transmit and store data, in which markup is a key part.

      Content can be created and then tagged with qualifying tags, making each word, phrase, or block a identifiable, classifiable piece of information. The created file, or document instance, consists of elements (tags) and content. Elements help to better understand documents when they are read from printed output or processed electronically. The more descriptive an element is, the easier it is to identify parts of the document. Since the advent of markup, content with markup has the advantage that in the absence of computer systems, the printed data can still be understood by markup.

      Markup languages ​​evolved from early private companies and governments to Standard Generalized Markup Language (SGML), Hypertext Markup Language (HTML), and eventually XML. SGML is more complex, and HTML (which is really just a set of elements) is not powerful enough to identify information. XML is an easy-to-use and easy-to-extensible markup language.

 

(2) Comparison of XML and HTML:

It's important to first understand that XML and HTML are designed for different purposes and are not replacements for HTML. The best description of XML is: XML is a software and hardware independent information transfer tool

①XML is designed to transmit and store data, mainly responsible for the content of the data; and there are no predefined tags, so you can create your own tags, which will be introduced below; spaces will be reserved

②HTML is designed to display data, mainly responsible for the display of data; and there are predefined tags; spaces will be cut into one

[XML details:]

1. A markup language much like HTML, designed to transmit data, not display it.

And the label is not predefined, you need to define the label yourself. Designed to be self-describing, a W3C Recommendation (1998.02)

2. XML doesn't do anything

Maybe it's a little hard to understand, but XML doesn't do anything. XML was designed to structure, store, and transmit information.

The following example is a note that Jani wrote to Tove, stored as XML:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

 The note above is self-descriptive. It contains the sender and receiver information, as well as the header and the message body.

However, this XML document still doesn't do anything. It's just pure information wrapped in XML tags. We need to write software or programs to transmit, receive and display this document.

3. You can create your own tags

The tags in the example above are not defined in any XML standard (eg <to> and <from>). These tags were invented by the creator of the XML document.

This is because the XML language has no predefined tags, whereas the tags used in HTML are all predefined. HTML documents can only use tags defined in the HTML standard (eg <p>, <h1>, etc.).

XML allows authors to define their own tags and their own document structure.

4. Applicability

XML is the most commonly used tool for data transfer between various applications, and its role in the Web is no less than HTML, which has always been the cornerstone of the Web.

 

(3) XML usage

①Separate data from HTML;

② Simplify data sharing;

③ Simplify data transmission;

④ Simplify platform changes;

⑤Enhancing data availability, through XML, your data can be used by various reading devices (handheld computers, voice devices, news readers, etc.), and can also be used by blind or other disabled people;

⑥ Create new Internet languages, such as XHTML, WSDL for describing available Web services, and WAP and WML as markup languages ​​for handheld devices

 

(4) Tree structure

XML documents form a tree structure that starts at the "root" and expands to the "leaves"

 

(5) Grammar

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

①XML 声明:

如果存在需要放在文档的第一行,如<?xml version="1.0" encoding="utf-8"?>

②关闭标签:

所有的 XML 元素都必须有一个关闭标签(除了声明外,因为声明不是 XML 文档本身的一部分,所以它没有关闭标签)

③对大小写敏感:

必须使用相同的大小写来编写打开标签和关闭标签

<Message>这是错误的</message>
<message>这是正确的</message>

④必须被正确嵌套;

⑤属性值必须加引号;

⑥实体引用/实体字符:

在 XML 中,只有字符 "<" 和 "&" 确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。

如果把字符 "<" 放在 XML 元素中,会发生错误,这是因为解析器会把它当作新元素的开始

在 XML 中,有 5 个预定义的实体引用:

&lt; < less than(小于号)
&gt; > greater than(大于号)
&amp; & ampersand(符号--和/与)
&apos; ' apostrophe(撇号/单引号)
&quot; " quotation mark(引号/双引号)

⑦注释:注释语法与HTML相同,即<!-- -->

⑧保留空格:与HTML裁减空格不同,XML的空格会被保留下来

⑨存储换行:XML 以 LF 存储换行

而在 Windows 应用程序中,换行通常以一对字符来存储:回车符(CR)和换行符(LF);在 Unix 和 Mac OSX 中,使用 LF 来存储新行;在旧的 Mac 系统中,使用 CR 来存储新行

 

(6)XML元素

①元素构成:

XML 元素指的是从(且包括)开始标签直到(且包括)结束标签的部分,包含文本,属性和其他元素

②元素命名规则:

名称可以包含字母、数字以及其他的字符;不能以数字或者标点符号开始

不能以字母 xml(或者 XML、Xml 等等)开始;不能包含空格

③命名习惯:

名称应具有描述性,使用下划线的名称也很不错:<first_name>、<last_name>;

名称应简短和简单,比如:<book_title>,而不是:<the_title_of_the_book>;

避免 "-" 字符,如果这样进行命名:"first-name",一些软件会认为您想要从 first 里边减去 name;

避免 "." 字符,如果按这样的方式进行命名:"first.name",一些软件会认为 "name" 是对象 "first" 的属性;

避免 ":" 字符,冒号会被转换为命名空间来使用(稍后介绍)

④可扩展:

XML 的优势之一,就是可以在不中断应用程序的情况下进行扩展

 

(7)XML属性

①属性VS元素:

 

<person date="10/01/2008">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<person>
<date>10/01/2008</date>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
    In the first instance, sex is an attribute. In the second instance, sex is an element. Both instances provide the same information.

 

There are no rules that tell us when to use attributes and when to use elements.

    My experience is that in HTML, attributes are convenient, but in XML, attributes should be avoided as much as possible. If information feels a lot like data, use elements

    Of course you can also use the extended data element:

 

<person>
<date>
<day>10</day>
<month>01</month>
<year>2008</year>
</date>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
 ②Avoid using XML attributes

 

Some problems caused by using properties:

Attributes cannot contain multiple values ​​(elements can); attributes cannot contain tree structures (elements can); attributes are not easily extensible (for future changes)

Properties are hard to read and maintain, etc.

So try to use elements to describe data, and only use attributes to provide data-independent information

Don't do stupid things like this (this is not how XML should be used):

<note day="10" month="01" year="2008"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>

 

(8) XML attributes for metadata

有时候会向元素分配 ID 引用,这些 ID 索引可用于标识 XML 元素,它起作用的方式与 HTML 中 id 属性是一样的

【注意】:id 属性仅仅是一个标识符,用于标识不同的便签,它并不是标签数据的组成部分。在此极力传递的理念是:元数据(有关数据的数据)应当存储为属性,而数据本身应当存储为元素

 

(9)XML验证

①通过 DTD 验证的XML是"合法"的 XML,DTD 的目的是定义 XML 文档的结构,它使用一系列合法的元素来定义文档结构

具体我在下一篇文章里做了总结

②XML Schema

W3C 支持一种基于 XML 的 DTD 代替者,它名为 XML Schema(架构)

 

(10)XML验证器

①使用XML验证器来对XML文件进行语法检查

XML文档里的错误会终止您的 XML 应用程序

W3C 的 XML 规范声明:如果 XML 文档存在错误,那么程序就不应当继续处理这个文档。理由是,XML 软件应当轻巧,快速,具有良好的兼容性

 ②还可以根据 DTD 来验证 XML,需要把 DOCTYPE 声明(带有 DTD)添加到XML中 <xml> 元素后

 

(11)查看XML文件

由于 XML 标签由 XML 文档的作者"发明",浏览器无法确定像 <table> 这样一个标签究竟描述一个 HTML 表格还是一个餐桌。

在没有任何有关如何显示数据的信息的情况下,大多数的浏览器都会仅仅把 XML 文档显示为源代码。

例如:一个简单的食物菜单-------这是一个来自餐馆的早餐菜单,存储为 XML 数据

 

(12)样式格式化

关于XML样式格式化有两种方式:CSS和XSLT,下面分开讲解

①使用 CSS 显示您的 XML(不常用,W3C 推荐使用 XSLT)

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>

 ②XSLT

XSLT(eXtensible可扩展 Stylesheet样式表 Language Transformations语言转换),在浏览器显示 XML 文件之前,先把它转换为 HTML

可以在浏览器完成转换,但是不同浏览器可能产生不同结果,所以也可以在服务器进行转换

 

(13)跨域访问

出于安全方面的原因,现代的浏览器不允许跨域的访问。

这意味着,网页以及它试图加载的 XML 文件,都必须位于相同的服务器上

 

(14)加载XML文件

如需从 XML 文件("note.xml")的 <to> 元素中提取文本 "Tove",语法是:

getElementsByTagName("to")[0].childNodes[0].nodeValue

请注意,即使 XML 文件只包含一个 <to> 元素,您仍然必须指定数组索引 [0]。这是因为 getElementsByTagName() 方法返回一个数组

 

(15)XML应用实例

在 HTML div 元素中显示第一个 CD

下面的实例从第一个 CD 元素中获取 XML 数据,然后在 id="showCD" 的 HTML 元素中显示数据

displayCD() 函数在页面加载时调用:

<!DOCTYPE html>
<html>
<head>
<script>
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
     xmlhttp=new XMLHttpRequest();
  }else{// code for IE6, IE5
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
x=xmlDoc.getElementsByTagName("CD");
i=0;
function displayCD(){
  artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
  title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
  year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
  txt="Artist: " + artist + "<br>Title: " + title + "<br>Year: "+ year;
  document.getElementById("showCD").innerHTML=txt;
}
</script>
</head>
<body onload="displayCD()">
<div id='showCD'></div>
</body>
</html>

 

 

【XML进阶】

(1) 命名冲突

在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突

①使用前缀来避免命名冲突,例如

<h:table>
<h:td>Apples</h:td>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
</f:table>

 ②XML 命名空间 - xmlns 属性

当在 XML 中使用前缀时,一个所谓的用于前缀的命名空间必须被定义,命名空间是在元素的开始标签的 xmlns 属性中定义

 1 . 命名空间声明语法:xmlns:前缀="URI"

<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:td>Apples</h:td>
</h:table>
<f:table xmlns:f="http://www.w3cschool.cc/furniture">
<f:name>African Coffee Table</f:name>
</f:table>
</root>

  2. 默认的命名空间

为元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作。它的语法如下:

xmlns="namespaceURI"

这个 XML 携带 HTML 表格的信息:

<table xmlns="http://www.w3.org/TR/html4/">
<td>Apples</td>
</table>

  3 . xsl命名空间标识xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

XSLT 是一种用于把 XML 文档转换为其他格式的 XML 语言,比如 HTML。

在下面的 XSLT 文档中,您可以看到,大多数的标签是 HTML 标签。

非 HTML 的标签都有前缀 xsl,并由此命名空间标识:xmlns:xsl="http://www.w3.org/1999/XSL/Transform":

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr>
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

 

(2)XML的CDATA

CDATA 是不应该由 XML 解析器解析的文本数据

像 "<" 和 "&" 字符在 XML 元素中都是非法的,而某些文本,比如JavaScript代码,包含大量 "<" 或 "&" 字符。为了避免错误,可以将脚本代码定义为 CDATA,这样CDATA 部分中的所有内容都会被解析器忽略

CDATA 部分由 "<![CDATA[" 开始,由 "]]>" 结束:

<script>
<![CDATA[
function matchwo(a,b){
if (a < b && a < 0) then{
return 1;
}else{
return 0;
}
}
]]>
</script>

 

(3)服务器上的 XML

XML 文件是类似 HTML 文件的纯文本文件,能够通过标准的 Web 服务器轻松地存储和生成

【存储】:在服务器上存储 XML 文件

XML 文件在 Internet 服务器上进行存储的方式与 HTML 文件完全相同

用适当的文件名,比如 "note.xml",在 Web 服务器上保存这个文件

【生成】

通过ASP,PHP,数据库都可以

在服务器上通过 XSLT 转换 XML,例如ASP代码在服务器上把 XML 文件转换为 XHTML

 

(4)XML相关技术

①可扩展超文本标记语言XHTML

②DTD (文档类型定义),定义 XML 文档中的合法元素的标准

③SVG (可伸缩矢量图形) ,定义 XML 格式的图形

 

 

 

【【这里分享个不错的技术文章,简单易懂】】

  XML即可扩展标记语言(eXtensible Markup Language)。标记是指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种信息的文章等。如何定义这些标记,既可以选择国际通用的标记语言,比如HTML,也可以使用象XML这样由相关人士自由决定的标记语言,这就是语言的可扩展性。XML是从SGML中简化修改出来的。它主要用到的有XML、XSL和XPath等。

上面这段是对XML的一个基本定义,一个被广泛接受的说明。简单说,XML就是一种数据的描述语言,虽然它是语言,但是通常情况下,它并不具备常见语言的基本功能——被计算机识别并运行。只有依靠另一种语言,来解释它,使它达到你想要的效果或被计算机所接受。

假如你是刚接触XML的新手,那么可能并无法从定义上是了解XML是什么。也许,你可以换个角度来认识XML是什么;从应用面来认识XML,从XML可以做些什么来认识它,这应该能比那更空洞的定义对你更有帮助。

XML应用面主要分为两种类型,文档型和数据型。下面介绍一下几种常见的XML应用:

1、自定义XML+XSLT=>HTML,最常见的文档型应用之一。XML存放整个文档的XML数据,然后XSLT将XML转换、解析,结合XSLT中的HTML标签,最终成为HTML,显示在浏览器上。典型的例子就是CSDN上的帖子。

2、XML作为微型数据库,这是最常见的数据型应用之一。我们利用相关的XML API(MSXML DOM、JAVA DOM等)对XML进行存取和查询。留言板的实现中,就经常可以看到用XML作为数据库。同时,这里要告诉一些新人,数据库和数据库系统,这两个概念是不同的。这里顺便提一下XML对数据库系统的影响。在新版本的传统数据库系统中,XML成为了一种数据类型。和“传统”相对的就是一种新形态的数据库,完全以XML相关技术为基础的数据库系统。目前比较知名的eXist。

3、作为信息传递的载体。为什么说是载体呢?因为这些应用虽然还是以XML为基本形态,但是都已经发展出具有特定意义的格式形态。最典型的就是WEB SERVICE,将数据包装成XML来传递,但是这里的XML已经有了特定的规格,即SOAP。不过这里还不得不说AJAX,AJAX的应用中,相信也有一部分的应用是以自定义XML为数据,不过没有成为工业标准,这里不做详述。

4、应用程序的配置信息数据。最典型的就是J2EE配置WEB服务器时用的web.XML。这个应用估计是很容易理解的了。我们只要将需要的数据存入XML,然后在我们的应用程序运行载入,根据不同的数据,做相应的操作。这里其实和应用2,有点类似,所不同的在于,数据库中的数据变化是个常态,而配置信息往往是较为静态,缺少变化的。

5、其他一些文档的XML格式。如WORD、EXCEL等。

6、保存数据间的映射关系。如Hibernate。

这几种常见应用中,我们还可以根据其应用广泛程度,分为:自定义XML和特定意义XML。在1和2就是属于自定义XML的范畴;3至6则属于特定意义XML,或者说是XML的延伸。

这里介绍的6种应用,基本涵盖了XML的主要用途。总之,XML是一种抽象的语言,它不如传统的程序语言那么具体。要深入的认识它,应该先从它的应用入手,选择一种你需要的用途,然后再学习如何使用。

 

 

 

 

总结:

①UTF-8 也是 HTML5, CSS, JavaScript, PHP, 和 SQL 的默认编码;

②极力传递的理念是:元数据(有关数据的数据)应当存储为属性,而数据本身应当存储为元素;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326153209&siteId=291194637