`
747017186
  • 浏览: 317354 次
社区版块
存档分类
最新评论

XSD 限定 / Facets

    博客分类:
  • xml
 
阅读更多

XSD 限定 / Facets

限定(restriction)用于为 XML 元素或者属性定义可接受的值。对 XML 元素的限定被称为 facet。

对值的限定

下面的例子定义了带有一个限定且名为 "age" 的元素。age 的值不能低于 0 或者高于 120:

<xs:element name="age">

<xs:simpleType>
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="120"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

对一组值的限定

如需把 XML 元素的内容限制为一组可接受的值,我们要使用枚举约束(enumeration constraint)。

下面的例子定义了带有一个限定的名为 "car" 的元素。可接受的值只有:Audi, Golf, BMW:

<xs:element name="car">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:enumeration value="Audi"/>
    <xs:enumeration value="Golf"/>
    <xs:enumeration value="BMW"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

上面的例子也可以被写为:

<xs:element name="car" type="carType"/>//单独提出来,直接引用,可以引用多个

<xs:simpleType name="carType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Audi"/>
    <xs:enumeration value="Golf"/>
    <xs:enumeration value="BMW"/>
  </xs:restriction>
</xs:simpleType>

注释:在这种情况下,类型 "carType" 可被其他元素使用,因为它不是 "car" 元素的组成部分。

对一系列值的限定

如需把 XML 元素的内容限制定义为一系列可使用的数字或字母,我们要使用模式约束(pattern constraint)。

下面的例子定义了带有一个限定的名为 "letter" 的元素。可接受的值只有小写字母 a - z 其中的一个:

<xs:element name="letter">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-z]"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

下一个例子定义了带有一个限定的名为 "initials" 的元素。可接受的值是大写字母 A - Z 其中的三个:

<xs:element name="initials">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[A-Z][A-Z][A-Z]"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

下一个例子也定义了带有一个限定的名为 "initials" 的元素。可接受的值是大写或小写字母 a - z 其中的三个:

<xs:element name="initials">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

下一个例子定义了带有一个限定的名为 "choice 的元素。可接受的值是字母 x, y 或 z 中的一个:

<xs:element name="choice">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[xyz]"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

下一个例子定义了带有一个限定的名为 "prodid" 的元素。可接受的值是五个阿拉伯数字的一个序列,且每个数字的范围是 0-9:

<xs:element name="prodid">

<xs:simpleType>
  <xs:restriction base="xs:integer">
    <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

对一系列值的其他限定

下面的例子定义了带有一个限定的名为 "letter" 的元素。可接受的值是 a - z 中零个或多个字母:

<xs:element name="letter">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="([a-z])*"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

下面的例子定义了带有一个限定的名为 "letter" 的元素。可接受的值是一对或多对字母,每对字母由一个小写字母后跟一个大写字母组成。举个例子,"sToP"将会通过这种模式的验证,但是 "Stop"、"STOP" 或者 "stop" 无法通过验证:

<xs:element name="letter">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="([a-z][A-Z])+"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

下面的例子定义了带有一个限定的名为 "gender" 的元素。可接受的值是 male 或者 female:

<xs:element name="gender">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="male|female"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

下面的例子定义了带有一个限定的名为 "password" 的元素。可接受的值是由 8 个字符组成的一行字符,这些字符必须是大写或小写字母 a - z 亦或数字 0 - 9:

<xs:element name="password">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-zA-Z0-9]{8}"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

对空白字符的限定

如需规定对空白字符(whitespace characters)的处理方式,我们需要使用 whiteSpace 限定。

下面的例子定义了带有一个限定的名为 "address" 的元素。这个 whiteSpace 限定被设置为 "preserve",这意味着 XML 处理器不会移除任何空白字符:

<xs:element name="address">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="preserve"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

这个例子也定义了带有一个限定的名为 "address" 的元素。这个 whiteSpace 限定被设置为 "replace",这意味着 XML 处理器将移除所有空白字符(换行、回车、空格以及制表符):

<xs:element name="address">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="replace"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

这个例子也定义了带有一个限定的名为 "address" 的元素。这个 whiteSpace 限定被设置为 "collapse",这意味着 XML 处理器将移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格):

<xs:element name="address">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="collapse"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

对长度的限定

如需限制元素中值的长度,我们需要使用 length、maxLength 以及 minLength 限定。

本例定义了带有一个限定且名为 "password" 的元素。其值必须精确到 8 个字符:

<xs:element name="password">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:length value="8"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

这个例子也定义了带有一个限定的名为 "password" 的元素。其值最小为 5 个字符,最大为 8 个字符:

<xs:element name="password">

<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:minLength value="5"/>
    <xs:maxLength value="8"/>
  </xs:restriction>
</xs:simpleType>

</xs:element> 

数据类型的限定

限定 描述
enumeration 定义可接受值的一个列表
fractionDigits 定义所允许的最大的小数位数。必须大于等于0。
length 定义所允许的字符或者列表项目的精确数目。必须大于或等于0。
maxExclusive 定义数值的上限。所允许的值必须小于此值。
maxInclusive 定义数值的上限。所允许的值必须小于或等于此值。
maxLength 定义所允许的字符或者列表项目的最大数目。必须大于或等于0。
minExclusive 定义数值的下限。所允许的值必需大于此值。
minInclusive 定义数值的下限。所允许的值必需大于或等于此值。
minLength 定义所允许的字符或者列表项目的最小数目。必须大于或等于0。
pattern 定义可接受的字符的精确序列。
totalDigits 定义所允许的阿拉伯数字的精确位数。必须大于0。
whiteSpace 定义空白字符(换行、回车、空格以及制表符)的处理方式。
分享到:
评论

相关推荐

    Schema教材

    schema文件详解: XML Schema 简介 为什么要使用 XML Schema? 如何使用 XSD? XSD 简易元素 XSD 属性 XSD 限定 / Facets XSD 复合元素

    javax.servlet.jar下载

    javax/servlet/resources/j2ee_web_services_client_1_1.xsd javax/servlet/resources/j2ee_web_services_1_1.xsd javax/servlet/resources/XMLSchema.dtd javax/servlet/resources/jsp_2_0.xsd javax/servlet/...

    castor实现xsd生成javabean所需jar

    加压放入D盘根目录 ...D:\xsd/commons-logging.jar org.exolab.castor.builder.SourceGeneratorMain -types j2 -i "D:\xsd\test\taxMlhl-wsyw-tyRequest.xsd" -package test -dest "D:/xsd/test" -f

    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=...

    cxf.apache.org/schemas/(jaxrs.xsd、jaxrs.xsd、core.xsd)文件下载

    CXF和Spring整合时,配置文件中所需要的CXF标签的 schema 文件,将所需的xsd文件配置到本地,可以解决部分IDE在编写配置文件时,无法自动提示的问题。

    使用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); } }

    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=...

    自定义XSD文件

    生成XML文件的定义规范文件XSD,能够严格定义XML文件的内容样式,对于刚学习XSD的人有一定的帮助。

    spring-aop-3.0.xsd spring-beans-3.0 spring-context-3.0.xsd spring-mvc-3.1.xsd

    spring-aop-3.0.xsd 这个下载之后可以直接放在eclipese的xml的catalog中配置,已到达eclipse可以自动提示xml的功能

    dubbo.xsd文件。最新约60kB

    引入alibabatech的 dubbo框架时,需要引用dubbo标签,dubbo.xsd 可以解决标签不识别的问题 。 最新dubbo.xsd 文件大小约60kB

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

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

    解决dubbo启动的时候报错,无法读取方案文档 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'

    解决启动dubbo项目的时候出现,无法读取方案文档 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd',其实在你本地把dubbo.jar文件解压,然后在META-INF下边就有个dubbo.xsd,就是他

    《电子商务技术基础》习题集及答案

    &lt;xsd:element name=”title” type=”xsd:string”/&gt; &lt;xsd:element name=”author” type=”xsd:string”/&gt; &lt;xsd:element name=”price” type=”xsd:decimal”/&gt; &lt;/xsd:sequnce&gt; &lt;/xsd:compexType&gt; &lt;/xsd:...

    xsd生成xml工具

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

    xsd2json:将XML模式转换为等效的JSON模式

    xsd2json 根据SWI-Prolog和约束处理规则(CHR)将XML模式转换为等效的JSON模式。 直接在Prolog或node.js中使用。 安装 首先,确保已安装 。 该模块期望swipl二进制文件位于PATH中的某个位置。 换句话说,键入: ...

    spring2.0声明式事务

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ...

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

    dubbo找不到dubbo.xsd报错

    dubbo找不到dubbo.xsd报错, cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document...

    由浅入深mit 老师教你学微分

    xmlns=...- &lt;schema&gt;IMS Content&lt;/schema&gt; &lt;schemaversion&gt;1.1&lt;/schemaversion&gt; - &lt;cwsp:sourceSystem&gt;OCW&lt;/cwsp:sourceSystem&gt; &lt;cwsp:profile&gt;CWSpace&lt;/cwsp:profile&gt; &lt;cwsp:profileVersion&gt;0.1&lt;/cwsp:...

Global site tag (gtag.js) - Google Analytics