Re: XML Schema Validation Error Report

Mukul Gandhi <[email protected]> Fri, 19 Jun 2020 12:16:49 +0530
Newsgroups gmane.text.xml.xerces-j.user
Message-ID <CABuuzNNC1oMz3Zr9A5xnPjdTSLCcgvho-zHJY4hwjiJ0MfP-og@mail.gmail.com>
--000000000000f6121a05a86a42d8
Content-Type: text/plain; charset="UTF-8"

Hi Herong,

On Thu, Jun 18, 2020 at 12:29 PM Herong Yang <[email protected]> wrote:

By default, Xerces2 only reports only the first invalid sub-element and
> skips other subsequent invalid siblings. Is the any way to set Xerces2 to
> report all invalid sub-elements?
>

I don't think that, what you're asking for is possible with Xerces-J.

Personally, I do find the error report you're getting with your XML and XSD
document samples, from Xerces-J to be useful.


> Here is the sample XML:
> <?xml version="1.0"?>
> <root>
> <X/>
> <A/>
> <Y/>
> <B/>
> </root>
>
> Here is the XML Schema:
> <?xml version="1.0"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
> <xs:element name="root">
> <xs:complexType>
> <xs:all>
> <xs:element name="X" minOccurs="0"/>
> <xs:element name="Y" minOccurs="0"/>
> </xs:all>
> </xs:complexType>
> </xs:element>
> </xs:schema>
>
> Here is the Xerces2 validation output:
> [Error] simple_4.xml:4:6: cvc-complex-type.2.4.a: Invalid content was
> found starting with element 'A'. One of '{Y}' is expected.
>
> I want to see both element 'A' and element 'B' being reported in the
> output.
>

For your requirements stated above, I could probably suggest the following
XML Schema 1.1 document (which makes the intent of XML Schema logic more
clear),

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="root">
      <xs:complexType>
<xs:sequence>
   <xs:any processContents="skip" minOccurs="0" maxOccurs="2"/>
</xs:sequence>
<xs:assert test="if (count(*) = 0) then true() else
                            if (count(*) = 1) then (*[1]/name() = ('X',
'Y')) else
                            if (count(*) = 2) then ((*[1]/name() = 'X' and
*[2]/name() = 'Y')
                                                              or
                                                            (*[1]/name() =
'Y' and *[2]/name() = 'X')) else
                            false()"/>
      </xs:complexType>
   </xs:element>

</xs:schema>



-- 
Regards,
Mukul Gandhi

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

<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div>Hi=C2=A0Herong,</di=
v><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On T=
hu, Jun 18, 2020 at 12:29 PM Herong Yang &lt;<a href=3D"mailto:herong_yang@=
yahoo.com">[email protected]</a>&gt; wrote:</div><div dir=3D"ltr" class=
=3D"gmail_attr"><br></div><blockquote class=3D"gmail_quote" style=3D"margin=
:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"=
><div style=3D"font-family:&quot;Helvetica Neue&quot;,Helvetica,Arial,sans-=
serif;font-size:13px"><div dir=3D"ltr">By default, Xerces2 only reports onl=
y the first invalid sub-element and skips other subsequent invalid siblings=
. Is the any way to set Xerces2 to report all invalid sub-elements?</div></=
div></blockquote><div><br></div><div>I don&#39;t think that, what you&#39;r=
e asking for is possible with Xerces-J.</div><div><br></div><div>Personally=
, I do find the error report you&#39;re getting with your XML and XSD docum=
ent samples, from Xerces-J to be useful.</div><div>=C2=A0</div><blockquote =
class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px sol=
id rgb(204,204,204);padding-left:1ex"><div style=3D"font-family:&quot;Helve=
tica Neue&quot;,Helvetica,Arial,sans-serif;font-size:13px"><div dir=3D"ltr"=
>Here is the sample XML:=C2=A0</div><div dir=3D"ltr"><div><div>&lt;?xml ver=
sion=3D&quot;1.0&quot;?&gt;</div><div>&lt;root&gt;</div><div><span style=3D=
"white-space:pre-wrap">	</span>&lt;X/&gt;</div><div><span style=3D"white-sp=
ace:pre-wrap">	</span>&lt;A/&gt;</div><div><span style=3D"white-space:pre-w=
rap">	</span>&lt;Y/&gt;</div><div><span style=3D"white-space:pre-wrap">	</s=
pan>&lt;B/&gt;</div><div>&lt;/root&gt;</div><div dir=3D"ltr"><br></div></di=
v><div dir=3D"ltr">Here is the XML Schema:=C2=A0</div><div dir=3D"ltr"><div=
><div>&lt;?xml version=3D&quot;1.0&quot;?&gt;</div><div>&lt;xs:schema xmlns=
:xs=3D&quot;<a href=3D"http://www.w3.org/2001/XMLSchema" target=3D"_blank">=
http://www.w3.org/2001/XMLSchema</a>&quot;&gt;=C2=A0</div><div><span style=
=3D"white-space:pre-wrap">	</span>&lt;xs:element name=3D&quot;root&quot;&gt=
;</div><div><span style=3D"white-space:pre-wrap">		</span>&lt;xs:complexTyp=
e&gt;</div><div><span style=3D"white-space:pre-wrap">			</span>&lt;xs:all&g=
t;</div><div><span style=3D"white-space:pre-wrap">				</span>&lt;xs:element=
 name=3D&quot;X&quot; minOccurs=3D&quot;0&quot;/&gt;</div><div><span style=
=3D"white-space:pre-wrap">				</span>&lt;xs:element name=3D&quot;Y&quot; mi=
nOccurs=3D&quot;0&quot;/&gt;</div><div><span style=3D"white-space:pre-wrap"=
>			</span>&lt;/xs:all&gt;</div><div><span style=3D"white-space:pre-wrap">	=
	</span>&lt;/xs:complexType&gt;</div><div><span style=3D"white-space:pre-wr=
ap">	</span>&lt;/xs:element&gt;</div><div dir=3D"ltr">&lt;/xs:schema&gt;</d=
iv></div></div></div><div dir=3D"ltr"><br></div><div dir=3D"ltr">Here is th=
e Xerces2 validation output:=C2=A0</div><div dir=3D"ltr"><span><div>[Error]=
 simple_4.xml:4:6: cvc-complex-type.2.4.a: Invalid content was found starti=
ng with element &#39;A&#39;. One of &#39;{Y}&#39; is expected.</div><div><b=
r></div><div dir=3D"ltr">I want to see both element &#39;A&#39; and element=
 &#39;B&#39; being reported in the output.</div></span></div></div></blockq=
uote><div><br></div><div>For your requirements stated above, I could probab=
ly suggest the following XML Schema 1.1 document (which makes the intent of=
 XML Schema logic more clear),</div><div><br></div><div><div>&lt;?xml versi=
on=3D&quot;1.0&quot;?&gt;</div><div>&lt;xs:schema xmlns:xs=3D&quot;<a href=
=3D"http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema</a>&=
quot;&gt;</div><div><br></div><div>=C2=A0 =C2=A0&lt;xs:element name=3D&quot=
;root&quot;&gt;</div><div>=C2=A0 =C2=A0 =C2=A0 &lt;xs:complexType&gt;</div>=
<div><span style=3D"white-space:pre">	</span> &lt;xs:sequence&gt;</div><div=
><span style=3D"white-space:pre">	</span>=C2=A0 =C2=A0&lt;xs:any processCon=
tents=3D&quot;skip&quot; minOccurs=3D&quot;0&quot; maxOccurs=3D&quot;2&quot=
;/&gt;</div><div><span style=3D"white-space:pre">	</span> &lt;/xs:sequence&=
gt;</div><div><span style=3D"white-space:pre">	</span> &lt;xs:assert test=
=3D&quot;if (count(*) =3D 0) then true() else</div><div><span style=3D"whit=
e-space:pre">	</span>=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 if (count(*) =3D 1) then (*[1=
]/name() =3D (&#39;X&#39;, &#39;Y&#39;)) else</div><div><span style=3D"whit=
e-space:pre">	</span>=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 if (count(*) =3D 2) then ((*[=
1]/name() =3D &#39;X&#39; and *[2]/name() =3D &#39;Y&#39;)</div><div><span =
style=3D"white-space:pre">	</span>=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 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 or</div><div><span style=3D"white-space:pre">	<=
/span>=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 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (*[1]/na=
me() =3D &#39;Y&#39; and *[2]/name() =3D &#39;X&#39;)) else</div><div><span=
 style=3D"white-space:pre">	</span>=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 false()&quot;/&=
gt;</div><div>=C2=A0 =C2=A0 =C2=A0 &lt;/xs:complexType&gt;</div><div>=C2=A0=
 =C2=A0&lt;/xs:element&gt;</div><div>=C2=A0 =C2=A0</div><div>&lt;/xs:schema=
&gt;</div></div><div><br></div><div>=C2=A0</div></div><div><br></div>-- <br=
><div dir=3D"ltr" class=3D"gmail_signature"><div dir=3D"ltr"><div>Regards,<=
br>Mukul Gandhi<br></div></div></div></div></div></div>

--000000000000f6121a05a86a42d8--