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'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 'maxOccurs' cannot appear i=
n element 'element'.<br>[Error] query1.xsd:3:67: s4s-att-not-allowe=
d: Attribute 'minOccurs' cannot appear in element 'element'=
.<br><br>i.e, the following is not allowed by XSD language,<br><br><xs:s=
chema attributeFormDefault=3D"unqualified" elementFormDefault=3D&=
quot;qualified" xmlns:xs=3D"<a href=3D"http://www.w3.org/2001/XML=
Schema">http://www.w3.org/2001/XMLSchema</a>"><br><br>=C2=A0 =C2=A0=
<xs:element name=3D"functionalView" maxOccurs=3D"1"=
minOccurs=3D"1"><br>=C2=A0 =C2=A0 ...<br><br></xs:schema&g=
t;<br><br>Removing, maxOccurs=3D"1" minOccurs=3D"1" 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've=
mentioned can be solved using an XSD 1.1 <assert> instruction. I gue=
ss that, you can rewrite your XSD fragment to following (I've only adde=
d an <assert>), to achieve what you've mentioned,<br><br><xs:e=
lement name=3D"where" maxOccurs=3D"1" minOccurs=3D"=
;0"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0<xs:complexType><br>=C2=A0=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<xs:sequence><br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<xs:element n=
ame=3D"condition" maxOccurs=3D"unbounded" minOccurs=3D&=
quot;1"><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<xs:complexType><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<xs:simpleContent><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<xs:extension base=3D"xs:stri=
ng"><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 <xs:attribute type=3D"xs:string" name=3D&=
quot;alias1" use=3D"required"/><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 <xs:attribut=
e type=3D"xs:string" name=3D"col1" use=3D"required=
"/><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 <xs:attribute type=3D"xs:string" name=3D&=
quot;operator" use=3D"required"/><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 <xs:attribut=
e type=3D"xs:string" name=3D"string" use=3D"option=
al"/><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 <xs:attribute type=3D"xs:string" name=
=3D"number" use=3D"optional"/><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 <xs:attri=
bute type=3D"xs:string" name=3D"date" use=3D"optio=
nal"/><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 <xs:assert test=3D"exists(@string | @numbe=
r | @date)"/><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</xs:extension><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 </xs:simpleC=
ontent><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</xs:complexType><br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0</xs:element&=
gt;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 </xs:sequ=
ence><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0</xs:complexType><div>&=
lt;/xs:element></div><div><br></div><div>I've not tested, the logic =
of <assert> I'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 'test' attribute of an <assert>. Also, 1=
upto any number of <assert> elements can be written as siblings in t=
he XSD document (all of the sibling <assert> 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 <<a href=3D"mailto:rajneeshshukla@gmai=
l.com">[email protected]</a>> 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 <xs:element name=3D"where" maxOccu=
rs=3D"1" minOccurs=3D"0"><br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 <xs:complexType><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 <xs:sequence><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 <xs:element name=3D"condition" maxOccurs=3D"unbounded=
" minOccurs=3D"1"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 <xs:complexType><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <xs:simpleContent><br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <xs:extensio=
n base=3D"xs:string"><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <xs:attribute type=3D"xs:=
string" name=3D"alias1" use=3D"required"/><br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 <xs:attribute type=3D"xs:string" name=3D"col1" u=
se=3D"required"/><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <xs:attribute type=3D"xs:string=
" name=3D"operator" use=3D"required"/><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"xs:string" name=3D"string" use=
=3D"optional"/><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <xs:attribute type=3D"xs:string&=
quot; name=3D"number" use=3D"optional"/><br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 <x=
s:attribute type=3D"xs:string" name=3D"date" use=3D&quo=
t;optional"/><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 </xs:extension><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 </xs:simpleContent><br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 </xs:complexType><br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 </xs:element><br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 </xs:sequence><br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 </xs:complexType><br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 </xs:element><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--