Xerces does not validate complete xml

Michael Szalay <[email protected]> Wed, 10 May 2023 15:22:09 +0200
Newsgroups gmane.text.xml.xerces-j.user
Message-ID <[email protected]>
--------------EOQSVKZjqvFYC8IaFV76H5up
Content-Type: text/plain; charset="UTF-8"; format=flowed
Content-Transfer-Encoding: 7bit

Hi all

i have a xml document and a list of schemas against which it should 
validate. I use the xerces contained in the tamburin jdk-17.

My problem is, that xerces does not validate the complete document, but 
skips element after a certain depth.

Here (XMLSchemaValidator, lines 1919)

// if we are in the content of "skip", then just skip this element // 
REVISIT: is this the correct behaviour for ID constraints? -NG if (fSkipValidationDepth >= 0) {
     fElementDepth++;
     if (fAugPSVI)
         augs = getEmptyAugs(augs);
     return augs;
}

the elements are skipped...

is there a way to make xerces to really validate the complete document?

Here is my source:

final SchemaFactory schemaFactory =SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final String pathPrefix =FilenameUtils.getFullPath(schemaPath);
schemaFactory.setResourceResolver(new LocalLSResolver(pathPrefix));
schemaFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);

final InputStream schemaUrl = getClass().getResourceAsStream(schemaPath);
final Schema schema =schemaFactory.newSchema(new StreamSource(schemaUrl));

final Validator validator =schema.newValidator();
try {
   validator.validate(new StreamSource(new ByteArrayInputStream(xmlFile.getBytes(StandardCharsets.ISO_8859_1))));
}catch (IOException ioe) {
   //...
}

Regards Michael

--------------EOQSVKZjqvFYC8IaFV76H5up
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit

<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi all</p>
    <p>i have a xml document and a list of schemas against which it
      should validate. I use the xerces contained in the tamburin
      jdk-17.<br>
    </p>
    <p>My problem is, that xerces does not validate the complete
      document, but skips element after a certain depth.</p>
    <p>Here (XMLSchemaValidator, lines 1919)<br>
    </p>
    <div
      style="background-color:#ffffff;color:#080808;font-family:'JetBrains
      Mono',monospace;font-size:9.8pt;">
      <pre><span style="color:#8c8c8c;font-style:italic;">// if we are in the content of "skip", then just skip this element
</span><span style="color:#8c8c8c;font-style:italic;">// REVISIT:  is this the correct behaviour for ID constraints?  -NG
</span><span style="color:#0033b3;">if </span>(<span style="color:#871094;">fSkipValidationDepth </span>&gt;= <span style="color:#1750eb;">0</span>) {
    <span style="color:#871094;">fElementDepth</span>++;
    <span style="color:#0033b3;">if </span>(<span style="color:#871094;">fAugPSVI</span>)
        augs = getEmptyAugs(augs);
    <span style="color:#0033b3;">return </span>augs;
}

</pre>
      the elements are skipped...</div>
    <div
      style="background-color:#ffffff;color:#080808;font-family:'JetBrains
      Mono',monospace;font-size:9.8pt;"><br>
    </div>
    <div
      style="background-color:#ffffff;color:#080808;font-family:'JetBrains
      Mono',monospace;font-size:9.8pt;">is there a way to make xerces to
      really validate the complete document?</div>
    <div
      style="background-color:#ffffff;color:#080808;font-family:'JetBrains
      Mono',monospace;font-size:9.8pt;"><br>
    </div>
    <div
      style="background-color:#ffffff;color:#080808;font-family:'JetBrains
      Mono',monospace;font-size:9.8pt;">Here is my source:</div>
    <div
      style="background-color:#ffffff;color:#080808;font-family:'JetBrains
      Mono',monospace;font-size:9.8pt;"><br>
    </div>
    <div
      style="background-color:#ffffff;color:#080808;font-family:'JetBrains
      Mono',monospace;font-size:9.8pt;">
      <div
        style="background-color:#ffffff;color:#080808;font-family:'JetBrains
        Mono',monospace;font-size:9.8pt;">
        <pre><span style="color:#0033b3;">final </span><span style="color:#000000;">SchemaFactory schemaFactory </span>= <span style="color:#000000;">SchemaFactory</span>.<span style="font-style:italic;">newInstance</span>(<span style="color:#000000;">XMLConstants</span>.<span style="color:#871094;font-style:italic;">W3C_XML_SCHEMA_NS_URI</span>);
<span style="color:#0033b3;">final </span><span style="color:#000000;">String pathPrefix </span>= <span style="color:#000000;">FilenameUtils</span>.<span style="font-style:italic;">getFullPath</span>(schemaPath);
<span style="color:#000000;">schemaFactory</span>.setResourceResolver(<span style="color:#0033b3;">new </span>LocalLSResolver(<span style="color:#000000;">pathPrefix</span>));
<span style="color:#000000;">schemaFactory</span>.setFeature(<span style="color:#067d17;"><a class="moz-txt-link-rfc2396E" href="http://apache.org/xml/features/validation/schema-full-checking">"http://apache.org/xml/features/validation/schema-full-checking"</a></span>, <span style="color:#0033b3;">true</span>);

<span style="color:#0033b3;">final </span><span style="color:#000000;">InputStream schemaUrl </span>= getClass().getResourceAsStream(schemaPath);
<span style="color:#0033b3;">final </span><span style="color:#000000;">Schema schema </span>= <span style="color:#000000;">schemaFactory</span>.newSchema(<span style="color:#0033b3;">new </span>StreamSource(<span style="color:#000000;">schemaUrl</span>));

<span style="color:#0033b3;">final </span><span style="color:#000000;">Validator validator </span>= <span style="color:#000000;">schema</span>.newValidator();
<span style="color:#0033b3;">try </span>{
  <span style="color:#000000;">validator</span>.validate(<span style="color:#0033b3;">new </span>StreamSource(<span style="color:#0033b3;">new </span>ByteArrayInputStream(xmlFile.getBytes(<span style="color:#000000;">StandardCharsets</span>.<span style="color:#871094;font-style:italic;">ISO_8859_1</span>))));
} <span style="color:#0033b3;">catch </span>(<span style="color:#000000;">IOException </span>ioe) {
  //...
}

Regards Michael
</pre>
      </div>
    </div>
    <p></p>
  </body>
</html>

--------------EOQSVKZjqvFYC8IaFV76H5up--