xml
<?xml version="1.0" encoding="UTF-8"?>
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="w3ctest.xsd">
<orderperson>George Bush</orderperson>
<shipto>
<name>John Adams</name>
<address>Oxford Street</address>
<city>London</city>
<country>UK</country>
</shipto>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
我写的是下面的 是ok的
schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="shiporderinfo">
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:string"/>
<xs:element name="price" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:element name="shiporder" type="shiporderinfo"/>
</xs:schema>
但可以使用 定义type-name 的方式简写整个 type 必须是 xs:schema 的直接元素
<xs:schema xmlns="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="orderidattr">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{6}"/>
</xs:restriction>
</xs:simpleType>
...
<xs:element name="shiporder" type="shiporderinfo"/>
</xs:schema>
php 验证函数
<?php
$dom = new DOMDocument;
$dom->load('w3ctest.xml');
libxml_use_internal_errors(true);
if($dom->schemaValidate("w3ctest.xsd")){
echo "validate";
}else{
var_dump(libxml_get_errors());
}
ref="引用" 的用法
<xs:element name="orderperson" type="xs:string"/>
<xs:element ref="orderperson"/>