XML元素
XML元素 is XMLdocumentation basic构建块, 用于表示data structure and in 容. 一个XML元素由开始tag, in 容 and 结束tag组成, or 者 is a 自闭合tag.
元素 basicstructure
XML元素 basicstructure has 两种形式:
- package含 in 容 元素: 由开始tag, in 容 and 结束tag组成
- 空元素: 没 has in 容, using自闭合语法
<!-- package含 in 容 元素 -->
<book>
<title>XMLBasicstutorial</title>
<author>张三</author>
</book>
<!-- 空元素 -->
<br />
<image src="book.jpg" />
元素 命名规则
XML元素 名称必须遵循以 under 规则:
- 名称可以package含字母, number, 连字符 (-) , under 划线 (_) and 点 (.)
- 名称不能以number or 标点符号开头
- 名称不能package含空格
- 名称区分 big small 写 (例such as:
<Book>and<book>is 不同 元素) - 名称应该具 has describes性, 避免using保留字
<!-- has 效 元素名称 --> <book>...</book> <book_title>...</book_title> <book-title>...</book-title> <book.title>...</book.title> <!-- 无效 元素名称 --> <1book>...</1book> <!-- 不能以number开头 --> <book title>...</book title> <!-- 不能package含空格 --> <book@title>...</book@title> <!-- 不能package含特殊字符 -->
元素 嵌套规则
XML元素必须正确嵌套, 不能交叉嵌套. 每个开始tag必须 has 一个 for 应 结束tag, 并且元素 嵌套relationships必须层次分明.
<!-- 正确 嵌套 -->
<library>
<book>
<title>XMLBasicstutorial</title>
<author>张三</author>
</book>
</library>
<!-- error 嵌套 -->
<library>
<book>
<title>XMLBasicstutorial</book>
</title>
<author>张三</author>
</library>
根元素
每个XMLdocumentation必须 has 且仅 has 一个根元素, 所 has other元素都 is 根元素 子元素 or after 代元素. 根元素 is XMLdocumentation 顶层containers.
<?xml version="1.0" encoding="UTF-8"?>
<!-- 根元素 is <students> -->
<students>
<student>
<name>张三</name>
<age>20</age>
</student>
<student>
<name>李四</name>
<age>21</age>
</student>
</students>
空元素
空元素 is 没 has in 容 元素, 可以using自闭合语法表示. 自闭合tag以斜杠 (/) 结尾, 不需要单独 结束tag.
空元素通常用于表示没 has in 容但需要property 元素, or 者用于插入特殊标记.
<?xml version="1.0" encoding="UTF-8"?>
<document>
<title>XMLdocumentationexample</title>
<content>
<p>这 is a 段落. </p>
<br /> <!-- 换行符, 空元素 -->
<p>这 is 另一个段落. </p>
<image src="example.jpg" alt="examplegraph片" /> <!-- graph片元素, 空元素 -->
<hr /> <!-- 水平分隔线, 空元素 -->
</content>
</document>
XMLproperty
XMLproperty用于 for 元素providing附加information, 它们 in 元素 开始tagin定义. property由名称 and 值组成, 值必须用引号括起来.
property basic语法
property basic语法格式 for : name="value", 其inname is property名, value is property值.
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="001" category="计算机">
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
</book>
<book id="002" category="文学">
<title> small 说集</title>
<author>李四</author>
<year>2024</year>
</book>
</library>
property 命名规则
XMLproperty 命名规则 and 元素名称 规则相同:
- 名称可以package含字母, number, 连字符 (-) , under 划线 (_) and 点 (.)
- 名称不能以number or 标点符号开头
- 名称不能package含空格
- 名称区分 big small 写
property值 规则
XMLproperty值必须遵循以 under 规则:
- property值必须用引号 (单引号 or 双引号) 括起来
- such as果property值package含双引号, 则必须using单引号括起来, or 者using实体引用 "
- such as果property值package含单引号, 则必须using双引号括起来, or 者using实体引用 '
- property值不能直接package含 &, < or > 字符, 必须using实体引用
<!-- 正确 property值用法 --> <book title="XMLBasicstutorial">...</book> <book title='XML "advanced" tutorial'>...</book> <book title="XML & HTML tutorial">...</book> <book title="XML < HTML">...</book> <!-- error property值用法 --> <book title=XMLBasicstutorial>...</book> <!-- 缺 few 引号 --> <book title="XML "advanced" tutorial">...</book> <!-- 双引号嵌套error --> <book title="XML & HTML tutorial">...</book> <!-- & 未using实体引用 -->
元素 and property 区别
in XMLin, 元素 and property都可以用于表示data, 但它们 has 不同 用途 and best practices:
| features | 元素 | property |
|---|---|---|
| 用途 | 表示主要data in 容 | 表示元素 附加information |
| structure | 可以package含文本, other元素 or 混合 in 容 | 只能package含文本值 |
| 嵌套 | 可以嵌套 many 层 | 不能嵌套 |
| 数量 | 一个元素可以 has many 个相同名称 子元素 | 一个元素in不能 has many 个相同名称 property |
| readable 性 | 更适合表示 complex datastructure, readable 性更 good | 适合表示 simple 元data, 语法更简洁 |
propertyusing best practices
in usingXMLproperty时, 应遵循以 under best practices:
- using元素表示data, usingproperty表示元data: data is documentation core in 容, 而元data is 关于data 附加information
- 避免过度usingproperty: such as果data可能需要进一步嵌套 or package含 complex in 容, 应using元素
- 避免usingpropertystore many 值data: many 值data应该using many 个子元素表示
- using唯一标识符serving asproperty: 例such as, idproperty用于标识元素 唯一性
- 保持property名 consistency: in 整个documentationinusing一致 命名约定
<!-- 推荐 用法: using元素表示data, property表示元data -->
<book id="001" category="计算机">
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
<price>99.00</price>
<tags>
<tag>XML</tag>
<tag>WebDevelopment</tag>
<tag>data交换</tag>
</tags>
</book>
<!-- 不推荐 用法: 过度usingproperty -->
<book id="001" category="计算机" title="XMLBasicstutorial" author="张三" year="2025" price="99.00" tags="XML,WebDevelopment,data交换">
</book>
实践case: designXML元素 and property
casedescribes
design一个XMLdocumentation, 用于storegraph书馆 graph书information, includinggraph书 basicinformation, 作者information and 馆藏information.
requirementsanalysis
需要store informationincluding:
- graph书basicinformation: 书名, ISBN, classification, 出版日期, 价格
- 作者information: 姓名, 国籍, 出生日期
- 馆藏information: 馆藏数量, 可借数量, where位置
design决策
- using
<library>serving as根元素 - using
<book>元素表示每本graph书 - for
<book>元素添加idpropertyserving as唯一标识符 - using子元素表示graph书 详细information, such as
<title>,<isbn>etc. - using
<authors>元素package含 many 个<author>子元素, 表示graph书 作者information - using
<馆藏>元素表示馆藏information
最终code
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="B001">
<title>XMLBasicstutorial</title>
<isbn>978-7-123-45678-9</isbn>
<category>计算机</category>
<publish-date>2025-01-01</publish-date>
<price>99.00</price>
<authors>
<author>
<name>张三</name>
<nationality>in国</nationality>
<birth-date>1980-01-01</birth-date>
</author>
</authors>
<馆藏>
<total-count>10</total-count>
<available-count>8</available-count>
<location>第三书架</location>
</馆藏>
</book>
<book id="B002">
<title>Pythonprogramming入门</title>
<isbn>978-7-987-65432-1</isbn>
<category>计算机</category>
<publish-date>2024-06-01</publish-date>
<price>89.00</price>
<authors>
<author>
<name>李四</name>
<nationality>in国</nationality>
<birth-date>1985-05-01</birth-date>
</author>
<author>
<name>王五</name>
<nationality>in国</nationality>
<birth-date>1990-03-15</birth-date>
</author>
</authors>
<馆藏>
<total-count>15</total-count>
<available-count>12</available-count>
<location>第四书架</location>
</馆藏>
</book>
</library>
互动练习
练习1: 识别 has 效 元素名称
<book><123book><book-title><book title><BookTitle><book.title><_book>
has 效 XML元素名称 is :
- has 效 :
<book>- 符合命名规则 - 无效 :
<123book>- 以number开头 - has 效 :
<book-title>- package含连字符, 符compliance则 - 无效 :
<book title>- package含空格 - has 效 :
<BookTitle>- 驼峰命名, 符compliance则 - has 效 :
<book.title>- package含点, 符compliance则 - has 效 :
<_book>- 以 under 划线开头, 符compliance则
练习2: 元素 and property 选择
- graph书 标题
- graph书 ISBN号
- graph书 classification
- graph书 作者 (可能 has many 个)
- graph书 出版日期
- graph书 价格
推荐 选择 and 理由:
- 元素: graph书标题 is coredata, 可能需要进一步processing or package含特殊字符
- property or 元素: ISBN号 is 标识information, 可以serving asproperty, 但such as果需要进一步verification or processing, 也可以serving as元素
- property or 元素: classificationinformation比较 simple , 可以serving asproperty, 但such as果classification体系 complex , 建议using元素
- 元素: 可能 has many 个作者, using元素可以方便地表示 many 个作者information
- 元素: 出版日期 is coredata, 可能需要for日期格式verification or processing
- 元素: 价格 is coredata, 可能需要for数值计算 or format
practicalapplicationin, 具体选择取决于data complex 性 and using场景, 但一般建议优先using元素表示coredata.
练习3: refactorXMLdocumentation
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="S001" name="张三" age="20" gender="男" score="85" courses="数学,英语,计算机"></student>
<student id="S002" name="李四" age="21" gender="女" score="92" courses="数学,英语,物理"></student>
</students>
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="S001">
<name>张三</name>
<age>20</age>
<gender>男</gender>
<score>85</score>
<courses>
<course>数学</course>
<course>英语</course>
<course>计算机</course>
</courses>
</student>
<student id="S002">
<name>李四</name>
<age>21</age>
<gender>女</gender>
<score>92</score>
<courses>
<course>数学</course>
<course>英语</course>
<course>物理</course>
</courses>
</student>
</students>