zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    React元素渲染
    初识JSX
    微信小程序复制文本到剪切板
    微信小程序报错request:fail url not in domain list
    小程序,通过自定义编译条件,模拟推荐人功能
    积分抵扣逻辑
    微信小程序 switch 样式
    tomcat 配置开启 APR 模式
    tomcat8 传输json 报错 Invalid character found in the request target. The valid characters are defined in RFC 3986
    c++数组初始化误区
  • 原文地址:https://www.cnblogs.com/ideaplusl/p/2842340.html
Copyright © 2011-2022 走看看