`
bdk82924
  • 浏览: 553162 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论
阅读更多

一.名词解释:

    1.  XSD是XML Schemas Definition的缩写,也是XML Schema文件的扩展名,与XML Schema、DTD、RELAX NG都是schema的一种。XML是数据的集合。xsd是来规定XML的;xsd验证XML数据是否正确,比如你的XML数据要写进数据库,如果不验证,可能就会有很多问题。当你的XML通过XSD验证后,就可以用XSL进行格式化,生成HTML,矢量图形等。

    2. 在写一个xml文档时怎么知道一个元素里应该有几个子元素或者属性呢,每个子元素或者属性都是什么类型呢(如数字或者字符串),而定义这些约束的就需要DTD或者schema,这个过程就是数据建模。

    3. schema和DTD就像一个模板一样,xml文档必须符合模板才能算是有效的xml文档,而xsl是负责显示xml文档。

    4. XSD是W3C推荐的XML SCHEMA标准,SCHEMA即描述XML的结构和元素关系的规则模式,他规定了一个xml文档可以使用那些元素、元素的类型以及一些限制规则。schema和DTD一样,都是描述xml的,只不过SCHEMA的内容更丰富,更具有扩展性,同时他本身也是一个xml文档,更方便解析。

二.应用

(本段来自网络收集,详见http://blog.csdn.net/ydsakyclguozi/archive/2008/08/06/2774652.aspx

一个简单的XML Schema文档

<xs:schema xmlns:xs=”http://www.w2.org/2001/XMLSchema”>

<xs:element type=“xs:nonNegativeInteger”>

</xs:element>

</xs:schema>

 

在这个Schema里面定义了一个元素:quantity,它的类型是nonNegativeInteger(非负整数),xmlnsSchema的命名空间。

正确

<quantity>5</quantity>

错误

<quantity>-4</quantiy>

 

Schema中的类型

Schema中主要包括三种部件:元素(element)、属性(attribute)、注释(notation)

这三种基本的部件还能组合成以下的部件:

①类型定义部件: 简单类型和复合类型

②组部件

③属性组部件

 

1.简单类型

⑴简单类型

XML Schema中定义了一些内建的数据类型,这些类型可以用来描述元素的内容和属性值。

一个元素中如果仅仅包含数字、字符串或其他数据,但不包括子元素,这种被称为简单类型。

如同上例中元素quantity就是一个简单类型。它的元素内容必须是非负整数,不包括任何属性和子元素。

所有内建的简单类型

原始类型

stringbooleandecimalfloatdoubledurationdatetimetimedategYearMonthgYeargMonthDay dDaygMonthhexBinarybase64Binaryany URIQnameNOTATION

衍生类型(括号中为基类型)

normalizedString(string)language(tonken)token(normalizedString)NMTOKEN(token)Name(token)NCName(Name)ID(NCName)IDREF(NCName)IDREFS(list of IDREF)ENTITY(NCName)ENTITIES(list of ENTITY)integer(decimal)nonPositiveInteger(integer)negativeInteger(noPositiveInteger)long(integer)int(long)short(int)byte(short)nonNegativeInteger(integer)unsignedLong(nonNegativeInteger)unsignedInt(unsignedLong)unsignedShort(unsignedInt)unsignedByte(unsignedShort)positiveInteger(nonNegativeInteger)

 

①简单类型的例子_1

<xsd:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>

<simpleType

<restriction base=’integer’>

<minInclusive value=’2’ />

<maxInclusive value=’5’ />

</restriction>

</simpleType>

<element type=’ quantity Type’ />

</xsd:schema>

实例中我们先创建了一个简单类型:quantityType,它是从integer继承过来的,minInclusivemaxInclusive定义了它的最小值2和最大值5。最后我们定义元素quantity的类型为quantityType

正确

<quantity>3</quantity>

错误

<quantity>10</quantity>

<qauntity>aaa</quantity>

 

使用restriction我们可以限制只能接受一定数值或者只能接受一定文字。

基本方面

equalorderedboundedcardinalitynumeric

限制方面

lengthminLengthmaxLengthpatternenumeration whiteSpacemaxInclusivemaxExclusiveminInclusiveminExclusivetotalDigitsfractionDigits

 

②简单类型的例子_

2

 

 

 

<xsd:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>

<simpleType

<restriction base=’xsd:string’>

<pattern value=’/\d{3}-[A-Z]{2}/’ />

</restriction>

</simpleType>

<element ourSKU’ type=’ SKUType’ />

</xsd:schema>

这个SKU的类型的取值:3个数字后面根着一个连字号接着跟着两个大写的英文字母。pattern后面跟的是正则表达式。有关正则表达式的语法请参阅其他书籍。

正确

<ourSKU>123-AB</ourSKU>

错误

<ourSKU>abc -AB</ourSKU>

<ourSKU>123-ab</ourSKU>

 

③简单类型的例子_

3

 

 

 

<xsd:simpleType

<xsd:restriction base=”xsd:string”>

<xsd:enumeration value=”AK”/>

<xsd:enumeration value=”AL”/>

<xsd:enumeration value=”AR”/>

<!-- and so on …-->

</xsd:restriction>

</xsd:simpleType>

<element type=”USState”/>

 

这是一个用来描述美国州名的类型USState,通过enumeration来列出所有州名,取值时就只能取里面列出的州名。

<!-- and so on ...-> 这是一个注释语句。

正确

<statename>AK</statename>

错误

<statename>Alaska</statename>

 

⑵列表类型

 

 

 

 

<!-- Schema Fragment -->

<xsd:simpleType

       <xsd:list itemType=”Integer” />

</xsd:simple>

<element type=”listOfIntType” />

 

list可以用来定义列表类型,listOfIntType这个类型被定义为一个Integer的列表,元素listOfMyInt的值可以几个整数,他们之间用空格隔开。

正确

<listOfMyInt>1 5 15037 95977 95945</listOfMyInt>

错误

<listOfMyInt>1 3 abc</listOfMyInt>

 

⑶联合类型

 

 

 

 

<!-- Schema Fragment -->

<xsd:simpleType

       <xsd:union memberTypes=”USState listOfMyIntType” />

</xsd:simple>

<element type=”zipUnion” />

 

实例中用union来定义了一个联合类型,里面的成员类型包括USStatelistOfMyIntType,应用了联合类型的元素的值可以是这些原子类型或列表类型中的一个类型的实例,但是一个元素实例不能同时包含两个类型。

正确

<zips>CA</zips>

<zips>95630 95977 95945</zips>

<zips>AK</zips>

错误

<zips>CA 95630</zips>

 

 

 

 

 

⑷匿名类型

<xsd:element

<xsd:simpleType>

<xsd:restriction base=”xsd:positiveInteger”>

<xsd:maxExclusive value=”100” />

</xsd:restriction>

</xsd:simpleType>

</xsd:element>

 

前面我们在定义元素类型时总是先定义一个数据类型,然后再把元素的type设成新定义的数据类型。如果这个新的数据类型只会用一次,我们就可以直接设置在元素定义里面,而不用另外来设置。

如实例中元素quantity的类型就是一个从199的整数。

这种新的类型没有自己的名字的定义方法我们称之为匿名类型定义。

 

2.复合类型

<xsd:element

<xsd:complexType>

<xsd:simpleContent>

<xsd:extension base=”xsd:decimal”>

<xsd:attribute type=”xsd:string” />

</xsd:simpleContent>

</xsd:complexType>

</xsd:element>

 

前面我们所讲到的都是属于简单类型,即元素里面只有内容,不再包括属性或者其它元素。接下来我们要让元素里面包含属性和其它元素,称之为复合类型。

实例中我们用complexType表示这是一个复合类型(这里我们是用匿名类型定义的)。simpleContent表示这个元素下面不包括子元素,extension表示这个元素值是decimal的,attribute来设置它的一个属性currency,类型为string.

正确

<internationalPrice currency="EUR">423.46</internationalPrice>

 

⑴混合内容

 

 

 

 

<xsd:element

<xsd:complexType mixed=”true”>

<xsd:sequence>

<xsd:element type=”xsd:string” />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

 

同样,我们采用了匿名类型方式来定义一个元素salutation。我们注意到在complexType后面多了一个mixed="true",这表明这是一个混合类型:里面既有元素本身的内容,又有其它子元素。name元素就是salutation的子元素。

正确

<salutation>Dear Mr.<name>Robert Smith</name>.</salutation>

错误

<salutation>Dear Mr.</salutation>

sequence表示子元素出现的顺序要和schema里面的顺序一样。和sequence对应的有choiceall两种方式。

 

⑵空内容

 

 

 

 

<xsd:element

<xsd:complexType>

<xsd:complexContent>

<xsd:restricion base=”xsd:anyType”>

<xsd:attribute type=”xsd:string” />

<xsd:attribute type=”xsd:decimal” />

</xsd:restriction>

</xsd:complexContent>

</xsd:complexType>

</xsd:element>

 

有的时候元素根本没有内容,他的内容模型是空。为了定义内容是空的类型,我们可以通过这样的方式:首先我们定义一个元素,它只能包含子元素而不能包含元素内容,然后我们又不定义任何子元素,依靠这样的方式,我们就能够定义出内容模型为空的元素。

实例中complexConet表示只包含子元素,然后我们定义了两个属性currencyvalue,但是却不定义任何子元素。

正确

<internationalPrice currency="EUR" value="/423.46"/>

错误

<internationalPrice currency="EUR" value="/423.46">

Here is a mistake!

</interanationPrice>

 

还有更简洁的方法定义:

<xsd:element >

<xsd:complexType>

<xsd:attribute type="xsd:string"/>

<xsd:attribute type="xsd:decimal"/>

</xsd:complexType>

</xsd:element>

 

因为一个不带有simpleContent 或者complexContent的复合类型定义,会被解释为带有类型定义为anyTypecomplexContent,这是一个默认的速记方法,所以这个简洁的语法可以在模式处理器中工作。

 

anyType

 

 

 

 

<xsd:element type=”xsd:anyType”/>

<xsd:element />

 

一个anyType类型不以任何形式约束其包含的内容。我们可以象使用其他类型一样使用anyType,如实例中第一个语句,这个方式声明的元素是不受约束的。所以元素的值可以为423.46,也可以为任何其他的字符序列,或者甚至是字符和元素的混合。实际上,anyType是默认类型,所以上面的可以被重写为第二个语句。

如果需要表示不受约束的元素内容,举例来说在元素包含散文,其中可能需要嵌入标签来支持国际化的表示,那么默认的声明(无约束)或者有些微约束的形式会很合适。

 

⑷注释

 

 

 

 

<xsd:element

<xsd:annotation>

    <xsd:documentation xml:lang=”en”>

           Element declared with anonymous type

    </xsd:documentation>

</xsd:annotation>

<xsd:complexType>

<xsd:annotation>

margin: 0cm 0cm 0pt

分享到:
评论

相关推荐

    根据xsd批量生成java类

    根据xsd批量生成java类,觉得很方便也很酷。但是有时候xsd生成的java类中含有汉字,结果总是有些问题。 可是xjc命令参数又没有encoding参数之类的。在网上搜了一通,忽然发现了一个好东东:xjc是由...

    xsd文件 xsd1.4,xsd2.0,xsd3.0

    xsd文件 xsd1.4,xsd2.0,xsd3.0

    dubbo.xsd文件

    &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" targetNamespace=...

    xml生成xsd工具

    xml生成xsd 使用方法:java -jar trang.jar EchoRequest.xml EchoRequest.xsd

    dubbo找不到dubbo.xsd报错

    构建dubbo项目的时候会遇到: Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is strict, but no ... 3) the root element of the document is not &lt;xsd:schema&gt;.

    dubbo.xsd阿里巴巴开源xsd文件

    &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:tool="http://www.springframework.org/schema/tool" targetNamespace=...

    dubbo.xsd下载

    1.解决:dubbo找不到dubbo.xsd报错 2.Eclipse配置 选择windows--&gt;preferrence--&gt;xml-&gt;xmlcatalog--&gt;add-&gt;catalog entry --&gt;file system,选择模版文件(本地下载的)后,修改key值为...

    dubbo的.xsd文件分享

    解压后dubbo.xsd 即可用,解压后dubbo.xsd 即可用,解压后dubbo.xsd 即可用,解压后dubbo.xsd 即可用

    dubbo.xsd文件下载

    dubbo配置xml文件报错“Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:...dubbo.xsd文件下载

    java生成xsd,xml示例

    java生成xsd,xml示例

    XML转XSD\DTD工具

    XML转XSD\DTD工具 W3C XML Schema XSD 工具

    使用xmlbeans讲xsd生成Jar文件

    1、下载xmlbeans-2.3.0.jar。 2、创建配置文件my.xsdconfig。 3、java方法如下: ... "D:\\2\\dcc.xsd",//xsd文件存放位置 "D:\\2\\my.xsdconfig"};//xsd配置描述文件 SchemaCompiler.main(a); } }

    xsd生成xml工具

    通过xsd文件生成样例xml的工具,有完整源代码

    根据XSD检查XML并修复

    附件是我写的一个demo程序,该程序的功能是根据一个定义好的XSD文件去检查指定的XML文件是否满足XSD的约束。 这里的约束比标准的约束要弱一些,比如:这里的约束不限制元素出现的顺序,只关心有没有这个元素。 我...

    XSD使用dom4j校验XML

    XSD使用dom4j校验XML

    dubbo xsd的支持

    dubbo.xsd文件。使用dubbo时标签解析不了,需要这个文件来做解析

    dubbo.xsd文件路径

    dubbo项目xml文件报错,不支持dubbo元素,需要在本地安装dubbo.xsd文件。

    spring-context-3.2.xsd

    spring-context-3.2.xsd

    xml转xsd,trang.jar最新版

    这是从google的地址下载的trang,最新版,根目录下的jar可根据xml生成xsd文件,下面介绍使用: 把jar放在 d:\trang 下 假设在 d:\trang 下有一个xml文件person.xml 打开windows命令行,将当前位置cd变更到 d:\trang ...

Global site tag (gtag.js) - Google Analytics