Search code examples
xmlif-statementxsdconditional-statementsxml-validation

How to validate if one attribute value is always a value of another attribute in XML by XSD


I have an xm structure like this and I want to Validate the XMl with XSD

<recipeConfig>
    <recipeStructures> <!-- recipe template/ recipeStructureTypes-->

        <recipeStructureDef mnemonic="BREAD" title="Recipe.Bread"/>
        <recipeStructureDef mnemonic="CAKE" title="Recipe.Bread"/> 
         <recipeStructureDef mnemonic="PANCAKE" title="Recipe.Bread"/> 

     </recipeStructures>
     <preDefinedRecipes>
        <preDefinedRecipe type="BREAD" name="sweet bread"  ordinal="1" writerLevel="Service">
        <parameterDef ref="SUGAR_QTY" value="3" />
                <parameterDef ref="SALT_QTY" value="3" />
                <parameterDef ref="OIL_QTY" value="1" /> 

        </preDefinedRecipe>
      </preDefinedRecipes>
</recipeConfig>

I want to check if the <preDefinedRecipe type="BREAD"> is always a value of mnemonic attribute from recipeStructureDef mnemonic= ""

To put it in other words Type under 'preDefinedRecipe' should be always a value of attribute mnemonic in 'recipeStructureDef '

So i have to do some check. I am stuck here and i dont know how to proceed.Below is my part of code form XSD

  <xs:complexType name="preDefinedRecipeType">
    <xs:sequence>

    <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="ordinal" use="required"/>
    <xs:attribute type="xs:string" name="writerLevel" use="required"/>
  </xs:complexType>

Thanks in advance for the help


Solution

  • Look up xs:key and xs:keyRef.

    On the element declaration for recipeConfig, declare an xs:key with selection .//recipeStructureDef and field @mnemonic; and a corresponding xs:keyRef with selection .//predefinedRecipe and field @type.