Validate XML Element Attribute value using XSD

15,038

Solution 1

You need to define an attribute with a simpleType representing a restriction in order to enforce the attribute value being member of a defined set of values.

Imagine you have the following xml:

<?xml version="1.0"?>
<Test Script="path/to/script" Parameter="Y" xmlns="http://www.example.org" />

You can enforce that they type attribute has a value of foo or bar using this xsd:

<?xml version="1.0"?>
<xsd:schema 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:tns="http://www.example.org"
  targetNamespace="http://www.example.org"
  elementFormDefault="qualified"
  attributeFormDefault="unqualified"
>
    <xsd:element name="Test">
      <xsd:complexType>
        <xsd:attribute name="Script" type="xsd:string" use="required"/>
        <!-- the following has no type attribute. It's type is 
             defined in the simpleType child -->
        <xsd:attribute name="Parameter" use="optional">
          <xsd:simpleType>
            <!-- define a set of xsd:strings
                 as possible values -->
            <xsd:restriction base="xsd:string">
              <xsd:enumeration value="Y"/>
              <xsd:enumeration value="N"/>
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:attribute>
      </xsd:complexType>
    </xsd:element>
</xsd:schema>

Solution 2

You have to define your Parameter attribute like this

      <xs:simpleType name="yesno">
            <xs:restriction base="xs:string">
                <xs:enumeration value="Y" />
                <xs:enumeration value="N" />
            </xs:restriction>
      </xs:simpleType>



      <xs:attribute name="Parameter" type="xs:yesno" use="optional"/>

Solution 3

try this:

<xs:element name="Test" maxOccurs="unbounded">
                <xs:complexType>
                            <xs:attribute name="Script">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="abc.sh">
                                        </xs:enumeration>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:attribute>
                              <xs:attribute name="Parameter">
                                 <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="Y|N" >
                                        </xs:pattern>
                                    </xs:restriction>
                                 </xs:simpleType>    
                              </xs:attribute>
                </xs:complexType>
</xs:element>  
Share:
15,038
logan
Author by

logan

web designer ....

Updated on June 05, 2022

Comments

  • logan
    logan almost 2 years

    I have following XML Element. I need to validate whether Parameter Attribute is holding only Y or N in following XML element

    <Test Script="abc.sh" Parameter="Y"/>
                   **OR**
    <Test Script="abc.sh" Parameter="N"/>
    

    My XSD is :

        <xs:element name="Test" minOccurs="0">
            <xs:complexType>
                <xs:attribute name="Script" type="xs:string" use="required"/>
                <xs:attribute name="Parameter" type="xs:string" use="optional"/>                    
            </xs:complexType>
        </xs:element>
    

    Currently this XSD is not validating whether Parameter is holding Y or N