簡體   English   中英

無法針對xsd模式驗證xml文檔(找不到元素'replyMessage'的聲明)

[英]Cannot validate xml doc against a xsd schema (Cannot find the declaration of element 'replyMessage')

我正在使用以下代碼針對XSD架構驗證XML文件

package com.forat.xsd;

import java.io.IOException;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XSDValidate {

 public void validate(String xmlFile, String xsd_url) {
  try {
   SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
   Schema schema = factory.newSchema(new URL(xsd_url));
   Validator validator = schema.newValidator();
   ValidationHandler handler = new ValidationHandler();
   validator.setErrorHandler(handler);
   validator.validate(getSource(xmlFile));

   if (handler.errorsFound == true) {
    System.err.println("Validation Error : "+ handler.exception.getMessage());
   }else {
    System.out.println("DONE");
   }
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private Source getSource(String resource) {
  return new StreamSource(XSDValidate.class.getClassLoader().getResourceAsStream(resource));
 }

 private class ValidationHandler implements ErrorHandler {
  private boolean errorsFound = false;
  private SAXParseException exception;

  public void error(SAXParseException exception) throws SAXException {
   this.errorsFound = true;
   this.exception = exception;
  }

  public void fatalError(SAXParseException exception) throws SAXException {
   this.errorsFound = true;
   this.exception = exception;
  }

  public void warning(SAXParseException exception) throws SAXException {
  }
 }

 /*
  * Test
  */
 public static void main(String[] args) {
  new XSDValidate().validate("com/forat/xsd/reply.xml", 
    "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.53.xsd"); // return error
 }

}

如圖所示,它是嘗試驗證以下XML文件的標准代碼:

<?xml version="1.0" encoding="UTF-8"?>
<replyMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <merchantReferenceCode>XXXXXXXXXXXXX</merchantReferenceCode>
 <requestID>XXXXXXXXXXXXX</requestID>
 <decision>XXXXXXXXXXXXX</decision>
 <reasonCode>XXXXXXXXXXXXX</reasonCode>
 <requestToken>XXXXXXXXXXXXX
 </requestToken>
 <purchaseTotals>
  <currency>XXXXXXXXXXXXX</currency>
 </purchaseTotals>
 <ccAuthReply>
  <reasonCode>XXXXXXXXXXXXX</reasonCode>
  <amount>XXXXXXXXXXXXX</amount>
  <authorizationCode>XXXXXXXXXXXXX</authorizationCode>

  <avsCode>XXXXXXXXXXXXX</avsCode>
  <avsCodeRaw>XXXXXXXXXXXXX</avsCodeRaw>
  <authorizedDateTime>XXXXXXXXXXXXX</authorizedDateTime>
  <processorResponse>0XXXXXXXXXXXXX</processorResponse>
  <authRecord>XXXXXXXXXXXXX
  </authRecord>
 </ccAuthReply>
</replyMessage>

針對以下XSD:

https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/Cyber​​SourceTransaction_1.53.xsd

錯誤是:

驗證錯誤:cvc-elt.1:找不到元素'replyMessage'的聲明。

您的XML無效,因為它不在架構所需的名稱空間中:

targetNamespace="urn:schemas-cybersource-com:transaction-data-1.53"

您需要將根元素的開始標記更改為:

<replyMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="urn:schemas-cybersource-com:transaction-data-1.53">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM