• 6.【xml schema 】实例demo


    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"/>

    慢慢沉淀自己
  • 相关阅读:
    zTree 优秀的jquery树插件
    The underlying provider failed on open 问题解决
    HTML 5 <input> list 属性
    C#拖曳控件加载,bll报错问题
    把VS2010的智能代码提示和注解从英文变成中文
    progressBar的使用
    C++内存读写例子
    bash 管理小脚本
    KVM虚拟机配置笔记
    Ettercap 实施中间人攻击
  • 原文地址:https://www.cnblogs.com/martinding/p/7478960.html
Copyright © 2020-2023  润新知