'XSD - How to validate an attribute value of different strings with different custom validations?

Greetings fellow stackoverflowians,

Work is asking for XML validation by using XSD and I'm the lucky one who was selected to spear head this effort (I have no XSD experience). I'm currently learning XSD so I apologize if I'm using wrong XSD lingo.

I was able to create a XSD to blankly validate the XML, but now custom validation for specific elements has been requested. I'm currently stuck on how to validate each string in the second attribute of element property that requires each string to be checked against different validation rules.

This is my first time asking a question, but have enjoyed the shared knowledge in stackoverflow for years. Any additional feedback on proper question asking is appreciate and I sincerely appreciate the help figuring out this problem.

I'm using version 1.0, but willing to bump up to version 1.1 if needed

Here is part of the XML that I have to validate with custom validation

<properties>

  <property name="description" value="long reference string"></property>

  <property name="passfailcriteria" value="long BDD string"></property>

  <property name="automationpath" value="url"></property>

  <property name="passfailcriteria" value="stuff, stuff, stuff"></property>

  <property name="teststatus" value="ready"></property>

  <property name="autocandidacy" value="automated"></property>

</properties>

Here is my current attempt at XSD validation

<xs:element name="properties">

        <xs:complexType>

            <xs:sequence>

                <xs:element ref="property" minOccurs="0" maxOccurs="unbounded"/>

            </xs:sequence>

        </xs:complexType>

</xs:element>

 

<xs:element name="property">

        <xs:complexType>

            <xs:attribute name="name" type="property-names" use="required"/>

            <xs:attribute name="value" type="property-values" use="required"/>

        </xs:complexType>

</xs:element>

 

<xs:simpleType name="property-names">

        <xs:restriction base="xs:string">

            <xs:enumeration value="autocandidacy"/>

            <xs:enumeration value="automationpath"/>

            <xs:enumeration value="description"/>

            <xs:enumeration value="passfailcriteria"/>

            <xs:enumeration value="ptes_req"/>

            <xs:enumeration value="tags"/>

            <xs:enumeration value="teststatus"/>

            <xs:enumeration value="verificationmentod"/>

        </xs:restriction>

</xs:simpleType>

 

<xs:complexType name="property-values">

       <xs:sequence minOccurs="0" maxOccurs="unbounded">

            <xs:element name="autocandidacy" type="autocandidacy"/>

            <xs:element name="automationpath" type="xs:string"/>

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

            <xs:element name="passfailcriteria" type="xs:string"/>

            <xs:element name="ptes_req" type="xs:string"/>

            <xs:element name="tags" type="tags"/>

            <xs:element name="teststatus" type="teststatus"/>

            <xs:element name="verificationmentod" type="verificationmentod"/>

        </xs:sequence>

</xs:complexType>

 

<!-- Verification Mentod-->

<!-- List of values acceptable for the property Verification Method -->

<xs:simpleType name="verificationmentod">

    <xs:restriction base="xs:string">

        <xs:enumeration value="Demonstration"/>

        <xs:enumeration value="Exam of Children"/>

        <xs:enumeration value="Test"/>

        <xs:enumeration value="Analysis"/>

    </xs:restriction>

</xs:simpleType>

 

<!-- Auto Candidacy-->

<!-- List of values acceptable for the property Auto Candidacy -->

<xs:simpleType name="autocandidacy">

    <xs:restriction base="xs:string">

        <xs:enumeration value="AIP"/>

        <xs:enumeration value="automated"/>

        <xs:enumeration value="No"/>

        <xs:enumeration value="Ready"/>

        <xs:enumeration value="Yes"/>

    </xs:restriction>

</xs:simpleType>

 

<!-- Test Status Value -->

<!-- List of values acceptable for the property teststatus -->

<xs:simpleType name="teststatus">

    <xs:restriction base="xs:string">

        <xs:enumeration value="WIP"/>

        <xs:enumeration value="Ready for Review"/>

        <xs:enumeration value="In Review"/>

        <xs:enumeration value="Ready"/>

        <xs:enumeration value="Repair"/>

    </xs:restriction>

</xs:simpleType>

 

<!-- tags -->

<!-- Checks that the string is a comma seperated value -->

<xs:simpleType name="tags" >

        <xs:restriction base="xs:string">

        <xs:pattern value="[a-zA-Z][a-zA-Z0-9]*(\s*,\s*[a-zA-Z][a-zA-Z0-9]*){0,10}" />

        </xs:restriction>

</xs:simpleType>

I broke each string that needs a custom validation into it's own block, put all required values into a block property-values and tried to reference that in the attribute that needs the validation <xs:attribute name="value" type="property-values" use="required"/>.

I can't seem to figure out how to validate the strings in attribute name="name" with the related attribute name="value" that uses the custom validation I built for that string. example: Tags is input in the property name So the value input to the property value should be validated by the tags block I created to make sure the string meets the criteria.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source