繁体   English   中英

如何使用 XSD 验证使 XML 中的属性唯一

[英]How to make an attribute unique in XML using XSD validation

我有这个 XML,并且name属性值应该是唯一的,如AB等:

<preDefinedRecipes>

       <preDefinedRecipe type="BREAD" name="A" ordinal="1" writerLevel="Service">

           <description>           <!-- // optional-->
           </description>

           <parameterDef ref="SUGAR_QTY" value="3" />
               <parameterDef ref="SALT_QTY" value="3" />
               <parameterDef ref="OIL_QTY" value="1" /> 

       </preDefinedRecipe>
       <preDefinedRecipe type="BREAD" name="B" ordinal="2" writerLevel="Service">

           <description>           <!-- // optional-->
           </description>

           <parameterDef ref="SUGAR_QTY" value="5" />
               <parameterDef ref="SALT_QTY" value="7" />
               <parameterDef ref="FLOUR_QTY" value="3" />

       </preDefinedRecipe>
   </preDefinedRecipes>

这是我的 XSD 文件:

<xs:complexType name="preDefinedRecipeType">
    <xs:sequence>
      <xs:element type="xs:string" name="description">
      </xs:element>

    <xs:element type="preDefinedRecipeParameterDefType" name="parameterDef" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="type" use="required"/>
    <xs:attribute type="xs:string" name="name" use="required"/>
    <xs:attribute type="xs:byte" name="fieldbusId" use="optional"/>
    <xs:attribute type="xs:byte" name="ordinal" use="required"/>
    <xs:attribute type="xs:string" name="writerLevel" use="required"/>

     <xs:unique name="uniqueTitle">
      <xs:selector xpath="recipeConfig/preDefinedRecipes/preDefinedRecipe"/>
      <xs:field xpath="@name"/>
      </xs:unique>
  </xs:complexType>


  <xs:complexType name="preDefinedRecipesType">
    <xs:sequence>
      <xs:element type="preDefinedRecipeType" name="preDefinedRecipe" maxOccurs="unbounded" minOccurs="0">
        <xs:annotation>
          <xs:documentation>Default recipe names shall be translatable, user defined recipes shall never be 
             translated to avoid collisions.</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

我为字段和选择器添加了xs:unique元素,但它给出了以下错误:

SAX 异常:s4s-elt-invalid-content.1: preDefinedRecipeType的内容无效。 元素unique无效、错位或出现太频繁。

我想我做错了什么,但我是 XSD 的新手,没有看到它。

您的xs:unique放错了位置。 请参阅在 XSD 中放置 xs:unique 约束的位置?

适用于您的情况,以下 XSD 将要求preDefinedRecipe/@namepreDefinedRecipes是唯一的:

XSD

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           elementFormDefault="qualified">
  <xs:complexType name="preDefinedRecipeType">
    <xs:sequence>
      <xs:element type="xs:string" name="description"/>   
      <xs:element type="preDefinedRecipeParameterDefType" name="parameterDef"
                  maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="type" use="required"/>
    <xs:attribute type="xs:string" name="name" use="required"/>
    <xs:attribute type="xs:byte" name="fieldbusId" use="optional"/>
    <xs:attribute type="xs:byte" name="ordinal" use="required"/>
    <xs:attribute type="xs:string" name="writerLevel" use="required"/>
  </xs:complexType>
  <xs:complexType name="preDefinedRecipeParameterDefType">
    <xs:attribute type="xs:string" name="ref" use="required"/>
    <xs:attribute type="xs:string" name="value" use="required"/>
  </xs:complexType>
  <xs:complexType name="preDefinedRecipesType">
    <xs:sequence>
      <xs:element type="preDefinedRecipeType" name="preDefinedRecipe" 
                  maxOccurs="unbounded" minOccurs="0">
        <xs:annotation>
          <xs:documentation>Default recipe names shall be translatable, user defined 
              recipes shall never be translated to avoid collisions.</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="preDefinedRecipes" type="preDefinedRecipesType">
    <xs:unique name="uniqueTitle">
      <xs:selector xpath="preDefinedRecipe"/>
      <xs:field xpath="@name"/>
    </xs:unique>
  </xs:element>
</xs:schema>

XML

<?xml version="1.0" encoding="UTF-8"?>
<preDefinedRecipes>
  <preDefinedRecipe type="BREAD" name="A" ordinal="1" writerLevel="Service">
    <description/>
    <parameterDef ref="SUGAR_QTY" value="3" />
    <parameterDef ref="SALT_QTY" value="3" />
    <parameterDef ref="OIL_QTY" value="1" /> 
  </preDefinedRecipe>
  <preDefinedRecipe type="BREAD" name="B" ordinal="2" writerLevel="Service">
    <description/>
    <parameterDef ref="SUGAR_QTY" value="5" />
    <parameterDef ref="SALT_QTY" value="7" />
    <parameterDef ref="FLOUR_QTY" value="3" />
  </preDefinedRecipe>
</preDefinedRecipes>

我认为您需要将 name 属性的数据类型声明为 xs:ID 而不是 xs:string: https : //www.w3.org/TR/xmlschema11-2/#ID

<xs:attribute type="xs:ID" name="name" use="required"/>

根据该 XSD 1.1 规范,XSD 1.1。 处理器将强制执行唯一性约束(而 XSD 1.0 处理器可能不会)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM