简体   繁体   English

针对XML验证多个XSD

[英]Validate multiple XSD against XML

I'm currently fighting with problem with writing some unit tests to check whether XSD are valid with sample XML files. 我目前正在通过编写一些单元测试来检查XSD是否适用于示例XML文件的问题。 I'm getting following error: 我收到以下错误:

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)

My XSD files: 我的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>

Second 第二

<?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>

Third 第三

<?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>

And my XML sample file: 还有我的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>

This works for me, jdk1.8.0_20 这对我有用jdk1.8.0_20

./third.xsd   # probably not the real name, but you no tell
./types/alr_msg_type.xsd
./types/vms_header.xsd

public static void main( String[] args ) throws Exception {
  Handler handler = new Handler();
  DocumentBuilder parser =
    DocumentBuilderFactory.newInstance().newDocumentBuilder();
  parser.setErrorHandler( handler );
  try {
    Document document = parser.parse(new File("x.xml"));
    SchemaFactory factory = 
      SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaFile = new StreamSource(new File("third.xsd"));
    Schema schema = factory.newSchema(schemaFile);
    Validator validator = schema.newValidator();
    validator.setErrorHandler( handler );
    // validate the DOM tree
    try {
      validator.validate(new DOMSource(document));
    } catch (SAXException e) {
      // ...
      System.out.println( "VAlidation error" );
    }
  } catch (SAXParseException e) {
    // syntax error in XML document     
    System.out.println( "Syntax error" );
  }
}

I used following code (with jdk 1.8.0_11): 我使用以下代码(与jdk 1.8.0_11一起使用):

DefaultHandler handler = new DefaultHandler();
        DocumentBuilder parser =
                DocumentBuilderFactory.newInstance().newDocumentBuilder();
        parser.setErrorHandler( handler );
        try {
            Document document = parser.parse(getClass().getResourceAsStream("x.xml"));
            SchemaFactory factory =
                    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Source schemaFile = new StreamSource(getClass().getResourceAsStream("third.xsd"));
            Schema schema = factory.newSchema(schemaFile);
            Validator validator = schema.newValidator();
            validator.setErrorHandler( handler );
            // validate the DOM tree
            try {
                validator.validate(new DOMSource(document));
            } catch (SAXException e) {
                // ...
                System.out.println( "Validation error" );
            }
        } catch (SAXParseException e) {
            // syntax error in XML document
            System.out.println( "Syntax error");
            System.out.println(e);
        }

Result is: 结果是:

Syntax error org.xml.sax.SAXParseException; 语法错误org.xml.sax.SAXParseException; lineNumber: 6; lineNumber:6; columnNumber: 45; columnNumber:45; src-resolve: Cannot resolve the name 'ALR_TYPE' to a(n) 'type definition' component. src-resolve:无法将名称“ ALR_TYPE”解析为一个(n)“类型定义”组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM