Table of Contents
XML 用于创建新的 Internet 语言
引用" >实体引用
XML 命名规则
对象?" >什么是 XMLHttpRequest 对象
创建 XMLHttpRequest 对象
创建 XMLHttpRequest 对象的语法:
编辑器?" >为什么使用 XML 编辑器?
Home Backend Development XML/RSS Tutorial Introduction and development of XML (pictures and text)

Introduction and development of XML (pictures and text)

Mar 17, 2017 pm 05:38 PM

概述

<span style="font-family:Verdana">XML:Extensible Markup Language(可扩展标记语言)的缩写,是用来定义其它语言的一种元语言,其前身是SGML(Standard Generalized <br>Markup Language,标准通用标记语言)。它没有标签集(tag set),也没有语法规则(grammatical rule),但是它有句法规则(syntax rule)。<br>任何XML文档对任何类型的应用以及正确的解析都必须是良构的(well-formed),即每一个打开的标签都必须有匹配的结束标签,不得<br>含有次序颠倒的标签,并且在语句构成上应符合技术规范的要求。XML文档可以是有效的(valid),但并非一定要求有效。所谓有效文档是指其符合其文档<br>类型定义(DTD)的文档。如果一个文档符合一个模式(schema)的规定,那么这个文档是"模式有效的(schema valid)"。</span>
Copy after login

XML 是一种标记语言,很类似 HTML ,XML 的设计宗旨是传输数据,而非显示数据 ,XML 标签没有被预定义。您需要自行定义标签。 XML 被设计为具有自我描述性。 XML 是 W3C 的推荐标准

XML 用于创建新的 Internet 语言

很多新的 Internet 语言是通过 XML 创建的:

其中的例子包括:

XHTML - 最新的 HTML 版本 ,WSDL - 用于描述可用的 web service ,WAP 和 WML - 用于手持设备的标记语言 ,RSS - 用于 RSS feed 的语言 ,

RDF 和 OWL - 用于描述资源和本体 ,SMIL - 用于描述针针对 web 的多媒体

下面来看一个常用案例:

<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title> 
  <author>Giada De Laurentiis</author> 
  <year>2005</year> 
  <price>30.00</price> 
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title> 
  <author>J K. Rowling</author> 
  <year>2005</year> 
  <price>29.99</price> 
</book>
<book category="WEB">
  <title lang="en">Learning XML</title> 
  <author>Erik T. Ray</author> 
  <year>2003</year> 
  <price>39.95</price> 
</book>
</bookstore>
Copy after login

可以用如下图来解释:

Introduction and development of XML (pictures and text)

实体引用

在 XML 中,一些字符拥有特殊的意义。

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

这样会产生 XML 错误:

<message>if salary < Introduction and development of XML (pictures and text)000 then</message>
Copy after login

为了避免这个错误,请用实体引用来代替 "<" 字符:

<message>if salary < Introduction and development of XML (pictures and text)000 then</message>
Copy after login

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

Introduction and development of XML (pictures and text)

XML 命名规则

XML 元素必须遵循以下命名规则:

  • 名称可以含字母、数字以及其他的字符

  • 名称不能以数字或者标点符号开始

  • 名称不能以字符 “xml”(或者 XML、Xml)开始

  • 名称不能包含空格

可使用任何名称,没有保留的字词。

什么是 XMLHttpRequest 对象

XMLHttpRequest 对象用于在后台与服务器交换数据。

XMLHttpRequest 对象是开发者的梦想,因为您能够:

  • 在不重新加载页面的情况下更新网页

  • 在页面已加载后从服务器请求数据

  • 在页面已加载后从服务器接收数据

  • 在后台向服务器发送数据

所有现代的浏览器都支持 XMLHttpRequest 对象。

创建 XMLHttpRequest 对象

所有现代浏览器 (IE7+、Firefox、Chrome、Safari 以及 Opera) 都内建了 XMLHttpRequest 对象。

通过一行简单的 JavaScript 代码,我们就可以创建 XMLHttpRequest 对象。

创建 XMLHttpRequest 对象的语法:
xmlhttp=new XMLHttpRequest();
Copy after login

为什么使用 XML 编辑器?

当今,XML 是非常重要的技术,并且开发项目正在使用这些基于 XML 的技术:

  • 用 XML Schema 定义 XML 的结构和数据类型

  • 用 XSLT 来转换 XML 数据

  • 用 SOAP 来交换应用程序之间的 XML 数据

  • 用 WSDL 来描述网络服务

  • 用 RDF 来描述网络资源

  • 用 XPath 和 XQuery 来访问 XML 数据

  • 用 SMIL 来定义图形

XSLT

XSLT(eXtensible Stylesheet Language Transformation)是一种用来转换XML文档结构的语言。

XSL的处理过程:

首先是根据XML文档构造源树,然后根据XSL规则将源树转换为结果树,生成结果树后,对其进行解释,格式化为一种适合显示、打印或是播放的格式。

XSLT和CSS的比较:

CSS

XSLT

使用简单

使用复杂

不能排序、添加或删除元素

可排序、添加或删除元素

不能访问文档除元素外的其它信息

能访问其它信息

使用内存比较少

使用较多内存和处理器能力

与XML语法不同

语法与XML相同

XPath基础

XPath就是一种专门用来在XML文档中定位和查找信息的语言,通过XPath可对XML文档中的元素和属性进行遍历,是高级XML应用的基础 。

XPath主要有四种数据类型:(Introduction and development of XML (pictures and text))节点集.(2)布尔类型.(3)字符串类型.(4)数值类型

在XPath中,可以将节点划分为七种节点类型:(Introduction and development of XML (pictures and text))根节点(Root Node).(2)元素节点(Element Nodes).(3)文本节点(Text Nodes).(4)属性节点(Attribute Nodes).(5)命名空间节点(Namespace Nodes).(6)处理指令节点(Processing Instruction Nodes).(7)注释节点(Comment Nodes).

XPath节点关系:(Introduction and development of XML (pictures and text))父(parent).(2)子(children).(3)同胞(sibling).(4)先辈(ancestor).(5)后代(descendant).

XPath表达式

XPath将XML文档看作由节点构成的层次树,通过编写XPath表达式来定位树中特定的节点

XPath路径表达式:

表达式

描述

nodename

选取此节点的所有子节点

/

从根节点选取,绝对路径,表示当前文档的根节点

//

从任意节点选取,相对路径,表示当前文档所有的节点

.

选取当前节点

..

选取当前节点的父节点

@

选取属性

例子

<?xml version="Introduction and development of XML (pictures and text).0" encoding="GB23Introduction and development of XML (pictures and text)2"?>
<bookstore>
    <book>
        <title lang="en">RESTful Web Services</title>
        <price>29.00</price>
    </book>
    <book>
        <title lang="zh">C#编程基础</title>
        <price>46.00</price>
    </book>
</bookstore>
Copy after login

解析:

bookstore

选取 bookstore 元素的所有子节点

/bookstore

选取根节点bookstore元素

/bookstore/book/price

选取bookstore元素下所有book元素的所有price元素

/bookstore/book/*

选取/bookstore/book的所有子元素

bookstore/book

选取bookstore元素下所有的book子元素

//bookstore

选取文档中所有的bookstore元素,无论它在什么层次

bookstore//book

选取在bookstore元素下所有的book元素,无论它们位于bookstore之下的什么位置

/bookstore/*/price

选取bookstore的子元素中,包含有price作为子元素的元素

//*

选取文件中的所有元素

//@lang

选取所有名为 lang 的属性

谓语表达式:

路径表达式

描述

/bookstore/book[last()]

选取属于bookstore子元素的最后一个book元素

/bookstore/book[last()-Introduction and development of XML (pictures and text)]

选取属于bookstore子元素的倒数第二个book元素

/bookstore/book[position()<3]

选取前两个属于bookstore元素的子元素的book元素

//title[@lang]

选取所有拥有名为lang的属性的title元素

//title[@lang="en"]

选取所有title元素,且这些元素拥有值为 eng 的 lang 属性

/bookstore/book[price>Introduction and development of XML (pictures and text)5.00]

选取所有bookstore元素的book元素,且其中的price元素的值须大于Introduction and development of XML (pictures and text)5.00

/bookstore/book[price>Introduction and development of XML (pictures and text)5.00]/title

选取所有bookstore元素中的book元素的title元素,且其中的price元素的值须大于Introduction and development of XML (pictures and text)5.00

//title[@*]

选取所有带有属性的 title 元素

如果需要选择一个以上的路径,可以在XPath表达式中使用“|”运算符:

路径表达式

描述

//book/title | //book/price

选取所有book元素的title和price元素

//title | //price

选取文档中所有的title和price元素

/bookstore/book/title | //price

选取所有属于bookstore元素的book元素的title元素,以及文档中所有的price元素

XPath表达式的常用运算符:

运算符

描述

实例

|

计算两个节点集

//book | //cd,返回所有带有 book 和 cd 元素的节点集

+

加法

6 + 4,

-

减法

6 - 4

*

乘法

6 * 4

p

除法

8 p 4

=

等于

price=Introduction and development of XML (pictures and text)5

!=

不等于

price!=Introduction and development of XML (pictures and text)5

<

小于

price

<=

小于或等于

price<=Introduction and development of XML (pictures and text)5

>

大于

price>Introduction and development of XML (pictures and text)5

>=

大于或等于

price>=Introduction and development of XML (pictures and text)5

or

price=Introduction and development of XML (pictures and text)5 or price=35

and

price>Introduction and development of XML (pictures and text)5 or price<35

mod

计算除法的余数

5 mod 2

示例

private void buttonIntroduction and development of XML (pictures and text)_Click(object sender, EventArgs e)
          {
              XmlDocument doc = new XmlDocument();
              doc.Load(@"..\..\CD.xml");
  
              XmlElement root = null;
              root = doc.DocumentElement;
  
              XmlNodeList listNodes = null;
              listNodes = root.SelectNodes("/bookstore/book/price");
              foreach (XmlNode node in listNodes )
              {
                  richTextBoxIntroduction and development of XML (pictures and text).Text += node.InnerText + "\n";
              }
          }
Copy after login

 

结果:

29.00

46.00

The above is the detailed content of Introduction and development of XML (pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles