> For the complete documentation index, see [llms.txt](https://jun-wang.gitbook.io/learnjava/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jun-wang.gitbook.io/learnjava/bian-cheng-yu-yan/xml/xml-zhi-shi-dian.md).

# XML知识点

XML即可扩展标记语言（e**X**tensible **M**arkup **L**anguage）。广泛被用于数据传输和数据存储。

## 实体引用

在xml中有一些字符具有特殊含义，比如"<"，如果放在xml元素中，会被解析为新元素的开始。为了避免这个错误，使用实体引用"<"来表示"<"，下面列举了几个常用的实体引用。

| 实体引用 | 代表值 | 含义  |
| ---- | --- | --- |
| \\<  | <   | 小于号 |
| \\>  | >   | 大于号 |
| \\&  | &   | and |
| \\'  | '   | 单引号 |
| \\"  | "   | 双引号 |

## XML命名空间

xml命名空间是为了避免xml元素命名冲突，使用前缀可以避免命名冲突，如下：

```markup
<!-- 两段xml内容都包含table，使用前缀来避免冲突 -->
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
```

在 XML 中使用前缀时，必须定义**命名空间**的URI，命名空间是在元素的开始标签的 **xmlns 属性**中定义的，命名空间声明的语法如下，xmlns:*前缀*="*URI*"。比如：

```markup
<root>

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

<f:table xmlns:f="http://www.w3cschool.cc/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

</root>
```

也可以在根元素中定义：

```markup
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3cschool.cc/furniture">

<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

</root>
```

使用`xmlns="namespaceURI"`可以定义默认的命令空间：

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

## XSL

XSL指扩展样式表语言（E**X**tensible **S**tylesheet **L**anguage)，它是一个 XML 文档的样式表语言。包含三部分：

* XSLT：指 XSL 转换，是一种用于将 XML 文档转换为 XHTML 文档或其他 XML 文档的语言。
* XPath：是一种用于在 XML 文档中进行导航的语言。
* XSL-FO ：一种用于格式化 XML 文档的语言。

## XML Schema

XML Schema负责描述xml文档的结构。

引用xml schema的格式：

```markup
<note
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
```

> 参考：
>
> <https://blog.51cto.com/5880861/1412883>
