8.【xml schema】几点问题区分

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 的一个回答

参考

https://stackoverflow.com/questions/17422175/whats-the-difference-between-ref-and-type-in-an-xml-schema

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());
}
时间: 2024-08-02 16:41:03

8.【xml schema】几点问题区分的相关文章

XML详解:第二部分 XML Schema

声明:原创作品,转载时请注明文章来自SAP师太技术博客:www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4290897.html XML Schema. 22 全局/局部声明/定义... 23 模式与名称空间... 23 目标名称空间... 23 局部元素和属性的限定... 24 未声明目标名称空间... 26 在XML实例文档中引用Schema

xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance

http://blog.sina.com.cn/s/blog_4b6f8d150100nx3e.html http://blog.csdn.net/iaiti/article/details/42263891 http://blog.csdn.net/qian_348840260/article/details/5396061 xmlns ——是XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上一个n

xml与xml schema命名空间学习

首先,xml与xml schema(xsd)文件都是xml格式的文件,都遵循相同的命名空间规则 在schema元素中通过xmlns指定约束文件位置 如下面的xsd文件中的代码 <?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example

DTD与XML Schema

XML是一种数据格式化语言,用于描述复杂的数据结构.而DTD/Schema是规范XML文档,对XML文档的书写进行约束 XML DTD 一个例子 DTD约束文档 <!ELEMENT books (book)> <!ELEMENT book (name,price)> <!ELEMENT name (#PCDATA)> <!ELEMENT price (#PCDATA)> 在DTD文档中定义book Tag的相关属性 XML文档 <?xml versio

疯狂XML学习笔记(7)-----------XML Schema

XML Schema 是基于 XML 的 DTD 替代者. XML Schema 可描述 XML 文档的结构. XML Schema 语言也可作为 XSD(XML Schema Definition)来引用. 您应当具备的基础知识 在继续学习之前,您需要对下面的知识有基本的了解: HTML / XHTML XML 以及 XML 命名空间 对 DTD 的基本了解 如果您希望首先学习这些项目,请在 首页 访问这些教程. 什么是 XML Schema? XML Schema 的作用是定义 XML 文档

XML Schema验证

XML Schema验证 一.什么事Schema(XSD) XML Schema是微软定义的一套用来验证XML技术.是一套预先规定的XML元素和属性创建的,这些元素和属性定义了XML文档的结构和内容模式. DTD的局限性: 1.DTD不遵循XML语法. 2.DTD的数据类型有限,与数据库类型不一致. 3.DTD不可以扩展. 4.DTD是不支持命名空间的. Schema的优势: 1.Schema是一种XML语法结构,编写更加方便. 2.Schema可以支持数据类型. 3.Schema是可以扩展的.

XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式

XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式 http://blog.csdn.net/gdjlc/article/details/11374787 2013-09-08 12:16 2824人阅读 评论(0) 收藏 举报 分类: XML(5) 版权声明:本文为博主原创文章,未经博主允许不得转载. XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合

Xml Schema:C#访问在complextype中插入新元素

最近用c#写Xml Schema代码,找了很久也找不到如何在已有的complextype中插入新的element,最后我充分发挥自己的聪明才智,哈哈,终于从...中找到了灵感. XmlSchemaSet xmlSSet; foreach( XmlSchema schema in xmlSSet.Schemas()) { XmlSchemaElement xmlSchRootElement = (XmlSchemaElement )schema.Items[0];//schema里面只能看到根节点

XML Schema编程快速入门

一.XML Schema简介 XML Schema 使用一套预先定义好元素和属性 二.XML Schema的定义和使用 1.编写数据XML文件 2.编写Schema文件 .xsd 引入W3C名称空间:xmlns="http://www.w3.org/2001/XMLSchema" 编写schema内容,对每一个元素,用一个<elementname=""></element>表示,包含其他元素的元素称为复杂元素,需要加<complexTy

使用Asp.Net中的XmlValidatingReader来验证XML Schema.

这段时间,我正在学习XML,书上介绍使用Xerces-C或Xerces-J来验证XML文档的Schema或DTD,结果,发现上面的工具不好用. 后来,只有放下书,到书店看看,有没有其他什么书能带来帮助.当检到Professiona ASP.NET XML with C# 这本书时,里面讲到了使用System.Xml 和 System.Xml.Schema命名空间下的XmlTextReader类和XmlValidatingReader类来验证带有Schema或DTD的XML文档.便买下来了. 回寝