4 Replies Latest reply on Feb 24, 2020 8:07 AM by Rajneesh S-Oracle

    XSD with "required" attribute option related query

    Rajneesh S-Oracle

      Hello All,

       

      I am new to XSD and XML and need to explore if there is option to make sure that any one attribute in a set of attributes within same element is required.

       

      Example:

       

        <xs:element name="where" maxOccurs="1" minOccurs="0">

                <xs:complexType>

                  <xs:sequence>

                    <xs:element name="condition" maxOccurs="unbounded" minOccurs="1">

                      <xs:complexType>

                        <xs:simpleContent>

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

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

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

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

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

                            <xs:attribute type="xs:string" name="number" use="optional"/>

                            <xs:attribute type="xs:string" name="date" use="optional"/>

                          </xs:extension>

                        </xs:simpleContent>

                      </xs:complexType>

                    </xs:element>

                  </xs:sequence>

                </xs:complexType>

              </xs:element>

       

      Here I want to ensure that minimum one  attribute  in a set of 3 attributes (mentioned as optional in above) are required. All can not be optional , however any one (can be more than one also) is required.

       

      Thanks,

      Rajneesh

        • 1. Re: XSD with "required" attribute option related query
          odie_63

          I don't think it's possible using XSD 1.0, which is the version you're probably using given your recent posts.

           

          If you can, I suggest redesigning this part.

           

          For example :

           

          <where>
            <condition alias1="T1" col1="COL_1" operator="=" value="10.5" valueType="number"/>
            <condition alias1="T1" col1="COL_2" operator="&lt;" value="2020-02-22" valueType="date"/>
            ...
          </where>
          

           

          Schema fragment :

           

          <xs:simpleType name="valueTypeEnum">
            <xs:restriction base="xs:string">
              <xs:enumeration value="number" />
              <xs:enumeration value="date" />
              <xs:enumeration value="string" />
            </xs:restriction>
          </xs:simpleType>
          
          <xs:element name="where" minOccurs="0">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="condition" maxOccurs="unbounded">
                  <xs:complexType>
                    <xs:attribute type="xs:string" name="alias1" use="required"/>
                    <xs:attribute type="xs:string" name="col1" use="required"/>
                    <xs:attribute type="xs:string" name="operator" use="required"/>
                    <xs:attribute type="xs:string" name="value" use="required"/>
                    <xs:attribute type="valueTypeEnum" name="valueType" use="required"/>
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          

           

           

           

          or, something a little more complicated using an extended abstract complexType, which allows type validation :

           

          <where>
            <condition xsi:type="numberConditionType" alias1="T1" col1="COL_1" operator="=" value="10.5" />
            <condition xsi:type="dateConditionType" alias1="T1" col1="COL_2" operator="&lt;" value="2020-02-22" />
            <condition xsi:type="stringConditionType" alias1="T1" col1="COL_3" operator="=" value="ABC" />
          </where>
          

           

          corresponding schema fragment :

           

          <xs:complexType name="conditionType" abstract="true">
            <xs:attribute type="xs:string" name="alias1" use="required"/>
            <xs:attribute type="xs:string" name="col1" use="required"/>
            <xs:attribute type="xs:string" name="operator" use="required"/>
          </xs:complexType>
          
          <xs:complexType name="numberConditionType">
            <xs:complexContent>
              <xs:extension base="conditionType">
                <xs:attribute name="value" type="xs:decimal" use="required"/>
              </xs:extension>
            </xs:complexContent>
          </xs:complexType>
          
          <xs:complexType name="dateConditionType">
            <xs:complexContent>
              <xs:extension base="conditionType">
                <xs:attribute name="value" type="xs:date" use="required"/>
              </xs:extension>
            </xs:complexContent>
          </xs:complexType>
          
          <xs:complexType name="stringConditionType">
            <xs:complexContent>
              <xs:extension base="conditionType">
                <xs:attribute name="value" type="xs:string" use="required"/>
              </xs:extension>
            </xs:complexContent>
          </xs:complexType>
          
          <xs:element name="where">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="condition" type="conditionType" maxOccurs="unbounded"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          
          1 person found this helpful
          • 2. Re: XSD with "required" attribute option related query
            Rajneesh S-Oracle

            Thank you, I guess below is also option with XSD 1.1, then I need to get rid of XML 1.0 and use 1.1 version:

             

             

            <xs:element name="where" maxOccurs="1" minOccurs="0">

                      <xs:complexType>

                        <xs:sequence>

                          <xs:element name="condition" maxOccurs="unbounded" minOccurs="1">

                            <xs:complexType>

                              <xs:simpleContent>

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

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

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

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

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

                                  <xs:attribute type="xs:string" name="number" use="optional"/>

                                  <xs:attribute type="xs:string" name="date" use="optional"/>

                                  <xs:assert test="exists(@string | @number | @date)"/>

                                </xs:extension>

                              </xs:simpleContent>

                            </xs:complexType>

                          </xs:element>

                        </xs:sequence>

                      </xs:complexType>

                    </xs:element>

            • 3. Re: XSD with "required" attribute option related query
              odie_63

              Rajneesh Shukla-Oracle wrote:

               

              Thank you, I guess below is also option with XSD 1.1, then I need to get rid of XML 1.0 and use 1.1 version:

              If you're working inside the database, it won't be possible.

              Oracle XML DB only supports XML Schema 1.0.

               

              You'll need a third-party validator, for example Xerces2, possibly published in the db using a java stored procedure.

              1 person found this helpful
              • 4. Re: XSD with "required" attribute option related query
                Rajneesh S-Oracle

                Hi odie_63,

                Yes you are correct.

                Encounterd below error , when I tried to register xsd with assert as below:

                ORA-30937: No schema definition for 'assert' (namespace 'http://www.w3.org/2001/XMLSchema') in parent '/schema/element[1]/complexType/sequence/element[4]/complexType/sequence/element[1]/complexType/simpleContent/extension'

                ORA-06512: at "XDB.DBMS_XMLSCHEMA_INT", line 72

                ORA-06512: at "XDB.DBMS_XMLSCHEMA", line 33

                 

                 

                 

                <xs:element name="where" maxOccurs="1" minOccurs="0">

                          <xs:complexType>

                            <xs:sequence>

                              <xs:element name="condition" maxOccurs="unbounded" minOccurs="1">

                                <xs:complexType>

                                  <xs:simpleContent>

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

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

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

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

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

                                      <xs:attribute type="xs:string" name="number" use="optional"/>

                                      <xs:attribute type="xs:string" name="date" use="optional"/>

                                      <xs:assert test="exists(@string | @number | @date)"/>

                                    </xs:extension>

                                  </xs:simpleContent>

                                </xs:complexType>

                              </xs:element>

                            </xs:sequence>

                          </xs:complexType>

                        </xs:element>