cvc-complex-type.2.4.a:从元素'ea:header'开始发现无效内容。其中一个'{“http://ea.efa.com”:header}'是预期的

时间:2015-06-21 11:13:13

标签: xml xsd

我一直在使用xsd文件验证xml架构。运行xml验证类后,我在控制台上出现cvc-complex-type.2.4.a: Invalid content was found starting with element 'ea:header'. One of '{"http://ea.efa.com":header}' is expected.错误。

这是xsd文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xerces="http://xerces.apache.org"
        xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
        xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" 
        xmlns:ea ="http://ea.efa.com" targetNamespace="http://ea.efa.com
        xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#" elementFormDefault="qualified"
        attributeFormDefault="unqualified">
        <xs:import namespace="http://uri.etsi.org/01903/v1.3.2#" schemaLocation="XAdES.xsd"/>
        <xs:import namespace="http://uri.etsi.org/01903/v1.4.1#" schemaLocation="XAdESv141.xsd"/>
        <xs:import namespace="http://www.w3.org/2000/09/xmldsig#"
            schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd"/>
        <xs:element name="ear">
            <xs:annotation>
               <xs:documentation>ear info</xs:documentation>
            </xs:annotation>
            <xs:complexType>
               <xs:sequence>
                  <xs:element name="header" type="ear:headerType" maxOccurs="1" minOccurs="1"/>
        </xs:complexType>
    </xs:schema>

这是我用于创建xml的类:

    package com.meta.ea.adapter;

 //imports


public class XmlCreator {
    private static final Object NEW_LINE = "\r\n";
    private static final Object TAB = "\t";
    static final Logger logger = LoggerFactory.getLogger(XmlCreator.class);
    ReportEngine engine ;

    public Connection conn;

    public XmlCreator(MetaFileWriter writer){
        this.engine = writer.processor.engine;
    }

    private CustomerType getCustomerType(String vknTckn){
        if (vknTckn.trim().length()==10) {
            return CustomerType.COMPANY;
        }else if(vknTckn.trim().length()==11) {
            return CustomerType.PERSON;
        }else {
            return null;
        }
    }

    private String getFromDB(Map row, String columnName){
        return (row.get(columnName) == null) ? "" : row.get(columnName).toString();
    }

    private ResultSet getTaxResultFromDB(int invoiceId) {
        ResultSet selectTaxesResult = null;
        try{
            PreparedStatement selectTaxesStatement;
            String selectTaxesQuery=IQuery.selectTaxesQuery;
            conn = createConnection();
            selectTaxesStatement = conn.prepareStatement(selectTaxesQuery, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
            selectTaxesStatement.setInt(1, invoiceId);
            selectTaxesStatement.setFetchSize(ReportEngine.FETCH_SIZE_100);
            selectTaxesResult = selectTaxesStatement.executeQuery();
        }catch (SQLException ex) {
            logger.error(ex.getLocalizedMessage());
            ex.printStackTrace();
        }catch (Exception ex) {
            logger.error(ex.getLocalizedMessage());
            ex.printStackTrace();
        }
        return selectTaxesResult;

    }

    private Connection createConnection() throws Exception {
        Connection connect = null;
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/datasource-definition-beans.xml");
        Object o = ctx.getBean("dataSource");
        DataSource dataSource = (DataSource) o;
        connect = dataSource.getConnection();
        connect.setAutoCommit(false);
        return connect;
    }

    public String getEmptySignature(){
        //some stuff..
    }

    public String getUniqueId() throws EngineSystemException{
        return engine.getUniqueueID();
    }

    public String getHeaderContent() throws EngineSystemException{
        StringBuilder sb = new StringBuilder();
        sb.append("<ear xmlns:ea=\"http://www.efa.com/ea.html\" xmlns=\"http://ea.efa.com\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  xsi:schemaLocation=\"http://ea.efa.com ea.xsd\" xmlns:xades=\"http://uri.etsi.org/01903/v1.3.2#\">").append(NEW_LINE);
        sb.append("<ear>");
        sb.append("<ea:header>").append(NEW_LINE);
        sb.append(TAB).append(getContent("VERSION", engine.getVersion())).append(NEW_LINE);
        sb.append(TAB).append("<ea:muk>").append(NEW_LINE);
        sb.append(TAB).append(TAB).append(getContent("MUK_VKN", engine.company.getVknTckn())).append(NEW_LINE);
        sb.append(TAB).append("</ea:muk>").append(NEW_LINE);
        sb.append(TAB).append("<ea:preparer>").append(NEW_LINE);
        sb.append(TAB).append(TAB).append(getContent("PREPARER_VKN", engine.getIntegratorVKN())).append(NEW_LINE);
        sb.append(TAB).append("</ea:preparer>").append(NEW_LINE);
        sb.append(TAB).append(getContent("REPORT_NO", engine.reportMasterDTO.getUuid())).append(NEW_LINE);
        String startOfPeriod= FormatterUtil.getFirstDayOfMountFromPeriod(engine.period).toString();
        String endOfPeriod= FormatterUtil.getLastDayOfMountFromPeriod(engine.period).toString();
        sb.append(TAB).append(getContent("PERIOD_START_DATE", startOfPeriod)).append(NEW_LINE);
        sb.append(TAB).append(getContent("PERIOD_END_DATE", endOfPeriod)).append(NEW_LINE);
        sb.append(TAB).append(getContent("PART_START_DATE", DateUtil.formatDate(engine.processor.fwReport.getPartStartDate(), DateUtil.DATE_FORMAT_2))).append(NEW_LINE);
        sb.append(TAB).append(getContent("PART_END_DATE", DateUtil.formatDate(engine.processor.fwReport.getPartEndDate(), DateUtil.DATE_FORMAT_2))).append(NEW_LINE);
        sb.append(TAB).append(getContent("PART_NO", String.valueOf(engine.processor.fileIndex))).append(NEW_LINE);
        sb.append(TAB).append(getEmptySignature()).append(NEW_LINE);
        sb.append("</ea:header>").append(NEW_LINE);

        return sb.toString();
    }

    private StringBuilder getContent(String property, String... values) {
        //some stuff
    }

    public  XmlBuilder newEntryDetail(Map<String, Object> faturaRow) {
        StringBuilder sb = new StringBuilder();

        sb.append("<ea:fa>").append(NEW_LINE);
        sb.append(getInvoiceBody(faRow));
        sb.append(getTaxXml(faRow));
        sb.append(getCustomerInformation(faRow));
        sb.append(getInternetSaleInformation(faRow));
        sb.append("</ea:fa>").append(NEW_LINE);

        XmlBuilder xmlBuilder = new XmlBuilder(null);
        xmlBuilder.append(sb);
        return xmlBuilder;
    }

    //Some stuff..

    public String getFooterContent(){
        return "</ear>";
    }
}

这是上面类创建的xml文件:

 <ear xmlns:ea="http://www.efa.com/ea.html"
    xmlns="http://ea.efa.com"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
xsi:schemaLocation="http://ea.efa.com ea.xsd" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">
        <ear><ea:header>
            <ea:version>2.0</ea:version>
            <ea:muk>
                <ea:vkn>0510037140</ea:vkn>
            </ea:muk>
            <ea:preparer>
                <ea:vkn>6190524774</ea:vkn>
            </ea:preparer>
            <ea:reportNo>7BF70141-553A-40EE-896B-52D8F9E74765</ea:reportNo>
            <ea:periodStartDate>2015-06-01</ea:periodStartDate>
            <ea:periodEndDate>2015-06-30</ea:periodEndDate>
            <ea:partStartDate>2015-06-16</ea:periodStartDate>
            <ea:partEndDate>2015-06-16</ea:partEndDate>
            <ea:partNo>18</ea:partNo>
                <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="Signature_1">
                <ds:SignedInfo Id="SignedInfo_1">
                    <ds:CanonicalizationMethod
                        Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" />
                    <ds:SignatureMethod
                        Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                    <ds:Reference URI="">
                        <ds:Transforms>
                            <ds:Transform
                                Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                        </ds:Transforms>
                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                        <ds:DigestValue></ds:DigestValue>
                    </ds:Reference>

                </ds:SignedInfo>
                <ds:SignatureValue Id="id1">
                </ds:SignatureValue>
                <ds:KeyInfo><ds:KeyValue><ds:RSAKeyValue><ds:Modulus/><ds:Exponent/></ds:RSAKeyValue></ds:KeyValue>     <ds:X509Data><ds:X509SubjectName>dummy</ds:X509SubjectName><ds:X509Certificate/></ds:X509Data></ds:KeyInfo>     <ds:Object><xades:QualifyingProperties Target="Signature_1"><xades:SignedProperties Id="SignedProperties_1"><xades:SignedSignatureProperties><xades:SigningTime>2013-01-28T12:04:41+02:00</xades:SigningTime><xades:SigningCertificate><xades:Cert><xades:CertDigest><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>+VSgWjpP9XlNGk6EM7WYWzHXQ2xrzUZLas1QR+IS7Rc=</ds:DigestValue></xades:CertDigest><xades:IssuerSerial><ds:X509SerialNumber>586340106507</ds:X509SerialNumber></xades:IssuerSerial></xades:Cert></xades:SigningCertificate><xades:SignerRole><xades:ClaimedRoles><xades:ClaimedRole></xades:ClaimedRole></xades:ClaimedRoles></xades:SignerRole></xades:SignedSignatureProperties></xades:SignedProperties></xades:QualifyingProperties></ds:Object>        
            </ds:Signature>
        </ea:header>
            //Some stuff..
        </ear>

我对xml&amp; xsd验证没有太多经验;所以我看不出我犯的错误。我该如何修复我的代码?

1 个答案:

答案 0 :(得分:0)

采用这个定义:

<xs:element name="header" type="ear:headerType" maxOccurs="1" minOccurs="1"/>
在您的架构中

,我找不到前缀为ear

的命名空间