Home > Net >  type checking based no attribute's name in XML/XSD
type checking based no attribute's name in XML/XSD

Time:06-10

I have to write an XSD to validate the type of element based on its name. I have tried the condition type assignement but I couldn't validate the value of the element. Thanks for your help in advance.

MY XSD

<xs:element name="PARAMS">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="param" maxOccurs="unbounded" minOccurs="0">
     <xs:alternative test="@name='Cste'" type="float"/>
     <xs:alternative test="@name='Unité'" type="string"/>
   </xs:element>
  </xs:sequence>
 </xs:complexType>
</xs:element>

MY XML

<PARAMS>
    <param name="Cste" type="Real" libelle="Constante">0.200000</param>
    <param name="Unit" type="String" libelle="Unité">bara</param>
</PARAMS>

CodePudding user response:

You're on the right lines, but

(a) make sure the schema processor is one that supports XSD 1.1 (many don't)

(b) your element declaration for param needs to permit the three attributes (name, type, libelle)

(c) the types need to be xs:float and xs:string with a namespace prefix.

If you get errors, please tell us what the errors are. We can always diagnose the problem more accurately if we know the symptoms.

  • Related