XMLdocumentationstructure and verification

LearningXMLdocumentation 正确structure, including根元素, 子元素 and documentation声明, 以及XMLdocumentation verificationmethod and tool

XMLdocumentation basicstructure

一个格式良 good XMLdocumentation必须遵循specific structure规则, 这些规则ensuredXMLdocumentation readable 性 and 可解析性. 一个完整 XMLdocumentation通常package含以 under 部分:

  1. XML声明: 可选但推荐, 指定XMLversion and 编码
  2. documentationclass型声明 (DTD) : 可选, 定义documentation structure and 规则
  3. 根元素: 必须, package含所 has other元素 顶层元素
  4. 子元素: 根元素 in 元素, 形成层次structure
  5. property: providing元素 附加information
  6. 文本 in 容: 元素 in practicaldata
  7. comment: 用于添加说明, 不影响documentationstructure
完整 XMLdocumentationexample
<?xml version="1.0" encoding="UTF-8"?>
<!-- 这 is a XMLcomment -->
<library>
    <book id="B001" category="计算机">
        <title>XMLBasicstutorial</title>
        <author>张三</author>
        <year>2025</year>
        <price>99.00</price>
    </book>
    <book id="B002" category="文学">
        <title> small 说集</title>
        <author>李四</author>
        <year>2024</year>
        <price>89.00</price>
    </book>
</library>

格式良 good XMLdocumentation

一个格式良 good XMLdocumentation必须满足以 under 条件:

  • 必须 has 且仅 has 一个根元素
  • 所 has 元素必须正确嵌套
  • 所 has 元素必须 has 结束tag ( or using自闭合语法)
  • tag区分 big small 写
  • property值必须用引号括起来
  • 必须遵循XML命名规则
格式良 good and 格式不良 XML for 比
<!-- 格式良 good  XML -->
<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <title>XMLBasicstutorial</title>
        <author>张三</author>
    </book>
</books>

<!-- 格式不良 XML -->
<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <title>XMLBasicstutorial</book>
        </title>
        <author>张三</author>
</books>

XMLdocumentation声明

XMLdocumentation声明 is XMLdocumentation 可选部分, 但such as果package含, 必须放 in documentation 第一行. 它providing了关于XMLdocumentation basicinformation, helping解析器正确processingdocumentation.

documentation声明 语法

XMLdocumentation声明 basic语法
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

documentation声明 property

documentation声明可以package含三个可选property:

property describes 必需性 example值
version 指定XMLversion 必需 "1.0", "1.1"
encoding 指定documentation 字符编码 可选 "UTF-8", "GB2312", "ISO-8859-1"
standalone 指定documentation is 否依赖 out 部resource 可选 "yes", "no"

documentation声明 best practices

  • 始终package含XML声明, 明确指定version and 编码
  • usingUTF-8编码, 这 is XML 默认编码, 也 is 最广泛support 编码
  • 只 has 当documentation完全自package含, 不依赖任何 out 部resource时, 才usingstandalone="yes"
  • documentation声明必须放 in documentation 第一行, before 面不能 has 任何other in 容, including空格 and comment
documentation声明example
<!-- basicdocumentation声明 -->
<?xml version="1.0"?>

<!-- 指定编码 documentation声明 -->
<?xml version="1.0" encoding="UTF-8"?>

<!-- 完整 documentation声明 -->
<?xml version="1.0" encoding="UTF-8" standalone="no"?>

根元素

根元素 is XMLdocumentation 顶层元素, 它package含所 has other元素. 每个XMLdocumentation必须 has 且仅 has 一个根元素, 这 is XMLdocumentationstructure core要求.

根元素 特点

  • 每个XMLdocumentation必须 has 且仅 has 一个根元素
  • 根元素必须package含所 has other元素
  • 根元素 命名必须遵循XML元素 命名规则
  • 根元素可以package含property
  • 根元素通常具 has describes性名称, 反映documentation 主题
根元素example
<?xml version="1.0" encoding="UTF-8"?>

<!-- 根元素 is  <library> -->
<library>
    <book>...</book>
    <book>...</book>
</library>

<!-- 根元素 is  <students> -->
<students>
    <student>...</student>
    <student>...</student>
</students>

<!-- 根元素 is  <company>, package含property -->
<company id="C001" name="科技公司">
    <department>...</department>
    <employee>...</employee>
</company>

XML元素 嵌套规则

XML元素必须正确嵌套, 不能交叉嵌套. 这意味着一个元素必须完全package含 in 另一个元素in, or 者完全 in 另一个元素之 out .

正确 嵌套example

正确 元素嵌套
<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <title>XMLBasicstutorial</title>
        <author>
            <first-name>张</first-name>
            <last-name>三</last-name>
        </author>
        <chapters>
            <chapter>第一章: XMLIntroduction</chapter>
            <chapter>第二章: XML语法</chapter>
        </chapters>
    </book>
</books>

error 嵌套example

error 元素嵌套
<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <title>XMLBasicstutorial</book>
        </title> <!-- error: 交叉嵌套 -->
        <author>
            <first-name>张</last-name> <!-- error: tag不匹配 -->
            <last-name>三</first-name> <!-- error: tag不匹配 -->
        </author>
    </book>
</books>

嵌套 best practices

  • 始终保持元素 正确嵌套, 避免交叉嵌套
  • using合理 indent, improvingcode readable 性
  • 保持适当 嵌套深度, 避免过度嵌套
  • usingdescribes性 元素名称, 使嵌套structure易于understanding
  • for complex datastructuredesign清晰 层次structure

XMLdocumentationverification

XMLdocumentationverification is checkXMLdocumentation is 否符合specific规则 and 约束 过程. 除了格式良 good out , XMLdocumentation还可以throughDTD (documentationclass型定义) or XML Schemaforverification, 确保其符合预期 structure and dataclass型.

verification class型

  • 格式良 good 性verification: checkXMLdocumentation is 否符合XML语法规则
  • has 效性verification: checkXMLdocumentation is 否符合specific DTD or Schema定义

格式良 good 性verification

格式良 good 性verification确保XMLdocumentation遵循basic XML语法规则, including:

  • has 且仅 has 一个根元素
  • 所 has 元素都 has 结束tag ( or using自闭合语法)
  • 元素正确嵌套, 不交叉
  • property值用引号括起来
  • 遵循XML命名规则

has 效性verification

has 效性verification确保XMLdocumentation符合specific structure and dataclass型规则, 这些规则由DTD or XML Schema定义. has 效 XMLdocumentation必须首先 is 格式良 good .

verificationtool

has many 种tool可用于verificationXMLdocumentation, including:

in 线verificationtool

  • W3C XMLverification器: W3Cproviding in 线XMLverificationtool, supportDTD and Schemaverification
  • XML Lint: in 线XMLverification and formattool
  • FreeFormatter XML Validator: support many 种XMLverification格式

桌面verificationtool

  • Notepad++: 带 has XML插件, support语法check and verification
  • XMLSpy: 专业 XML编辑 and verificationtool
  • Oxygen XML Editor: functions强 big XMLDevelopmentenvironment

commands行tool

  • xmllint: Linux and macOSsystem自带 XMLverificationtool
  • XMLStarlet: commands行XMLtoolpackage, supportverification, 转换 and query

usingxmllintverificationXML

xmllint is a 强 big commands行tool, 用于verification and formatXMLdocumentation. 以 under is 一些常用 xmllintcommands:

xmllintcommandsexample
# checkXML格式 is 否良 good 
xmllint --noout file.xml

# usingDTDverificationXML
xmllint --valid --noout file.xml

# usingSchemaverificationXML
xmllint --schema schema.xsd --noout file.xml

# formatXMLdocumentation
xmllint --format file.xml

# 显示XMLdocumentation structure
xmllint --tree file.xml

实践case: creation并verificationXMLdocumentation

casedescribes

creation一个XMLdocumentation, 用于store公司员工information, 然 after usingxmllinttoolverification该documentation 格式良 good 性.

implementation步骤

  1. creation一个名 for employees.xml XMLfile
  2. 添加XML声明, 指定version and 编码
  3. creation根元素<company>
  4. in 根元素 in 添加 many 个<employee>子元素
  5. for 每个员工添加<id>, <name>, <department> and <salary>子元素
  6. usingxmllintverificationdocumentation

最终code

<?xml version="1.0" encoding="UTF-8"?>
<company>
    <employee>
        <id>E001</id>
        <name>张三</name>
        <department>techniques部</department>
        <salary>8000</salary>
    </employee>
    <employee>
        <id>E002</id>
        <name>李四</name>
        <department>市场部</department>
        <salary>7500</salary>
    </employee>
    <employee>
        <id>E003</id>
        <name>王五</name>
        <department>人事部</department>
        <salary>7000</salary>
    </employee>
</company>

verification过程

usingxmllintverificationdocumentation:

# verificationXML格式 is 否良 good 
$ xmllint --noout employees.xml

# such as果没 has 输出, 说明documentation格式良 good 

# formatXMLdocumentation
$ xmllint --format employees.xml > formatted_employees.xml

互动练习

练习1: 识别XMLdocumentationstructureissues

以 under XMLdocumentation存 in 哪些structureissues?
<?xml version="1.0" encoding="UTF-8"?>
<!-- 这 is a  has issues XMLdocumentation -->
<students>
    <student id=1001>
        <name>张三</name>
        <age>20</age>
        <gender>男
    </student>
    <student id="1002">
        <name>李四</name>
        <age>21</age>
        </gender>
        <gender>女</gender>
    </student>
</students>

XMLdocumentation存 in 以 under structureissues:

  1. 第一个student元素 idproperty值没 has 用引号括起来: id=1001 应该 is id="1001"
  2. 第一个student元素 gender元素没 has 结束tag: <gender>男 缺 few </gender>
  3. 第二个student元素in存 in many 余 gender结束tag: </gender> 没 has for 应 开始tag

练习2: creation格式良 good XMLdocumentation

creation一个格式良 good XMLdocumentation, 用于store以 under 产品information:
  • 产品1: ID=P001, 名称=智能手机, 价格=3999, class别=电子产品
  • 产品2: ID=P002, 名称=笔记本电脑, 价格=7999, class别=电子产品
  • 产品3: ID=P003, 名称=无线耳机, 价格=999, class别=配件
<?xml version="1.0" encoding="UTF-8"?>
<products>
    <product id="P001">
        <name>智能手机</name>
        <price>3999</price>
        <category>电子产品</category>
    </product>
    <product id="P002">
        <name>笔记本电脑</name>
        <price>7999</price>
        <category>电子产品</category>
    </product>
    <product id="P003">
        <name>无线耳机</name>
        <price>999</price>
        <category>配件</category>
    </product>
</products>