1
votes

I have this XML, and the name attribute values should be unique like A, B, etc:

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

This is my XSD file:

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

I added xs:unique element for the field and selector, but it gives the following error:

SAX Exception: s4s-elt-invalid-content.1: The content of preDefinedRecipeType is invalid. Element unique is invalid, misplaced, or occurs too often.

I think I am doing something wrong, but I am new to XSD and do not see it.

2

2 Answers

1
votes

Your xs:unique is misplaced. See Where to place xs:unique constraint in XSD?

Applied to your case, the following XSD will require that preDefinedRecipe/@name be unique with preDefinedRecipes:

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>
-2
votes

I think you need to declare the data type of the name attribute to be xs:ID instead of xs:string: https://www.w3.org/TR/xmlschema11-2/#ID

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

According to that XSD 1.1 spec, an XSD 1.1. processor will enforce the uniqueness constraint (and an XSD 1.0 processor probably will not).