转:http://blog.sina.com.cn/s/blog_68d733750101n7rd.html
(1)带命名空间
xml文件node.xml:
<?xml
<note
xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't
</note>
对应的XSD文件node.xsd:
<xs:schema
//using SML Schema to defaine gramme
//targetNamespace
<?xml
<xs:schema
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">
<xs:element
<xs:complexType>
<xs:sequence>
<xs:element
<xs:element
<xs:element
<xs:element
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
(2)不带命名空间
xml文件node.xml:
<?xml
<note
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocatio
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't
</note>
对应的XSD文件node.xsd:
<?xml
<xs:schema
elementFormDefault="qualified">
<xs:element
<xs:complexType>
<xs:sequence>
<xs:element
<xs:element
<xs:element
<xs:element
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
主要存在两区别:
(1)指定命名空间
带命名空间的XSD文件要指定命名空间targetNamespace,同样xml文件也要设置该命名空间xmlns;
不带命名空间时XSD文件和xml文件都不用设置
(2)设置XSD验证的方式不一样
带命名空间的xml用
不带命名空间的noNamespaceSchemaLocatio
但是不管有没有自定义命名空间,xml和xsd文件都要放在同一目录下
_____________________________________________
例二:重点理解Schema文档使用自身定义类型
- <?xml
version="1.0"?> - <xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema" - targetNamespace="http://www.w3schools.com"
- elementFormDefault="qualified">
- <xsd:element
name="note"> -
<xsd:complexType> -
<xsd:sequence> -
<xsd:element name="to" type="xs:string"/> -
<xsd:element name="from" type="xs:string"/> - <xsd:element
name="heading" type="xs:string"/> -
<xsd:element name="body" type="xs:string"/> -
</xsd:sequence> -
</xsd:complexType> - </xsd:element>
- <xsd:element
name="Student" type="stu"/> -
<xsd:complexType name="stu"> -
<xsd:sequence> -
<xsd:element name="Name" type="xs:string"/> -
<xsd:element name="Class" type="xs:string"/> -
</xsd:sequence> -
</xsd:complexType> - </xsd:schema>
- <?xml
version="1.0"?> - <xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema" - targetNamespace="http://www.w3schools.com"
- xmlns:student="http://www.w3schools.com"
- elementFormDefault="qualified">
- <xsd:element
name="note"> -
<xsd:complexType> -
<xsd:sequence> -
<xsd:element name="to" type="xs:string"/> -
<xsd:element name="from" type="xs:string"/> - <xsd:element
name="heading" type="xs:string"/> -
<xsd:element name="body" type="xs:string"/> -
</xsd:sequence> -
</xsd:complexType> - </xsd:element>
- <xsd:element
name="Student" type="student:stu"/> -
<xsd:complexType name="stu"> -
<xsd:sequence> -
<xsd:element name="Name" type="xs:string"/> -
<xsd:element name="Class" type="xs:string"/> -
</xsd:sequence> -
</xsd:complexType> - </xsd:schema>