[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;"><xs:complexType name="Body"></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;"><xs:sequence></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;"><xs:any namespace="##any" minOccurs="0" m \
axOccurs="unbounded" processContents="lax"/></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;"></xs:sequence></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 "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.</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. <span \
style="font-size: 13.3333px;">In the same chunk of code, </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;"> else if (wildcard != null \
&& wildcard.fProcessContents == XSWildcardDecl.PC_STRICT) {</span></div> <div \
style="font-size: 13.3333px;"><br> </div>
<div>Whereas, <span style="font-size: 13.3333px;">XMLSchemaValidator:2178 makes \
no such check and will throw no matter what.</span></div> <div><br>
</div>
<div> 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> private static final String envelope =</div>
<div> "<?xml \
version=\"1.0\" encoding=\"utf-8\"?>\n" +</div> \
<div> "<soapenv:Envelope \n" \
+</div> <div> " \
xmlns=\"http://www.w3.org/2001/XMLSchema\"" +</div> \
<div> " \
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" \
+</div> <div> " \
xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" \
+</div> <div> " >\n" \
+</div> <div> " \
<soapenv:Body>\n" +</div> <div> \
" <ipo:purchaseOrder \
xmlns:ipo=\"http://www.example.com/IPO\">\n" +</div> \
<div> " <shipTo \
exportCode=\"1\" xsi:type=\"ipo:UKAddress\">\n" \
+</div> <div> " \
<name>Helen Zoe</name>\n" +</div> <div> \
" <street>47 \
Eden Street</street>\n" +</div> <div> \
" \
<city>Cambridge</city>\n" +</div> <div> \
" <postcode>CB1 \
1JR</postcode>\n" +</div> <div> \
" </shipTo>\n" +</div> <div> \
" \
</ipo:purchaseOrder>\n" +</div> <div> \
" </soapenv:Body>\n" +</div> <div> \
"</soapenv:Envelope>";</div> \
<div><br> </div>
<div> protected static final String HONOUR_ALL_SCHEMALOCATIONS \
= </div> <div> \
Constants.XERCES_FEATURE_PREFIX + \
Constants.HONOUR_ALL_SCHEMALOCATIONS_FEATURE;</div> <div> protected \
static final String IGNORE_XSI_TYPE_FEATURE = </div> <div> <span \
class="Apple-tab-span" style="white-space:pre"></span>Constants.XERCES_FEATURE_PREFIX \
+ Constants.IGNORE_XSI_TYPE_FEATURE;</div> <div> private static \
final String SOAP_1_1_ENVELOPE =</div> <div> <span \
class="Apple-tab-span" \
style="white-space:pre"></span>"http://schemas.xmlsoap.org/soap/envelope";</div>
<div> protected static final String JAXP_SCHEMA_LANGUAGE =</div>
<div> <span class="Apple-tab-span" \
style="white-space:pre"></span>"http://java.sun.com/xml/jaxp/properties/schemaLanguage";</div>
<div> protected static final String W3C_XML_SCHEMA =</div>
<div> <span class="Apple-tab-span" \
style="white-space:pre"></span>"http://www.w3.org/2001/XMLSchema";</div> \
<div><br> </div>
<div> public static void validate() throws ParserConfigurationException, \
SAXException, IOException, TransformerException {</div> <div> \
final DocumentBuilderFactory documentBuilderFactory = \
DocumentBuilderFactory.newInstance();</div> <div><br>
</div>
<div> \
documentBuilderFactory.setNamespaceAware(true);</div> <div> \
documentBuilderFactory.setValidating(false);</div> <div><br>
</div>
<div> final Class<?> clazz = \
documentBuilderFactory.getClass();</div> <div> final \
CodeSource source = clazz.getProtectionDomain().getCodeSource();</div> <div> \
System.out.println("Document builder implementation: " \
+ clazz.getName() + " from : " + (source == null ? \
"JRE" : source));</div> <div><br>
</div>
<div> final DocumentBuilder documentBuilder = \
documentBuilderFactory.newDocumentBuilder();</div> <div> \
final InputStream is = new \
ByteArrayInputStream(envelope.getBytes(StandardCharsets.UTF_8));</div> <div> \
final Document document = documentBuilder.parse(is);</div> \
<div> final DOMSource domSource = new \
DOMSource(document);</div> <div><br>
</div>
<div> final StreamSource streamSource = new \
StreamSource(new URL(SOAP_1_1_ENVELOPE).openStream());</div> <div> \
final SchemaFactory schemaFactory = \
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);</div> <div> \
final Schema schema = \
schemaFactory.newSchema(streamSource);</div> <div> \
</div> <div> final Validator validator = \
schema.newValidator();</div> <div> \
validator.setFeature("http://apache.org/xml/features/validation/schema/ignore-xsi-type-until-elemdecl", \
true);</div> <div> \
validator.setFeature("http://apache.org/xml/features/honour-all-schemaLocations", \
true);</div> <div> \
validator.setFeature("http://apache.org/xml/features/validation/schema-full-checking", \
false);</div> <div> validator.validate(domSource);</div>
<div> }</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