XSD related quetion to enforce "required" option

Questions about XML that are not covered by the other forums should go here.
rajneesh
Posts: 4
Joined: Fri Feb 21, 2020 1:51 pm

XSD related quetion to enforce "required" option

Post by rajneesh »

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
tavy
Posts: 365
Joined: Thu Jul 01, 2004 12:29 pm

Re: XSD related quetion to enforce "required" option

Post by tavy »

Hello Rajneesh,

I think that you can add this constraint only using XSD 1.1 "xs:assert" or "xs:alternative", or maybe using a Schematron schema and create an assert there.
You can read more about XSD 1.1 in the specification: https://www.w3.org/TR/xmlschema11-1/
Maybe you can address this question on the XML Schema list: xmlschema-dev@w3.org

Best Regards,
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
rajneesh
Posts: 4
Joined: Fri Feb 21, 2020 1:51 pm

Re: XSD related quetion to enforce "required" option

Post by rajneesh »

Thank you
rajneesh
Posts: 4
Joined: Fri Feb 21, 2020 1:51 pm

Re: XSD related quetion to enforce "required" option

Post by rajneesh »

Thanks Tavy,

I reached to w3.org and below is recommendation from their end:

I think that, the issue you've mentioned can be solved using an XSD 1.1 <assert> instruction. I guess that, you can rewrite your XSD fragment to following (I've only added an <assert>), to achieve what you've mentioned,

<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>

However I am working inside the database, it won't be possible.
Oracle XML DB only supports XML Schema 1.0. :-(

Thanks,
Rajneesh
Post Reply