• XML Schema使用技巧——unique


    XML Schema使用技巧——unique

     

         XML Scheam允许指定某个元素或属性的值在一定得范围内是唯一的。为了指定元素或属性值的唯一性,可以使用<xs:unqiue>元素,使用方法为选择一组xml示例元素作为范围,然后依据上下文关系定义一个field,这里的field就是要指定的唯一性的元素或属性。

         1、元素唯一性

    1. <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.test.com" xmlns:tn="http://www.test.com">
          <xs:element name="books">
              <xs:complexType>
                  <xs:sequence>
                      <xs:element name="bookName" type="xs:string" maxOccurs="unbounded"/>            
                  </xs:sequence>        
              </xs:complexType>
              <xs:unique name="bookUnique">
                  <xs:selector xpath="hn:bookName"></xs:selector>
                  <xs:field xpath="."></xs:field>
              </xs:unique>
          </xs:element>
      </xs:schema>

          <xs:unique name="bookUnique"> 是一个unique声明,其中“bookUnique”是unique的名字。

          <xs:selector xpath="hn:bookName"></xs:selector> 用来选择一组元素,作为元素或属性唯一性的范围。xpath属性是XML路径表达式。由于<xs:selector>声明在元素<books>的声明中,可以将当前路径深度看做<books>,代表books元素下的所有bookName。

          <xs:field xpath="."></xs:field> 用来指定需要指定唯一性的元素或属性。

    示例xml:

    1. <?xml version="1.0" encoding="UTF-8"?>
      <tn:books xmlns:tn="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <tn:bookName>Test Book</tn:bookName>
          <tn:bookName>Test</tn:bookName>
          <tn:bookName>Test Book</tn:bookName>
      </tn:books>

    在XMLSpy提示错误信息:

     image

             2、属性唯一性

    1. <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.test.com" xmlns:tn="http://www.test.com">
          <xs:element name="books">
              <xs:complexType>
                  <xs:sequence>
                      <xs:element name="book" maxOccurs="unbounded">
                          <xs:complexType>
                              <xs:sequence>
                                  <xs:element name="title" type="xs:string"/>
                                  <xs:element name="price" type="xs:decimal"/>                   
                              </xs:sequence> 
                              <xs:attribute name="id" type="xs:integer"/>                  
                          </xs:complexType>                
                      </xs:element>         
                  </xs:sequence>        
              </xs:complexType>
              <xs:unique name="bookUnique">
                  <xs:selector xpath="tn:book"/>
                  <xs:field xpath="@id"/>
              </xs:unique>
          </xs:element>
      </xs:schema>

             <xs:field xpath="@id"/>  表示对元素employee的id属性进行唯一性约束。

    xml实例:

    1. <?xml version="1.0" encoding="UTF-8"?>
      <tn:books xsi:schemaLocation="http://www.test.com t.xsd" xmlns:tn="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <tn:book id="1">
              <tn:title>a</tn:title>
              <tn:price>10</tn:price>
          </tn:book>
          <tn:book id="2">
              <tn:title>a</tn:title>
              <tn:price>10</tn:price>
          </tn:book>
          <tn:book id="1">
              <tn:title>a</tn:title>
              <tn:price>10</tn:price>
          </tn:book>
      /tn:books>

    XMLSpy错误信息:

    image

  • 相关阅读:
    HTTP Authorization
    php导出数组到csv格式demo
    nginx 配置ajax跨域访问php接口
    node.js和npm离线安装
    使用Docker快速搭建Nginx+PHP-FPM环境
    django中文件下载(HttpResponse)
    django 下载文件 无法正常打开
    C语言Review2_struct
    基础概念——回车换行
    C语言Review1_预处理器和宏
  • 原文地址:https://www.cnblogs.com/tian830937/p/5130765.html
Copyright © 2020-2023  润新知