1.simpleType 和 complexType的区别
这两个使用的范围不一样,一个是简单类型包括 xs:element 和 xs:attribute
在设置简单类型限制(restriction)的时候使用xs:simpleType
2.simpleContent 和 complexContent
simpleContent 是对简单元素的扩展 complexContent 是对复杂元素的扩展
<xs:element name="message">
<xs:complexType>
<xs:complexContent>
<xs:extension base="personinfo">//限定的是下面complexType定义的类型
<xs:sequence>
<xs:element name="h"/>
</xs:sequence>
<xs:attribute name="date" type="xs:date"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
和
<xs:element name="message">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="date" type="xs:date"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
参考 http://coresun.blog.sohu.com/73396995.html
<xsd:simpleContent>和<xsd:complexContent>这两个标签都不能单独使用,经常和 <xsd:extension>、<xsd:retriction>、<xsd:complexType>、<xsd:simpleType>嵌套使用。
demo
xs:simpleType name="price">
<xs:restriction base="xs:decimal">
<xs:maxInclusive value="100"/>
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="currency">
<xs:restriction base="xs:string">
<xs:enumeration value="$"/>
<xs:enumeration value="Y"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="complex1">
<xs:simpleContext>
<xs:extension base="price">
<xs:attribute name="simple_price" type="currency">
</xs:extension>
</xs:simpleContext>
</xs:complexType>
上面例子中 先定义了两个simpleType (用来做simple 元素的限制) 然后通过extension的方式扩展 上面 complexType 定义的元素内容0-100之间 attribute 是 simple_price 是 ($|Y)的一个
<xs:complexType name="complex2">
<xs:simpleContent>
<xs:restriction base="complex1">
<xs:maxInclusive value="1000"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
上面的更新了 complex1 将最大值扩大到1000
DEMO2
xs:complexType name="complex1">
<xs:sequence>
<xs:element name="street" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="complex2">
<xs:complexContent>
<xs:extension base="complex1">
<xs:sequence>
<xs:element name="state" type="xs:string"/>
<xs:element name="country" type="xs:string" fixed="US"/>
</xs:sequence>
<xs:attribute name="" type="xs:string"/ >
</xs:extension>
</xs:complexContent>
</xs:complexType>
#complex2 有四个元素 而且含有一个attribute
也可以使用restriction
<xs:complexType name="complex2">
<xs:complexContent>
<xs:restriction base="complex1">
<xs:sequence>
<xs:element name="street" type="xs:string" fixed="wow"/>
<xs:element name="city" type="xs:string"/>
</xs:sequence>
<xs:attribute name="" type="xs:string"/ >
</xs:restriction>
</xs:complexContent>
</xs:complexType>
street 设定成 fixed值
3.ref include 的使用
两个语法
There are two methods for this. <xsd:include schemaLocation="pathToFile" /> should be used for including files of the same namespace. <xsd:import namespace="namespace" schemaLocation="pathToFile" /> should be used for include files in a different namespace. Usually you will specify the namespace given as the targetNamespace of the imported schema.
另外一个回答参考
The fundamental difference between include and import is that you must use import to refer to declarations or definitions that are in a different target namespace and you must use include to refer to declarations or definitions that are (or will be) in the same target namespace.
参考 https://web.archive.org/web/20070804031046/http://xsd.stylusstudio.com/2002Jun/post08016.htm
找到一个中文博文也说明问题
http://blog.csdn.net/tuolingss/article/details/8557328
关于 ref 和type 的区别 参考了一下 stackoverflow 的一个回答
参考
Using ref=".." you are "pasting" existing element/attribute defined on the other place. Using type=".." you are assigning some structure (defined in complextype/simpletype) to new element/attribute. Look at following:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tst="test" targetNamespace="test">
<xs:complexType name="Root">
<xs:sequence>
<xs:element ref="tst:Child" />
<xs:element name="Child2" type="tst:ChildType" />
</xs:sequence>
<xs:attribute ref="tst:AttRef" />
<xs:attribute name="Att2" type="tst:AttType" />
</xs:complexType>
<xs:complexType name="ChildType">
<xs:attribute ref="tst:AttRef" />
</xs:complexType>
<xs:element name="Child">
</xs:element>
<xs:simpleType name="AttType">
<xs:restriction base="xs:string">
<xs:maxLength value="10" />
</xs:restriction>
</xs:simpleType>
<xs:attribute name="AttRef" type="xs:integer" />
</xs:schema>
Yes I think it could be said in this way (if "instance" mean some top-level element declared somewhere in xsd). Another difference: when you use type then you can have two element with different name with the same structure. When you use ref then you have elements with either same name or structure everywhere. – Jirka ?.
简单的讲 ref 就是直接调用 已经定义好的 element 而 type 相当于应用了结构 name 肯定要自己写 和 integer string 一样的知识使用我们自己定义的 simpleType 或者 complexType
另外一个参考 https://coderanch.com/t/127287/ref-type-XML-Schema
我测试的例子 如下
usaddress.xsd 只是一个很简单的element
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="USaddress">
<xs:complexType>
<xs:sequence>
<xs:element name="street1" type="xs:string" minOccurs="0"/>
<xs:element name="street2" type="xs:string" minOccurs="0"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
#address.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="usaddress.xsd"/>
<xs:element name="address">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element ref="USaddress"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
注意上面的xs:include 因为我们在同一个namespace 里面 所已只需要名字即可 ref用法不再赘述
PHP 验证函数
<?php
$dom = new DOMDocument;
$dom->load(‘test1.xml‘);
libxml_use_internal_errors(true);
if($dom->schemaValidate("address.xsd")){
echo "validate";
}else{
var_dump(libxml_get_errors());
}