0
我對xml架構,xsd文件和驗證非常缺乏經驗;我一直在爲我的項目開發一個xml文件。運行xml驗證類後,控制檯上出現cvc-elt.1.a: Cannot find the declaration of element 'ear'
錯誤。 這是我使用的驗證XSD文件:cvc-elt.1.a:無法找到元素「ear」的聲明
<?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 information</xs:documentation>
</xs:annotation>
<xs:complexType>
//Some stuff
</xs:complexType>
<xs:unique name="faNo">
<xs:selector xpath="ea:facancel"/>
<xs:field xpath="ea:faNo"/>
</xs:unique>
<xs:unique name="facancelNo">
<xs:selector xpath="ea:fa"/>
<xs:field xpath="ea:faNo"/>
</xs:unique>
</xs:element>
<xs:complexType name="headerType">
<xs:sequence>
<xs:element name="version" type="xs:string"/>
<xs:element name="muk" type="ea:vknTcknType"/>
<xs:element name="preparer" type="ea:vknTcknType"/>
<xs:element name="reportNo">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="36"/>
<xs:pattern
value="[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"
/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="periodStartdate" type="xs:date"/>
<xs:element name="periodEndDate" type="xs:date"/>
<xs:element name="partStartDate" type="xs:date"/>
<xs:element name="partEnddate" type="xs:date"/>
<xs:element name="partNo" type="xs:int"/>
<xs:element ref="ds:Signature"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="vknType">
<xs:restriction base="xs:string">
<xs:pattern value="\d\d\d\d\d\d\d\d\d\d"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="tcknType">
<xs:restriction base="xs:string">
<xs:pattern value="\d\d\d\d\d\d\d\d\d\d\d"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="idType">
<xs:restriction base="xs:string">
<xs:length value="16"/>
<xs:pattern value="[A-Za-z0-9]{3}20[0-9]{2}[0-9]{9}"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="vknTcknType">
<xs:choice>
<xs:element name="vkn" type="ea:vknType"/>
<xs:element name="tckn" type="ea:tcknType"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="personType">
<xs:sequence>
<xs:element name="tuPerson" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="vkn" type="earsiv:vknType"/>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="realPerson" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="tckn" type="ea:tcknType"/>
<xs:element name="nameSurname">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="receiverType">
<xs:sequence>
<xs:element name="tuPerson" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="vkn" type="ea:vknType"/>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="realPerson" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="tckn" type="ea:tcknType"/>
<xs:element name="nameSurname">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="0" name="tsstNo" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="paymentTypeEnum">
<xs:restriction base="xs:string">
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="paymentType">
<xs:union memberTypes="ea:paymentTypeEnum ea:paymentTypeOther"/>
</xs:simpleType>
<xs:simpleType name="timeType">
<xs:restriction base="xs:time">
<xs:pattern value="(([01][0-9])|(2[0-3]))(:[0-5][0-9]){2}(\.[0-9]+)?"/>
<xs:minInclusive value="00:00:00"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
這是我使用的驗證類:
package com.meta.ea.adapter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.meta.ea.adapter.util.FormatterUtil;
import com.meta.ea.adapter.util.Matchers;
import com.meta.ea.model.type.CustomerType;
import com.meta.ea.model.type.InvoiceSendType;
import com.meta.ed.adapter.exception.EngineSystemException;
import com.meta.edefter.model.util.DateUtil;
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(){
return " <ds:Signature xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\" Id=\"Signature_1\">\r\n" +
" <ds:SignedInfo Id=\"SignedInfo_1\">\r\n" +
" <ds:CanonicalizationMethod\r\n" +
" Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments\" />\r\n" +
" <ds:SignatureMethod\r\n" +
" Algorithm=\"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256\" />\r\n" +
" <ds:Reference URI=\"\">\r\n" +
" <ds:Transforms>\r\n" +
" <ds:Transform\r\n" +
" Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\" />\r\n" +
" </ds:Transforms>\r\n" +
" <ds:DigestMethod Algorithm=\"http://www.w3.org/2001/04/xmlenc#sha256\" />\r\n" +
" <ds:DigestValue></ds:DigestValue>\r\n" +
" </ds:Reference>\r\n" +
" \r\n" +
" </ds:SignedInfo>\r\n" +
" <ds:SignatureValue Id=\"id1\">\r\n" +
" </ds:SignatureValue>\r\n" +
" <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:X509IssuerName></ds:X509IssuerName></xades:IssuerSerial></xades:Cert></xades:SigningCertificate><xades:SignerRole><xades:ClaimedRoles><xades:ClaimedRole>Tedarikci</xades:ClaimedRole></xades:ClaimedRoles></xades:SignerRole></xades:SignedSignatureProperties></xades:SignedProperties></xades:QualifyingProperties></ds:Object>" +
" \r\n" +
" </ds:Signature>";
}
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.tr/ea.html\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://ea.efat.com.tr 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) {
StringBuilder sb = new StringBuilder();
if(values!=null){
if(values[0] ==null) return sb;
Map<String, String> parameters = new HashMap<String, String>();
int i=0;
for(String value : values){
parameters.put("ATTRIBUTE"+(i+1), value);
i++;
}
sb.append(XmlTemplates.getString(property, parameters));
}
return sb;
}
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: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 creator類來解決這個問題?
我根據你的解釋修改了代碼,現在就工作了。非常感謝你。 – bsel