我目前正在與編寫一些單元測試以檢查XSD是否對示例XML文件有效有關的問題。我收到以下錯誤:根據XML驗證多個XSD
org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element 'VMS'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1906)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:277)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:244)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:190)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:109)
我的XSD文件:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Messages header -->
<xs:complexType name="VMS_TYPE">
<!-- Recipient of message -->
<xs:attribute name="TO" type="xs:string"/>
<!-- Sender of message -->
<xs:attribute name="FR" type="xs:string" />
<!-- Message identification number consists -->
<xs:attribute name="VN" type="xs:string" />
<!-- Message creation date -->
<xs:attribute name="OD" type="xs:date" />
<!-- Message creation time -->
<xs:attribute name="OT" type="xs:string" />
<!-- Mean of communication to sender of message -->
<xs:attribute name="MTO" type="xs:string" />
<!-- Mean of communication to recipient of message -->
<xs:attribute name="MFR" type="xs:string" />
<!-- TODO - explain attribute meaning -->
<xs:attribute name="MD" type="xs:string" />
</xs:complexType>
</xs:schema>
二
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="vms_header.xsd" />
<!-- Alarm message -->
<xs:complexType name="ALR_TYPE" >
<xs:complexContent>
<xs:extension base="VMS_TYPE">
<xs:sequence>
<xs:element name="ALR" type="ALR_BODY_TYPE" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ALR_BODY_TYPE">
<xs:attribute name="BAT" type="xs:int"/>
<xs:attribute name="TEM" type="xs:int"/>
<xs:attribute name="GPS" type="xs:int"/>
<xs:attribute name="SOS" type="xs:int"/>
<xs:attribute name="CV" type="xs:int"/>
<xs:attribute name="MOD" type="xs:int"/>
<xs:attribute name="FGPS" type="xs:int"/>
<xs:attribute name="FBAT" type="xs:int"/>
<xs:attribute name="FALM" type="xs:int"/>
<xs:attribute name="FSOS" type="xs:int"/>
<xs:attribute name="FFW" type="xs:int"/>
<xs:attribute name="ASOS" type="xs:int"/>
</xs:complexType>
</xs:schema>
三
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="types/alr_msg_type.xsd"/>
<xs:element name="VMS" type="ALR_TYPE"/>
</xs:schema>
我的XML樣本文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VMS TO="CV0001" FR="CAR001" VN="CAR001201406040001" OD="2014-06-04" OT="12:14" MD="+3465104505050">
<ALR BAT="0" TEM="0" GPS="0" SOS="0" CV="0" MOD="0" FGPS="0" FBAT="0" FALM="0" FSOS="0" FFW="0" ASOS="0"/>
</VMS>
得到任何Java代碼? – laune 2014-09-01 10:28:44