[prev in list] [next in list] [prev in thread] [next in thread] 

List:       xerces-j-user
Subject:    Problem with lax validation
From:       Jamie Peabody <jpeabody () axway ! com>
Date:       2016-03-31 12:50:25
Message-ID: 7C9729F1987DD84F8F21DC529C32ED3901873255CB () WPTXMAIL1 ! ptx ! axway ! int
[Download RAW message or body]

I am having a problem validating this SOAP Envelope using this snippet of code \
(below).

The error that I get is:
org.xml.sax.SAXParseException; cvc-elt.4.2: Cannot resolve 'ipo:UKAddress' to a type \
definition for element 'shipTo'.

SOAP XSD defines the Body as:

<xs:complexType name="Body">
<xs:sequence>
<xs:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xs:sequence>

My expectation is that "lax" should validate if it has a definition, but ignore if it \
does not.  However, that is not the case with respect to the \
xsi:type="ipo:UKAddress".  I am only validating the SOAP Envelope - not the Body.

It looks like a bug in xerces-j.  In the same chunk of code, XMLSchemaValidator:2152 \
actually checks processContents before raising an error:

    else if (wildcard != null && wildcard.fProcessContents == \
XSWildcardDecl.PC_STRICT) {

Whereas, XMLSchemaValidator:2178 makes no such check and will throw no matter what.

    fCurrentType = getAndCheckXsiType(element, xsiType, attributes);

Any help, or confirmation that this is indeed a bug is appreciated.

Thanks,
Jamie


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.CodeSource;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.xerces.impl.Constants;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Validate {
    private static final String envelope =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
            "<soapenv:Envelope \n" +
            "  xmlns=\"http://www.w3.org/2001/XMLSchema\"" +
            "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
            "  xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
            "  >\n" +
            "  <soapenv:Body>\n" +
            "    <ipo:purchaseOrder xmlns:ipo=\"http://www.example.com/IPO\">\n" +
            "      <shipTo exportCode=\"1\" xsi:type=\"ipo:UKAddress\">\n" +
            "        <name>Helen Zoe</name>\n" +
            "        <street>47 Eden Street</street>\n" +
            "        <city>Cambridge</city>\n" +
            "        <postcode>CB1 1JR</postcode>\n" +
            "      </shipTo>\n" +
            "    </ipo:purchaseOrder>\n" +
            "  </soapenv:Body>\n" +
            "</soapenv:Envelope>";

    protected static final String HONOUR_ALL_SCHEMALOCATIONS =
            Constants.XERCES_FEATURE_PREFIX + \
Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE;  protected static final String \
IGNORE_XSI_TYPE_FEATURE =  Constants.XERCES_FEATURE_PREFIX + \
Constants.IGNORE_XSI_TYPE_FEATURE;  private static final String SOAP_1_1_ENVELOPE =
    "http://schemas.xmlsoap.org/soap/envelope";
    protected static final String JAXP_SCHEMA_LANGUAGE =
    "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    protected static final String W3C_XML_SCHEMA =
    "http://www.w3.org/2001/XMLSchema";

    public static void validate() throws ParserConfigurationException, SAXException, \
                IOException, TransformerException {
        final DocumentBuilderFactory documentBuilderFactory = \
DocumentBuilderFactory.newInstance();

        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setValidating(false);

        final Class<?> clazz = documentBuilderFactory.getClass();
        final CodeSource source = clazz.getProtectionDomain().getCodeSource();
        System.out.println("Document builder implementation: " + clazz.getName() + " \
from : " + (source == null ? "JRE" : source));

        final DocumentBuilder documentBuilder = \
                documentBuilderFactory.newDocumentBuilder();
        final InputStream is = new \
ByteArrayInputStream(envelope.getBytes(StandardCharsets.UTF_8));  final Document \
document = documentBuilder.parse(is);  final DOMSource domSource = new \
DOMSource(document);

        final StreamSource streamSource = new StreamSource(new \
                URL(SOAP_1_1_ENVELOPE).openStream());
        final SchemaFactory schemaFactory = \
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  final Schema schema = \
schemaFactory.newSchema(streamSource);

        final Validator validator = schema.newValidator();
        validator.setFeature("http://apache.org/xml/features/validation/schema/ignore-xsi-type-until-elemdecl", \
                true);
        validator.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", \
                true);
        validator.setFeature("http://apache.org/xml/features/validation/schema-full-checking", \
false);  validator.validate(domSource);
    }
}


[Attachment #3 (text/html)]

<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" id="owaParaStyle"></style>
</head>
<body fpstyle="1" ocsi="0">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">
<div>
<div>I am having a problem validating this SOAP Envelope using this snippet of code \
(below).</div> <div><br>
</div>
<div><span style="font-size: 10pt;">The error that I get is:</span></div>
<div>org.xml.sax.SAXParseException; cvc-elt.4.2: Cannot resolve 'ipo:UKAddress' to a \
type definition for element 'shipTo'.</div> <div><br>
</div>
<div>
<div style="font-size: 13.3333px;">SOAP XSD defines the Body as:</div>
<div style="font-size: 13.3333px;"><br>
</div>
<div>
<div style="font-size: 16px; font-family: 'Times New Roman'; margin: 0px;"><font \
face="Times New Roman,serif" size="3"><span style="font-size: 12pt;"><font \
face="Courier New" size="2"><span style="font-size: \
10pt;">&lt;xs:complexType&nbsp;name=&quot;Body&quot;&gt;</span></font></span></font></div>
 <div style="font-size: 16px; font-family: 'Times New Roman'; margin: 0px;"><font \
face="Times New Roman,serif" size="3"><span style="font-size: 12pt;"><font \
face="Courier New" size="2"><span style="font-size: \
10pt;">&lt;xs:sequence&gt;</span></font></span></font></div> <div style="font-size: \
16px; font-family: 'Times New Roman'; margin: 0px;"><font face="Times New \
Roman,serif" size="3"><span style="font-size: 12pt;"><font face="Courier New" \
size="2"><span style="font-size: \
10pt;">&lt;xs:any&nbsp;namespace=&quot;##any&quot;&nbsp;minOccurs=&quot;0&quot;&nbsp;m \
axOccurs=&quot;unbounded&quot;&nbsp;processContents=&quot;lax&quot;/&gt;</span></font></span></font></div>
 <div style="font-size: 16px; font-family: 'Times New Roman'; margin: 0px;"><font \
face="Times New Roman,serif" size="3"><span style="font-size: 12pt;"><font \
face="Courier New" size="2"><span style="font-size: \
10pt;">&lt;/xs:sequence&gt;</span></font></span></font></div> <div style="font-size: \
13.3333px;"><font face="Times New Roman,serif" size="3"><span style="font-size: \
12pt;"><font face="Courier New" size="2"><span style="font-size: 10pt;"><br> \
</span></font></span></font></div> <div>My expectation is that &quot;lax&quot; should \
validate if it has a definition, but ignore if it does not. &nbsp;However, that is \
not the case with respect to the xsi:type=&quot;ipo:UKAddress&quot;. &nbsp;I am only \
validating the SOAP Envelope - not the Body.</div> <div style="font-size: \
13.3333px;"><br> </div>
</div>
</div>
<div style="font-size: 13.3333px;">It looks like a bug in xerces-j. &nbsp;<span \
style="font-size: 13.3333px;">In the same chunk of code,&nbsp;</span><span \
style="font-size: 13.3333px;">XMLSchemaValidator:2152 actually checks processContents \
before raising an error:</span></div> <div><br>
</div>
<div><span style="font-size: 13.3333px;">&nbsp; &nbsp; else if (wildcard != null \
&amp;&amp; wildcard.fProcessContents == XSWildcardDecl.PC_STRICT) {</span></div> <div \
style="font-size: 13.3333px;"><br> </div>
<div>Whereas,&nbsp;<span style="font-size: 13.3333px;">XMLSchemaValidator:2178 makes \
no such check and will throw no matter what.</span></div> <div><br>
</div>
<div>&nbsp; &nbsp;&nbsp;fCurrentType = getAndCheckXsiType(element, xsiType, \
attributes);</div> <div><br>
</div>
<div><span style="font-size: 10pt;">Any help, or confirmation that this is indeed a \
bug is appreciated.</span></div> <div><br>
</div>
<div>Thanks,<br>
Jamie</div>
<div><br>
</div>
<div><br>
</div>
<div>import java.io.ByteArrayInputStream;</div>
<div>import java.io.IOException;</div>
<div>import java.io.InputStream;</div>
<div>import java.net.URL;</div>
<div>import java.nio.charset.StandardCharsets;</div>
<div>import java.security.CodeSource;</div>
<div><br>
</div>
<div>import javax.xml.XMLConstants;</div>
<div>import javax.xml.parsers.DocumentBuilder;</div>
<div>import javax.xml.parsers.DocumentBuilderFactory;</div>
<div>import javax.xml.parsers.ParserConfigurationException;</div>
<div>import javax.xml.transform.TransformerException;</div>
<div>import javax.xml.transform.dom.DOMSource;</div>
<div>import javax.xml.transform.stream.StreamSource;</div>
<div>import javax.xml.validation.Schema;</div>
<div>import javax.xml.validation.SchemaFactory;</div>
<div>import javax.xml.validation.Validator;</div>
<div><br>
</div>
<div>import org.apache.xerces.impl.Constants;</div>
<div>import org.w3c.dom.Document;</div>
<div>import org.xml.sax.SAXException;</div>
<div><br>
</div>
<div>public class Validate {</div>
<div>&nbsp; &nbsp; private static final String envelope =</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&lt;?xml \
version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;\n&quot; &#43;</div> \
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&lt;soapenv:Envelope \n&quot; \
&#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; \
&nbsp;xmlns=\&quot;http://www.w3.org/2001/XMLSchema\&quot;&quot; &#43;</div> \
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; \
&nbsp;xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot;\n&quot; \
&#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; \
&nbsp;xmlns:soapenv=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot;\n&quot; \
&#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; &nbsp;&gt;\n&quot; \
&#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; \
&nbsp;&lt;soapenv:Body&gt;\n&quot; &#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &quot; &nbsp; &nbsp;&lt;ipo:purchaseOrder \
xmlns:ipo=\&quot;http://www.example.com/IPO\&quot;&gt;\n&quot; &#43;</div> \
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; &nbsp; &nbsp; &nbsp;&lt;shipTo \
exportCode=\&quot;1\&quot; xsi:type=\&quot;ipo:UKAddress\&quot;&gt;\n&quot; \
&#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; &nbsp; &nbsp; \
&nbsp; &nbsp;&lt;name&gt;Helen Zoe&lt;/name&gt;\n&quot; &#43;</div> <div>&nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; &nbsp; &nbsp; &nbsp; &nbsp;&lt;street&gt;47 \
Eden Street&lt;/street&gt;\n&quot; &#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &quot; &nbsp; &nbsp; &nbsp; \
&nbsp;&lt;city&gt;Cambridge&lt;/city&gt;\n&quot; &#43;</div> <div>&nbsp; &nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &quot; &nbsp; &nbsp; &nbsp; &nbsp;&lt;postcode&gt;CB1 \
1JR&lt;/postcode&gt;\n&quot; &#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &quot; &nbsp; &nbsp; &nbsp;&lt;/shipTo&gt;\n&quot; &#43;</div> <div>&nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot; &nbsp; \
&nbsp;&lt;/ipo:purchaseOrder&gt;\n&quot; &#43;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; \
&nbsp; &nbsp; &quot; &nbsp;&lt;/soapenv:Body&gt;\n&quot; &#43;</div> <div>&nbsp; \
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;&lt;/soapenv:Envelope&gt;&quot;;</div> \
<div><br> </div>
<div>&nbsp; &nbsp; protected static final String HONOUR_ALL_SCHEMALOCATIONS \
=&nbsp;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \
Constants.XERCES_FEATURE_PREFIX &#43; \
Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE;</div> <div>&nbsp; &nbsp; protected \
static final String IGNORE_XSI_TYPE_FEATURE =&nbsp;</div> <div>&nbsp; &nbsp; <span \
class="Apple-tab-span" style="white-space:pre"></span>Constants.XERCES_FEATURE_PREFIX \
&#43; Constants.IGNORE_XSI_TYPE_FEATURE;</div> <div>&nbsp; &nbsp; private static \
final String SOAP_1_1_ENVELOPE =</div> <div>&nbsp; &nbsp; <span \
class="Apple-tab-span" \
style="white-space:pre"></span>&quot;http://schemas.xmlsoap.org/soap/envelope&quot;;</div>
 <div>&nbsp; &nbsp; protected static final String JAXP_SCHEMA_LANGUAGE =</div>
<div>&nbsp; &nbsp; <span class="Apple-tab-span" \
style="white-space:pre"></span>&quot;http://java.sun.com/xml/jaxp/properties/schemaLanguage&quot;;</div>
 <div>&nbsp; &nbsp; protected static final String W3C_XML_SCHEMA =</div>
<div>&nbsp; &nbsp; <span class="Apple-tab-span" \
style="white-space:pre"></span>&quot;http://www.w3.org/2001/XMLSchema&quot;;</div> \
<div><br> </div>
<div>&nbsp; &nbsp; public static void validate() throws ParserConfigurationException, \
SAXException, IOException, TransformerException {</div> <div>&nbsp; &nbsp; &nbsp; \
&nbsp; final DocumentBuilderFactory documentBuilderFactory = \
DocumentBuilderFactory.newInstance();</div> <div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; \
documentBuilderFactory.setNamespaceAware(true);</div> <div>&nbsp; &nbsp; &nbsp; \
&nbsp; documentBuilderFactory.setValidating(false);</div> <div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; final Class&lt;?&gt; clazz = \
documentBuilderFactory.getClass();</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; final \
CodeSource source = clazz.getProtectionDomain().getCodeSource();</div> <div>&nbsp; \
&nbsp; &nbsp; &nbsp; System.out.println(&quot;Document builder implementation: &quot; \
&#43; clazz.getName() &#43; &quot; from : &quot; &#43; (source == null ? \
&quot;JRE&quot; : source));</div> <div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; final DocumentBuilder documentBuilder = \
documentBuilderFactory.newDocumentBuilder();</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; \
final InputStream is = new \
ByteArrayInputStream(envelope.getBytes(StandardCharsets.UTF_8));</div> <div>&nbsp; \
&nbsp; &nbsp; &nbsp; final Document document = documentBuilder.parse(is);</div> \
<div>&nbsp; &nbsp; &nbsp; &nbsp; final DOMSource domSource = new \
DOMSource(document);</div> <div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; final StreamSource streamSource = new \
StreamSource(new URL(SOAP_1_1_ENVELOPE).openStream());</div> <div>&nbsp; &nbsp; \
&nbsp; &nbsp; final SchemaFactory schemaFactory = \
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);</div> <div>&nbsp; \
&nbsp; &nbsp; &nbsp; final Schema schema = \
schemaFactory.newSchema(streamSource);</div> <div>&nbsp; &nbsp; &nbsp; \
&nbsp;&nbsp;</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; final Validator validator = \
schema.newValidator();</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; \
validator.setFeature(&quot;http://apache.org/xml/features/validation/schema/ignore-xsi-type-until-elemdecl&quot;, \
true);</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; \
validator.setFeature(&quot;http://apache.org/xml/features/honour-all-schemaLocations&quot;, \
true);</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; \
validator.setFeature(&quot;http://apache.org/xml/features/validation/schema-full-checking&quot;, \
false);</div> <div>&nbsp; &nbsp; &nbsp; &nbsp; validator.validate(domSource);</div>
<div>&nbsp; &nbsp; }</div>
<div>}</div>
</div>
<div><br>
</div>
</div>
</body>
</html>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic