• 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

  • 相关阅读:
    滚动新闻的实现
    使用DLC(Dynamic Loader Compatibility)函数提取iPhone当前运营商信号的强度
    iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
    如何在Typora中使用流程图
    连接池的使用场景
    Mybatisplus入门教程(一)
    mybatis第一篇
    Mybatis需要注意的细节
    测试代码着色插件
    (转)企业Elearning的产业链与业务模式
  • 原文地址:https://www.cnblogs.com/ideaplusl/p/2842340.html
Copyright © 2020-2023  润新知