Xerces, XML, XSD and validation
843834Apr 20 2006 — edited Apr 20 2006I'm having trouble with the 1.4.4 xerces trying to validate an xml doc against a XSD schema.
It always gives me the error shown below. I know it is finding the schema file because when it
doesn't I get a exception telling me so.
Here are:
Error message
XML
XSD
The code
==========================
The Error
==========================
The content of element type "shippingRequest" must match "(order_date,brand_name,shipRequestType,shipHow,exportDocsURL,so,po,customer_po?,shipInstructions?,shipTo,items,comment?)". Column: 19 Line: 43 PublicID: null
==========================
The XML
==========================
<?xml version="1.0"?>
<shippingRequest xmlns="http://foo.comp.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://foo.comp.com/downloads/it/schema riverbed_ship_request.xsd">
<order_date>2006-03-09</order_date>
<brand_name>FOO</brand_name>
<shipRequestType>Evaluation</shipRequestType>
<shipHow shippingCarrier="ROUTED" shippingServiceLevel="NEXT DAY AM" modeOfTransport="AIR" freightTerms="WE PAY"/>
<exportDocsURL>https://foo.comp.com</exportDocsURL>
<so>99165</so>
<po>65512</po>
<customer_po>77836 NRR</customer_po>
<shipTo>
<company>GENERAL AIRCRAFT ENGINES - OH</company>
<attn>DAVID SMITHY</attn>
<address>ONE MAN WAY</address>
<city>CINCINNATI</city>
<state>OH</state>
<postalCode>2342315</postalCode>
<country>US</country>
</shipTo>
<items>
<item>
<item_reason>NEW</item_reason>
<type>STANDARD</type>
<creation_date>2006-03-09</creation_date>
<update_date>2006-03-09</update_date>
<schedule_ship_date>2006-03-16</schedule_ship_date>
<order_line_id>799082</order_line_id>
<quantity>2</quantity>
<part_num>M23SHA-03010</part_num>
</item>
<item>
<item_reason>NEW</item_reason>
<type>STANDARD</type>
<creation_date>2006-03-09</creation_date>
<update_date>2006-03-09</update_date>
<schedule_ship_date>2006-03-16</schedule_ship_date>
<order_line_id>799091</order_line_id>
<quantity>1</quantity>
<part_num>M324CMC-08003</part_num>
</item>
</items>
<comment>ATTN: DAVID SMITHY</comment>
</shippingRequest>
=======================
The Schema
=======================
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://foo.comp.com" xmlns="http://foo.comp.com">
<!-- Schema definition for shipping requests sent to rbed from foo -->
<xsd:element name="shippingRequest" type="ShippingRequestType"/>
<xsd:complexType name="ShippingRequestType">
<xsd:sequence>
<!-- create date of the order -->
<xsd:element name="order_date" type="xsd:date"/>
<!-- rbed or OEM (foo) name -->
<xsd:element name="brand_name" type="xsd:string"/>
<xsd:element name="shipRequestType" type="ShipRequestType"/>
<xsd:element name="shipHow" type="ShippingMethod"/>
<!-- URL pointing to a PDF file containing the export documents -->
<xsd:element name="exportDocsURL" type="xsd:string"/>
<!-- foo so (order) number (this is used in the documentation file names) -->
<xsd:element name="so" type="xsd:string"/>
<!-- rbed or OEM (foo) po number -->
<xsd:element name="po" type="xsd:string"/>
<!-- rbed or OEM customer's PO number -->
<xsd:element name="customer_po" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="shipInstructions" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="shipTo" type="Address"/>
<xsd:element name="items" type="Items"/>
<xsd:element name="comment" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ShipRequestType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Sale"/>
<xsd:enumeration value="Evaluation"/>
<xsd:enumeration value="RMA Replacement"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="ShippingMethod">
<xsd:attribute name="shippingCarrier" type="xsd:string"/>
<xsd:attribute name="shippingServiceLevel" type="xsd:string"/>
<xsd:attribute name="modeOfTransport" type="xsd:string"/>
<xsd:attribute name="freightTerms" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="Address">
<xsd:sequence>
<xsd:element name="company" type="xsd:string"/>
<xsd:element name="attn" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="address2" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="address3" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="postalCode" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
<xsd:element name="phone" type="xsd:string" minOccurs="0" maxOccurs="1"/>
<xsd:element name="email" type="xsd:string" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="LineItemType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="MODEL"/>
<xsd:enumeration value="CLASS"/>
<xsd:enumeration value="OPTION"/>
<xsd:enumeration value="CONFIG"/>
</xsd:restriction>
</xsd:simpleType>
<!--
An item is a single line item from the foo order system. It doesn't necessarily
represent an actuall orderable part number. Sometime it is just there to show
configuration information.
-->
<xsd:complexType name="Items">
<xsd:sequence>
<xsd:element name="item" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<!-- the reson this line is in the data -->
<xsd:element name="item_reson" type="xsd:string"/>
<!-- the type of this line on the order -->
<xsd:element name="type" type="LineItemType"/>
<!-- create date of the line item -->
<xsd:element name="creation_date" type="xsd:date"/>
<!-- date of last update to the line item -->
<xsd:element name="update_date" type="xsd:date"/>
<!-- date the line item is supposed to be shipped -->
<xsd:element name="schedule_ship_date" type="xsd:date"/>
<!-- this is the order_line_id of the item on the order that is this items parent line,
if this line is it's own parent then it is the top of a configured item. This happens
when the part_num element is a star "*" part. A generated part number for a config.
-->
<xsd:element name="parent_line_id" type="xsd:integer" minOccurs="0" maxOccurs="1"/>
<!-- uniquely identifies this PO line item in our OEM's database -->
<xsd:element name="order_line_id" type="xsd:integer" minOccurs="1" maxOccurs="1"/>
<xsd:element name="quantity">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxExclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<!-- used in the packing list -->
<xsd:element name="part_num" type="xsd:string"/>
<!-- Special packing instructions, shipping instructions, etc. -->
<xsd:element name="note" type="xsd:string" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
=================
The code
=================
public boolean validateSchema(String schemaURL, String xml, String ordernumber)
{
boolean retval = true;
try
{
DOMParser domParser = new DOMParser();
domParser.setFeature("http://xml.org/sax/features/validation",true);
domParser.setFeature("http://apache.org/xml/features/validation/schema",true);
domParser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
domParser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",schemaURL);
Validator handler=new Validator();
domParser.setErrorHandler(handler);
domParser.parse(new InputSource(new StringReader(xml)));
if(handler.validationError==true)
{
doLogging("validateSchema()","XML failed validation:"+handler.saxParseException.getMessage()+" Column: "+handler.saxParseException.getColumnNumber()+" Line: "+handler.saxParseException.getLineNumber()+" PublicID: "+handler.saxParseException.getPublicId(),true,true,false);
logError(ordernumber,"XML failed validation:","System");
//handler.saxParseException.printStackTrace();
retval=false;
}
else
doLogging("validateSchema()","XML has been validated",true,true,false);
}
catch (Exception e)
{
e.printStackTrace();
}
return retval;
}
private class Validator extends DefaultHandler
{
public boolean validationError = false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{
}
}