XML基础

本文介绍了XML的基础知识,包括其语法、命名空间的应用及如何使用XML Schema (XSD) 定义文档结构。详细解释了XSD的各种约束条件及其实例演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文的所有内容都来自于W3C school,只是负责把知识整理下


一、XML语法


XML主要是用来做数据交换,为异构的系统提供一个统一的交换方式。下面有一个简单的XML样例
<?xml version="1.0" encoding="ISO-8859-1"?>
<note id = "101">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

第一行是XML的声明,用来定义XML的版本和编码方式,<note>是XML的元素(element),id是XML的属性名,101是对应的属性值。

下面我简单的罗列下XML的约束条件:
  1. 所有的XML元素都必须关闭。
  2. XML的标签对大小写敏感,比如<Note>和<note>表示不同的标签。
  3. XML必须正确地嵌套。
  4. XML的属性值必须加引号(单引号或者双引号)。
  5. 对于特殊字符必须要转义。转义表如下:
  6. &lt;<小于
    &gt;>大于
    &amp;&和号
    &apos;'单引号
    &quot;"引号

二、XML命名空间

XML中的命名空间主要是为了解决元素冲突
这个 XML 文档携带着某个表格中的信息:
<table>
   <tr>
   <td>Apples</td>
   <td>Bananas</td>
   </tr>
</table>


这个 XML 文档携带有关桌子的信息(一件家具):
<table>
   <name>African Coffee Table</name>
   <width>80</width>
   <length>120</length>
</table>

假如这两个 XML 文档被一起使用,由于两个文档都包含带有不同内容和定义的 <table> 元素,就会发生命名冲突。
XML 解析器无法确定如何处理这类冲突。

对于这种问题,可以采用命名空间来解决。
默认的命名空间(Default Namespaces)
为元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作。
请使用下面的语法:
xmlns="namespaceURI"
对于上面的XML,可以这样定义:
<table xmlns="http://www.w3.org/TR/html4/">
   <tr>
   <td>Apples</td>
   <td>Bananas</td>
   </tr>
</table>

<table xmlns="http://www.w3school.com.cn/furniture">
   <name>African Coffee Table</name>
   <width>80</width>
   <length>120</length>
</table>

二、XML Schema

XML Schema 是基于 XML 的 DTD 替代者。
XML Schema 描述 XML 文档的结构。
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)

一个简单的 XML 文档:
请看这个名为 "note.xml" 的 XML 文档:

<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

DTD 文件
下面这个例子是名为 "note.dtd" 的 DTD 文件,它对上面那个 XML 文档的元素进行了定义:
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
第 1 行定义 note 元素有四个子元素:"to, from, heading, body"。
第 2-5 行定义了 to, from, heading, body 元素的类型是 "#PCDATA"。

XML Schema
下面这个例子是一个名为 "note.xsd" 的 XML Schema 文件,它定义了上面那个 XML 文档的元素:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">

<xs:element name="note">
    <xs:complexType>
      <xs:sequence>
	<xs:element name="to" type="xs:string"/>
	<xs:element name="from" type="xs:string"/>
	<xs:element name="heading" type="xs:string"/>
	<xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

对 DTD 的引用
此文件包含对 DTD 的引用:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "http://www.w3school.com.cn/dtd/note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>

对 XML Schema 的引用
此文件包含对 XML Schema 的引用:
<?xml version="1.0"?>
<note
xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>


三、XML Schema之 XSD

此段转自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html

XSD (xml Schema Definition)

Xml Schema的用途

1.  定义一个Xml文档中都有什么元素

2.  定义一个Xml文档中都会有什么属性

3.  定义某个节点的都有什么样的子节点,可以有多少个子节点,子节点出现的顺序

4.  定义元素或者属性的数据类型

5.  定义元素或者属性的默认值或者固定值

Xml Schema的根元素:

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 表示数据类型等定义来自w3

targetNamespace="http://www.w3schools.com" 表示文档中要定义的元素来自什么命名空间

xmlns="http://www.w3schools.com"表示此文档的默认命名空间是什么

elementFormDefault="qualified"> 表示要求xml文档的每一个元素都要有命名空间指定

……定义主体部分……

</xs:schema>

如何定义一个简单元素

<xs:element  此处表示要定义一个元素

name=”color” 表示要定义元素的名称

type=”xs:string”  表示要定义元素的数据类型

default=”red” 表示定义元素的默认值

fixed=”red”/> 表示要定义元素的固定值,此元素只可以取“red”值

以上定义了一个简单元素,元素实例:<color>red</color>

如何定义一个属性

<xs:attribute

         name=”birthday” 表示要定义属性的名字

         type=”xs:date” 表示要定义属性的数据类型

         default=”2001-01-11” 表示要定义属性的默认值

         fixed=”2001-01-11” 表示要定义属性的固定值

         use=”required”/> 表示此属性是否是必须指定的,即如果不指定就不符合Schema,默认没有use=”required”属性表示属性可有可无

如何定义元素或者属性值的限制

1.最大值最小值限制

<xs:element name="age">

<xs:simpleType>

<xs:restriction base="xs:integer">

<xs:minInclusive value="0"/> 大于等于0<xs: minExclusive>表示最小值但是不包括指定值

 <xs:maxInclusive value="120"/> 小于等于120<xs: maxExclusive>

</xs:restriction>

</xs:simpleType>

</xs:element>

2.枚举限制,指只能在指定的几个值中取值

         <xs:element name="car" type="carType"/>

<xs:simpleType name="carType">

  <xs:restriction base="xs:string">

    <xs:enumeration value="Audi"/>

    <xs:enumeration value="Golf"/>

    <xs:enumeration value="BMW"/>

  </xs:restriction>

</xs:simpleType>

3.模式(pattern)限制 ,指字符串的格式必须满足制定的匹配模式

例子

说明

<xs:element name="letter">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-z]"/>
  </xs:restriction>
</xs:simpleType>

</xs:element>

表示只能在小写字母中取一个值

<xs:element name="initials">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[A-Z][A-Z][A-Z]"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

表示必须是三个大写字母

<xs:element name="initials">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

表示必须是三个字母,可以是大写或小写的

<xs:element name="choice">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[xyz]"/>
  </xs:restriction>
</xs:simpleType>

</xs:element>

表示必须是xyz中的一个

<xs:element name="prodid">
<xs:simpleType>
  <xs:restriction base="xs:integer">
    <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

表示数字的范围是0-99999

<xs:element name="letter">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="([a-z])*"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

表示必须是0或者多个小写字符组成的序列

<xs:element name="letter">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="([a-z][A-Z])+"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

表示必须是多个字母。

<xs:element name="gender">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="male|female"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

表示是male或者female中的一个

<xs:element name="password">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-zA-Z0-9]{8}"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

表示必须是8个字母数字字符

4.字符串长度的限制

<xs:element name="password">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:length value="8"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

长度必须是8。

<xs:element name="password">

<xs:simpleType>

  <xs:restriction base="xs:string">

    <xs:minLength value="5"/>

    <xs:maxLength value="8"/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

表示长度在5-8之间

6. 对于空白字符的限制

示例

说明

<xs:element name="address">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="preserve"/>
  </xs:restriction>
</xs:simpleType>

</xs:element>

保留原样,表示xml处理器不会移除或者替换任何空白字符

<xs:element name="address">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="replace"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

指回车,换行,Tab都会被替换成空格处理

<xs:element name="address">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="collapse"/>
  </xs:restriction>
</xs:simpleType>
</xs:element> 

去掉多于一个空格,和html中处理方式相同

 

如何定义复杂类型

复杂类型是指定义元素中包含属性或者子元素的类型

1. 定义只包含子元素的复杂类型

<xs:element name="person">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="firstname" type="xs:string"/>

      <xs:element name="lastname" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

2. 定义只包含属性的复杂类型

<xs:element name="product" type="prodtype"/>

<xs:complexType name="prodtype">

  <xs:attribute name="prodid" type="xs:positiveInteger"/>

</xs:complexType>

3. 定义只包含内容的复杂类型

<xs:element name="shoesize" type="shoetype"/>

<xs:complexType name="shoetype">

  <xs:simpleContent>

    <xs:extension base="xs:integer">

      <xs:attribute name="country" type="xs:string" />

    </xs:extension>

  </xs:simpleContent>

</xs:complexType>

4. 定义包含内容和子元素混合的复杂类型

<xs:element name="letter">

  <xs:complexType mixed="true">

    <xs:sequence>

      <xs:element name="name" type="xs:string"/>

      <xs:element name="orderid" type="xs:positiveInteger"/>

      <xs:element name="shipdate" type="xs:date"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

以上定义对应的Xml

<letter>

Dear Mr.<name>John Smith</name>.

Your order <orderid>1032</orderid>

will be shipped on <shipdate>2001-07-13</shipdate>.

</letter>

5. 定义包含属性和子元素的复杂类型

 

使用指示器

在Xsd中的指示器包括

1. 顺序指示器

1) All

指示子元素可以以任何顺序出现,并且每一个元素都必须出现一次

<xs:element name="person">

  <xs:complexType>

    <xs:all>

      <xs:element name="firstname" type="xs:string"/>

      <xs:element name="lastname" type="xs:string"/>

    </xs:all>

  </xs:complexType>

</xs:element>

2) Choice

指示子元素中可以出现一个或者另一个

<xs:element name="person">

  <xs:complexType>

    <xs:choice>

      <xs:element name="employee" type="employee"/>

      <xs:element name="member" type="member"/>

    </xs:choice>

  </xs:complexType>

</xs:element>

3) Sequence

指示子元素必须按照顺序出现

<xs:element name="person">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="firstname" type="xs:string"/>

      <xs:element name="lastname" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

2. 出现次数指示器minOccurs,maxOccurs

<xs:element name="person">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="full_name" type="xs:string"/>

      <xs:element name="child_name" type="xs:string"

      maxOccurs="10" minOccurs="0"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

3. 组指示器(group Indicators)

用来定义相关的一组元素

<xs:group name="persongroup">

  <xs:sequence>

    <xs:element name="firstname" type="xs:string"/>

    <xs:element name="lastname" type="xs:string"/>

    <xs:element name="birthday" type="xs:date"/>

  </xs:sequence>

</xs:group>

<xs:element name="person" type="personinfo"/>

<xs:complexType name="personinfo">

  <xs:sequence>

    <xs:group ref="persongroup"/>

    <xs:element name="country" type="xs:string"/>

  </xs:sequence>

   </xs:complexType>

用来定义一组相关的属性

<xs:attributeGroup name="personattrgroup">

  <xs:attribute name="firstname" type="xs:string"/>

  <xs:attribute name="lastname" type="xs:string"/>

  <xs:attribute name="birthday" type="xs:date"/>

</xs:attributeGroup>

<xs:element name="person">

  <xs:complexType>

    <xs:attributeGroup ref="personattrgroup"/>

  </xs:complexType>

</xs:element>

Any关键字

表示可以有任意元素

<xs:element name="person">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="firstname" type="xs:string"/>

      <xs:element name="lastname" type="xs:string"/>

      <xs:any minOccurs="0"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

anyAttribute关键字

<xs:element name="person">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="firstname" type="xs:string"/>

      <xs:element name="lastname" type="xs:string"/>

    </xs:sequence>

    <xs:anyAttribute/>

  </xs:complexType>

</xs:element>

substitutionGroup关键字

表示某一个元素和另一个替代元素定义相同

<xs:element name="name" type="xs:string"/>

<xs:element name="navn" substitutionGroup="name"/>

<xs:complexType name="custinfo">

  <xs:sequence>

    <xs:element ref="name"/>

  </xs:sequence>

</xs:complexType><xs:element name="customer" type="custinfo"/>

<xs:element name="kunde" substitutionGroup="customer"/>

文中的例子都来自w3school.


      
内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值