Re: XSD with "required" attribute option related query

Mukul Gandhi <[email protected]> Sat, 22 Feb 2020 11:27:39 +0530
Newsgroups gmane.text.xml.schema.devel
Message-ID <CABuuzNNCP7koKzfot3gwu61cviPEk=xFL4z9gcd4g2v8FxUvJg@mail.gmail.com>
--000000000000d01152059f23d152
Content-Type: text/plain; charset="UTF-8"

Hi Rajneesh,
    Within the XSD document you've attached, following is a XSD issue to
start with (using Xerces-J 2.12.1 as XSD validator),

[Error] query1.xsd:3:67: s4s-att-not-allowed: Attribute 'maxOccurs' cannot
appear in element 'element'.
[Error] query1.xsd:3:67: s4s-att-not-allowed: Attribute 'minOccurs' cannot
appear in element 'element'.

i.e, the following is not allowed by XSD language,

<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="functionalView" maxOccurs="1" minOccurs="1">
    ...

</xs:schema>

Removing, maxOccurs="1" minOccurs="1" from above shown xs:element
declaration from your attached XSD document, makes your XSD document
correct (with both 1.0 and 1.1 versions of XSD language).

Now coming to your question.
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>

I've not tested, the logic of <assert> I've mentioned above.

Could be useful information to share that, you may use the full XPath 2.0
language, to write value of 'test' attribute of an <assert>. Also, 1 upto
any number of <assert> elements can be written as siblings in the XSD
document (all of the sibling <assert> elements, have to evaluate to true
for having the XML instance document valid).

On Fri, Feb 21, 2020 at 11:16 PM Rajneesh Shukla <[email protected]>
wrote:

> 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.
>
> Thanking you in anticipation !!!
>
> Attachment:
> Complete XSD file.
>



-- 
Regards,
Mukul Gandhi

--000000000000d01152059f23d152
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr">Hi Rajneesh,=C2=A0<br></div>=C2=A0 =C2=A0=
 Within the XSD document you&#39;ve attached, following is a XSD issue to s=
tart with (using Xerces-J 2.12.1 as XSD validator),<br><br>[Error] query1.x=
sd:3:67: s4s-att-not-allowed: Attribute &#39;maxOccurs&#39; cannot appear i=
n element &#39;element&#39;.<br>[Error] query1.xsd:3:67: s4s-att-not-allowe=
d: Attribute &#39;minOccurs&#39; cannot appear in element &#39;element&#39;=
.<br><br>i.e, the following is not allowed by XSD language,<br><br>&lt;xs:s=
chema attributeFormDefault=3D&quot;unqualified&quot; elementFormDefault=3D&=
quot;qualified&quot; xmlns:xs=3D&quot;<a href=3D"http://www.w3.org/2001/XML=
Schema">http://www.w3.org/2001/XMLSchema</a>&quot;&gt;<br><br>=C2=A0 =C2=A0=
 &lt;xs:element name=3D&quot;functionalView&quot; maxOccurs=3D&quot;1&quot;=
 minOccurs=3D&quot;1&quot;&gt;<br>=C2=A0 =C2=A0 ...<br><br>&lt;/xs:schema&g=
t;<br><br>Removing, maxOccurs=3D&quot;1&quot; minOccurs=3D&quot;1&quot; fro=
m above shown xs:element declaration from your attached XSD document, makes=
 your XSD document correct (with both 1.0 and 1.1 versions of XSD language)=
.<br><br>Now coming to your question.<br>I think that, the issue you&#39;ve=
 mentioned can be solved using an XSD 1.1 &lt;assert&gt; instruction. I gue=
ss that, you can rewrite your XSD fragment to following (I&#39;ve only adde=
d an &lt;assert&gt;), to achieve what you&#39;ve mentioned,<br><br>&lt;xs:e=
lement name=3D&quot;where&quot; maxOccurs=3D&quot;1&quot; minOccurs=3D&quot=
;0&quot;&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;xs:complexType&gt;<br>=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;xs:sequence&gt;<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;xs:element n=
ame=3D&quot;condition&quot; maxOccurs=3D&quot;unbounded&quot; minOccurs=3D&=
quot;1&quot;&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;xs:complexType&gt;<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0&lt;xs:simpleContent&gt;<br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;xs:extension base=3D&quot;xs:stri=
ng&quot;&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 &lt;xs:attribute type=3D&quot;xs:string&quot; name=3D&=
quot;alias1&quot; use=3D&quot;required&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:attribut=
e type=3D&quot;xs:string&quot; name=3D&quot;col1&quot; use=3D&quot;required=
&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 &lt;xs:attribute type=3D&quot;xs:string&quot; name=3D&=
quot;operator&quot; use=3D&quot;required&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:attribut=
e type=3D&quot;xs:string&quot; name=3D&quot;string&quot; use=3D&quot;option=
al&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:attribute type=3D&quot;xs:string&quot; name=
=3D&quot;number&quot; use=3D&quot;optional&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:attri=
bute type=3D&quot;xs:string&quot; name=3D&quot;date&quot; use=3D&quot;optio=
nal&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:assert test=3D&quot;exists(@string | @numbe=
r | @date)&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0&lt;/xs:extension&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/xs:simpleC=
ontent&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;/xs:complexType&gt;<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;/xs:element&=
gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/xs:sequ=
ence&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;/xs:complexType&gt;<div>&=
lt;/xs:element&gt;</div><div><br></div><div>I&#39;ve not tested, the logic =
of &lt;assert&gt; I&#39;ve mentioned above.</div><div><br></div><div>Could =
be useful information to share that, you may use the full XPath 2.0 languag=
e, to write value of &#39;test&#39; attribute of an &lt;assert&gt;. Also, 1=
 upto any number of &lt;assert&gt; elements can be written as siblings in t=
he XSD document (all of the sibling &lt;assert&gt; elements, have to evalua=
te to true for having the XML instance document valid).</div><div><br><div =
class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Fri, Feb 21,=
 2020 at 11:16 PM Rajneesh Shukla &lt;<a href=3D"mailto:rajneeshshukla@gmai=
l.com">[email protected]</a>&gt; wrote:<br></div><blockquote class=
=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rg=
b(204,204,204);padding-left:1ex"><div dir=3D"ltr">Hello All,<br><br>I am ne=
w to XSD and XML and need to explore if there is option to make sure that a=
ny one attribute in a set of attributes within same element is required.<br=
><br>Example:<br><br>=C2=A0 &lt;xs:element name=3D&quot;where&quot; maxOccu=
rs=3D&quot;1&quot; minOccurs=3D&quot;0&quot;&gt;<br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 &lt;xs:complexType&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 &lt;xs:sequence&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 &lt;xs:element name=3D&quot;condition&quot; maxOccurs=3D&quot;unbounded=
&quot; minOccurs=3D&quot;1&quot;&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 &lt;xs:complexType&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:simpleContent&gt;<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:extensio=
n base=3D&quot;xs:string&quot;&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:attribute type=3D&quot;xs:=
string&quot; name=3D&quot;alias1&quot; use=3D&quot;required&quot;/&gt;<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 &lt;xs:attribute type=3D&quot;xs:string&quot; name=3D&quot;col1&quot; u=
se=3D&quot;required&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:attribute type=3D&quot;xs:string=
&quot; name=3D&quot;operator&quot; use=3D&quot;required&quot;/&gt;<br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &=
lt;xs:attribute type=3D&quot;xs:string&quot; name=3D&quot;string&quot; use=
=3D&quot;optional&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;xs:attribute type=3D&quot;xs:string&=
quot; name=3D&quot;number&quot; use=3D&quot;optional&quot;/&gt;<br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;x=
s:attribute type=3D&quot;xs:string&quot; name=3D&quot;date&quot; use=3D&quo=
t;optional&quot;/&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 &lt;/xs:extension&gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/xs:simpleContent&gt;<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/xs:complexType&gt;<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/xs:element&gt;<br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/xs:sequence&gt;<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 &lt;/xs:complexType&gt;<br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 &lt;/xs:element&gt;<br><br><div>Here I want to ensure that minimum o=
ne =C2=A0attribute =C2=A0in a set of 3 attributes (mentioned as optional in=
 above) are required. All can not be optional , however any one (can be mor=
e than one also) is required.</div><div><br></div><div>Thanking you in anti=
cipation !!!<br></div><div><br></div><div>Attachment:</div><div>Complete XS=
D file.</div></div></blockquote><div>=C2=A0</div></div><br clear=3D"all"><d=
iv><br></div>-- <br><div dir=3D"ltr" class=3D"gmail_signature"><div dir=3D"=
ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div>Regards,<br>Mukul Gandhi</div><=
/div></div></div></div></div></div>

--000000000000d01152059f23d152--