问题
I am trying to validate an xml file. Here is my xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EXTRACT">
<xs:complexType>
<xs:sequence>
<xs:element ref="HEAD"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="HEAD">
<xs:complexType>
<xs:sequence>
<xs:element name="RequestId" type="xs:integer"/>
<xs:element name="RequestsInBatch" type="xs:string"/>
<xs:element name="PeriodDate" type="xs:date"/>
<xs:element name="Type" type="xs:string"/>
<xs:element name="StartDate" type="xs:date"/>
<xs:element name="EndDate" type="xs:date"/>
<xs:element name="PricingDate" type="xs:date"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here is my xml file
<?xml version='1.0'?>
<EXTRACT>
<HEAD>
<RequestId>1</RequestId>
<RequestsInBatch>1,2</RequestsInBatch>
<PeriodDate>2013-03-31</PeriodDate>
<Type>Monthly</Type>
<StartDate>2013-03-01</StartDate>
<EndDate>2013-03-31</EndDate>
<PricingDate>2013-03-29</PricingDate>
<ReceiptTime>2013-04-02 12:30:00</ReceiptTime>
<CreateTime>2013-04-02 16:00:00</CreateTime>
<RecordCount>3</RecordCount>
<ExceptionCount>1</ExceptionCount>
<ExtractType>FLOWS</ExtractType>
<ExtractCurrency>USD</ExtractCurrency>
</HEAD>
</EXTRACT>
I don't care about the other tags, which I thought is indicated by
<xs:any minOccurs="0"/>
But when I run in Java, I get the following error org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'ReceiptTime'
How can I validate the file so it ignores any element I don't declare in my xsd because I don't need them, but they are in the xml document anyway? I cannot control the contents of the xml document so I only need to focus on the data I want to extract.
回答1:
Try setting processContents="lax"
. The default is strict
which means it can be any element so long as it is defined in your schema somewhere.
<xs:any processContents="lax" minOccurs="0"/>
Checkout http://msdn.microsoft.com/en-us/library/aa547371(v=BTS.20).aspx.
来源:https://stackoverflow.com/questions/17256901/org-xml-sax-saxparseexception-cvc-complex-type-2-4-c-the-matching-wildcard-is