无法根据 xsd 架构验证 xml 文档(找不到元素“replyMessage”的声明)

发布于 2024-09-05 23:47:20 字数 3483 浏览 5 评论 0原文

我正在使用以下代码根据 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”的声明。

I am using the following code to validate an an XML file against a XSD schema

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
 }

}

As appears, It is a standard code that try to validate the following XML file:

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

Against the following XSD:

https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.53.xsd

The error is :

Validation Error : cvc-elt.1: Cannot find the declaration of element 'replyMessage'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-09-12 23:47:20

您的 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">

Your XML is invalid, because it is not in the namespace required by the schema:

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

You need to change the opening tag of the root element to:

<replyMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="urn:schemas-cybersource-com:transaction-data-1.53">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文